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

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

问题描述

我正在尝试创建一个 Scala 应用程序,它包含一个库项目(我们称之为 common)、一个 Thrift 服务器项目(我们称之为 server)和一个 PlayWeb 应用程序项目(以下称为 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 子项目.显然 play run 命令在 web 子目录中运行时不起作用,因为那里没有 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 Framework 作为 SBT Non-Root Module,但答案并不令人满意,我也不认为它在 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.

推荐答案

if

 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 无关.它是 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天全站免登陆