我有一个包含模块的现有SBT项目。我想将Play 2.2添加到我的项目中作为子模块。这个新的Play模块将取决于其他模块。
到目前为止我发现的主要是关于Play是支持模块的主要项目。如果Play确实支持此设置,请指出我正确的方向如何做到这一点。感谢。
我的预期设置(简化):
my_project
\--- desktop_ui
\--- src/main
\--- src/test
\--- common
\--- src/main
\--- src/test
\--- web_ui (Play framework)
\--- app/controllers
\--- app/views
\--- app/models
\--- conf
答案 0 :(得分:4)
Play 2.2 supports sbt 0.13因此,要将其用于预期的项目布局,我建议在build.sbt
根项目中使用以下my_project
:
import play.Project._
lazy val my_project = project in file(".") aggregate (desktop_ui, common, web_ui)
lazy val desktop_ui = project dependsOn common
lazy val common = project
// no need to dependsOn "common" since it's already a dependency of "desktop_ui"
lazy val web_ui = play.Project(name = "web_ui", path = file("web_ui"))
.dependsOn(desktop_ui)
由于my_project
使用Play 2.2课程 - play.Project
- 来定义Play项目,因此需要project/plugins.sbt
。
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.2-RC1")
这也许可以解释为什么Play模块通常是顶级模块(因为顶层项目需要这么多才能成为Play模块)。
完整的项目布局如下:
$ tree
.
├── build.sbt
└── project
├── build.properties
└── plugins.sbt
1 directory, 3 files
有趣的是,即使没有所有项目目录也没有Play项目(仅build.sbt
中的定义),您仍然可以run
web_ui
项目并访问它使用您的网络浏览器(!)它会因为显而易见的原因而失败,但表明没有太多需要与sbt和Play一起运行。
$ sbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Loading project definition from /Users/jacek/sandbox/so/play-2.2-multi/my_project/project
[info] Updating {file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/project/}my_project-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to my_project (in build file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/)
[my_project]> projects
[info] In file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/
[info] common
[info] desktop_ui
[info] * my_project
[info] web_ui
[my_project]> project web_ui
[info] Set current project to web_ui (in build file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/)
[web_ui]> run
[info] Updating {file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/}common...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Updating {file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/}desktop_ui...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Updating {file:/Users/jacek/sandbox/so/play-2.2-multi/my_project/}web_ui...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
--- (Running the application from SBT, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)
[info] play - Application started (Dev)
答案 1 :(得分:3)
两个选项:
1)有一个'空'主项目聚合你的3个子项目:
root
\--- project
\--- Build.scala
\--- web_ui
\--- common
\--- desktop_ui
在Build.scala中有这样的东西:
lazy val common = Project(id = "common", base = file("common"))
lazy val desktopUi = Project(id = "desktop_ui", base = file("desktop_ui")
lazy val webUi = play.Project(name = "web_ui", path = file("web_ui"))
.dependsOn(common, desktopUi)
lazy val root = Project(id =“root”,base = file(“。”))。aggregate(common,desktopUi,webUi)
使用此选项,您可以从根文件夹启动sbt并构建所有项目。您还可以在此唯一构建定义中定义所有设置和依赖项。
2)可以使用另一种布局来保持子项目彼此独立。我倾向于选择这种方式,因为它更清晰(例如,我可以将common
作为一个独立项目,而不是作为一个子模块)但是构建整个系统并不方便。
root
\--- web_ui
\--- project
\--- Build.scala
\--- common
\--- project
\--- Build.scala
\--- desktop_ui
\--- project
\--- Build.scala
这里,每个项目都是独立的(如果需要,可以使用build.sbt而不是Build.scala,请参阅sbt文档)和web_ui / project / Build.scala:
lazy val common = RootProject(file("../common"))
lazy val desktopUi = RootProject(file("../desktop_ui"))
val main = play.Project(name = "web_ui", path = file("web_ui")).dependsOn(common, desktopUi)
这里,root只用于收集一个文件夹中的所有内容,然后播放项目引用其他模块。