进行多项目构建时,可以在dependsOn
中列出您依赖的项目,并且任务将首先在依赖项上运行,因此您可以依赖于它们的结果。
还有一个aggregate
任务,它聚合子项目。汇总和取决于子项目的方式有何不同?在什么情况下应使用aggregrate
而不是dependsOn
答案 0 :(得分:0)
关键区别在于aggregate
不会修改 classpath ,也不会在子项目之间建立 ordering 。考虑以下由root
,core
和util
项目组成的多项目构建:
├── build.sbt
├── core
│ ├── src
│ └── target
├── project
│ ├── build.properties
│ └── target
├── src
│ ├── main
│ └── test
├── target
│ ├── scala-2.13
│ └── streams
└── util
├── src
└── target
其中
core/src/main/scala/example/Core.scala:
package example
object Core {
def foo = "Core.foo"
}
util/src/main/scala/example/Util.scala
package example
object Util {
def foo =
"Util.foo" + Core.foo // note how we depend on source from another project here
}
src/main/scala/example/Hello.scala:
package example
object Hello extends App {
println(42)
}
请注意Util.foo
项目对Core.foo
的{{1}}具有类路径依赖性。如果我们现在尝试像这样使用core
建立“依赖关系”
aggregate
然后从lazy val root = (project in file(".")).aggregate(core, util)
lazy val util = (project in file("util"))
lazy val core = (project in file("core"))
项目执行compile
root
它确实会尝试编译所有聚合的项目,但是root/compile
将因为缺少类路径依赖项而编译失败:
Util
查看此问题的另一种方法是执行sbt:aggregate-vs-dependsOn> root/compile
[info] Compiling 1 Scala source to /Users/mario/IdeaProjects/aggregate-vs-dependson/target/scala-2.13/classes ...
[info] Compiling 1 Scala source to /Users/mario/IdeaProjects/aggregate-vs-dependson/core/target/scala-2.13/classes ...
[info] Compiling 1 Scala source to /Users/mario/IdeaProjects/aggregate-vs-dependson/util/target/scala-2.13/classes ...
[error] /Users/mario/IdeaProjects/aggregate-vs-dependson/util/src/main/scala/example/Util.scala:5:18: not found: value Core
[error] "Util.foo" + Core.foo // note how we depend on source from another project here
[error] ^
[error] one error found
[error] (util / Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed 13-Oct-2019 12:35:51
,它应该在输出中缺少show util/dependencyClasspath
依赖性。
另一方面,Core
将修改类路径并在项目之间建立适当的顺序
dependsOn
现在lazy val root = (project in file(".")).aggregate(core, util)
lazy val util = (project in file("util")).dependsOn(core)
lazy val core = (project in file("core"))
给出
root/compile
和sbt:aggregate-vs-dependsOn> root/compile
[info] Compiling 1 Scala source to /Users/mario/IdeaProjects/aggregate-vs-dependson/target/scala-2.13/classes ...
[info] Compiling 1 Scala source to /Users/mario/IdeaProjects/aggregate-vs-dependson/core/target/scala-2.13/classes ...
[info] Compiling 1 Scala source to /Users/mario/IdeaProjects/aggregate-vs-dependson/util/target/scala-2.13/classes ...
[success] Total time: 1 s, completed 13-Oct-2019 12:40:25
在类路径上显示show util/dependencyClasspath
:
Core
最后,sbt:aggregate-vs-dependsOn> show util/dependencyClasspath
[info] * Attributed(/Users/mario/IdeaProjects/aggregate-vs-dependson/core/target/scala-2.13/classes)
和aggregate
并不互斥,实际上,通常同时使用两者,通常在dependsOn
上同时使用aggregate
子项目以帮助构建,同时使用root
为特定子项目手工制作特定订单。