sbt:子项目动态聚合 [英] sbt: dynamic aggregation of subproject
问题描述
我想发明一个系统来动态发现子项目并将它们自动聚合到我的项目中.或者至少以某种方式配置它.
I would like to invent a system to dynamically discover subprojects and aggregate them into my project automatically. Or at least configure this somehow.
为此,我计划拥有一个模块"文件夹或一个包含模块路径的可选配置文件.
To do this, I'm planning to have either a "modules" folder or an optional configuration file containing paths to the modules.
在任何情况下,我都需要遍历子文件夹(或遍历配置文件中的路径列表),并聚合每个子项目.我不知道该怎么做.
In any case I'd need to loop through subfolders (or loop through a list of paths from a configuration file), and aggregate each subproject. I don't know how to do that.
目前我正在使用 build.sbt
文件在 Play 框架中构建.我需要像这样添加循环:
Currently I'm building in the Play framework with the build.sbt
file. I would need to add the loop like this:
name := "mysite"
version := "1.0"
scalaVersion := "2.11.1"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
//pseudocode:
foreach( folder in the 'modules' folder) {
lazy val module = (project in file(folder)).enablePlugins(PlayJava)
root = root.dependsOn(module).aggregate(module)
}
有没有办法做到这一点?
Is there a way to do this?
编辑 3:这里的代码几乎可以工作了:
EDIT 3: The code here is almost working:
object MyBuild extends Build {
name := "mysite"
version := "1.0"
scalaVersion := "2.11.6"
var m = new File("modules")
var list = Seq[ProjectReference]()
var deps = Seq[ClasspathDependency]()
if (m.exists) {
val subs = m.listFiles.filter ( _.isDirectory ).foreach { folder =>
var modulePath = new File("modules", folder.getName)
println("Found module " + modulePath)
lazy val module:ProjectRef = ProjectRef(modulePath,folder.getName)
lazy val dep:ClasspathDependency = ClasspathDependency(module, None)
list = list :+ module
deps = deps :+ dep
}
}
lazy val root = Project(id = "mysite", base = file(".")).enablePlugins(PlayJava).aggregate(list:_*).dependsOn(deps:_*)
}
编辑 4:
请参阅下面的 Dale Wijnand 的解决方案.
See Dale Wijnand's solution below.
关于错误:RuntimeException: No project 'myModule' in 'file:/Users/me/mysite/modules/myModule'
.我使用 https://stackoverflow.com/a/28820578
推荐答案
这里:
project/Build.scala
import sbt._
import sbt.Keys._
import play.sbt._
import play.sbt.Play.autoImport._
object Build extends Build {
val commonSettings: Seq[Setting[_]] = Seq(
scalaVersion := "2.11.7"
)
lazy val modules = (file("modules") * DirectoryFilter).get.map { dir =>
Project(dir.getName, dir).enablePlugins(PlayJava).settings(commonSettings: _*)
}
lazy val root = (project in file("."))
.enablePlugins(PlayJava)
.settings(
name := "mysite",
version := "1.0"
)
.settings(commonSettings: _*)
.dependsOn(modules map (m => m: ClasspathDependency): _*)
.aggregate(modules map (m => m: ProjectReference): _*)
override lazy val projects = root +: modules
}
注意,确保模块目录不包含将它们定义为项目的 build.sbt
文件,因为这会导致混淆 RuntimeException: No project 'x' in 'file:/x'
类型异常,参见不能将 sbt 0.13.7 与 Play 子项目一起使用
Note, make sure that the module directories don't also contain build.sbt
files defining them as projects as that will cause confusing RuntimeException: No project 'x' in 'file:/x'
type exception, see Can't use sbt 0.13.7 with Play subprojects
这篇关于sbt:子项目动态聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!