如何将Play 2.2 Scala应用程序创建为SBT子项目 [英] How to create a Play 2.2 Scala application as an SBT sub-project

查看:80
本文介绍了如何将Play 2.2 Scala应用程序创建为SBT子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Scala应用程序,该应用程序由一个库项目(以下称为server),一个Thrift服务器项目(以下称为server)和一个Play网络应用程序项目(以下称为web)组成).这三个都是用Scala编写的,并使用sbt构建.

I am trying to create a Scala application consisting of a library project (let's call this common), a Thrift server project (let's call this server) and a Play web application project (hereinafter known as web). All three are written in Scala and built with sbt.

我的项目结构如下:

myproject/
-common/
  ...
-server/
  ...
-web/
  -app/
  -conf/
  ...
-project/
  -Build.scala
  -build.properties
-build.sbt

我的build.sbt文件(略有简化)看起来像这样:

My build.sbt file (simplified a bit) looks like this:

import play.Project._

name := "myproject"

version := "1.0-SNAPSHOT"

lazy val common = project

lazy val web = project
    .settings(playScalaSettings: _*)
    .dependsOn(common)

lazy val server = project
    .dependsOn(common)

lazy val root = project.in(file("."))
    .aggregate(common, web, server)

此问题是,根项目不是Play项目,因此play命令不起作用(错误出现

The problem with this is that the root project is not a Play project, so the play command does not work (it errors out with

java.lang.RuntimeException: */*:playRunHooks is undefined.
at scala.sys.package$.error(package.scala:27)

如果我在SBT文件中的version行之后插入playScalaSettings行,则可以通过使根项目看起来像Play项目来解决此问题,但是我还有另一个问题:play run命令尝试执行以下操作:运行根项目,而不是web子项目.显然,当在web子目录中运行play run命令时,该命令不起作用,因为那里没有描述项目及其依赖项的SBT文件.

I can fix this by making the root project look like a Play project if I insert the line playScalaSettings after the version line in the SBT file, but then I have another problem: the play run command attempts to run the root project, not the web subproject. Obviously the play run command does not work when run in the web subdirectory since there is no SBT file there to describe the project and its dependencies.

我正在寻找一种解决方案,使我可以保留此项目结构(这意味着Play项目是我的应用程序中许多子项目之一),同时保留所有Play框架的热度,如代码更改(甚至代码更改)时的热更新.在像common这样的依赖库中.

I am looking for a solution that allows me to retain this project structure (meaning the Play project is one of many sub-projects in my application), while retaining all the Play framework hotness like hot updates when code changes (even code in dependent libraries like common).

我以为我找到了解决方案,方法是运行play以获得交互式控制台,然后

I thought I found the solution by running play to get the interactive console, and then

 project web
 run

那行得通,但是在命令行上行不通. play web/run由于某种原因运行了根级run命令,如上所述,该命令不起作用,因为根应用程序不是Play应用程序.

That works, but it doesn't work on the command line. play web/run for some reason runs the root level run command, which as noted above does not work because the root application is not a Play application.

注意:以前在Play 2.0上下文中的上也曾问过类似的问题.将Play框架用作SBT非根模块,但是答案并不令人满意,我也不认为从Play 2.2起它还是正确的.

Note: A similar question was asked before in the context of Play 2.0 at Play Framework as SBT Non-Root Module, but the answer is not satisfactory, nor do I believe it is still correct as of Play 2.2.

推荐答案

如果

 play (entering shell)
 project web
 run

有效,那么您可以在命令行中使其有效:

works, then you can make it work from the command line:

 play "project web" "run"

您可以在命令行中执行任何操作,无论您在Shell中可以执行什么操作.

You can do in command line whatever you can do in the shell.

我具有相同的项目结构,这是对我来说很好的工作方式.

I have the same project structure and it is the way to do that works fine for me.

顺便说一句,我不认为热门的重装游戏与Play有关.它是由Play使用的SBT提供的增量编译.我认为play命令只是一些被黑客入侵的SBT启动器.

By the way, I don't think the hot reload stuff is related to Play. It is incremental compilation that is provided by SBT which is used by Play. The play command is just some hacked SBT launcher I think.

以下命令对我来说效果很好:

The following command works fine for me:

 sbt "project web" "run"

它会以热重载的方式启动Play项目.

And it starts the Play project with hot reload.

我认为您甚至可以使用

 sbt "project web" "~run"

每次更改源文件时,它将尝试重新编译,而不是等待浏览器刷新,这将赢得一些时间.

Which will try to recompile each time you change a source file, instead of waiting for a browser refresh, and will make win some time.

这篇关于如何将Play 2.2 Scala应用程序创建为SBT子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆