我有一个相当大的sbt项目(大约30个子项目)。根据我的想法,如果没有在build.sbt
中明确声明,sbt将使用根目录的名称作为根项目的名称。根据项目的签出位置,例如在CI环境中,该名称可能会更改。我目前正在使用sbt 1.2.8。
我的问题是我想为根项目分配一个稳定的名称,以便我可以例如使用sbt root/test
[0]运行所有测试,利用所有子项目的根项目的默认聚合。到目前为止,我发现为根项目分配名称的唯一方法是明确声明项目。但这将禁用默认聚合。
有没有办法为根项目分配一个名称,该名称将保留所有子项目的默认聚合?或者是否有另一种方法来访问命令行上的根项目而不依赖于它的名称?
[0]:build.sbt
使用onLoad in Global := (Command.process("project ...", _)) compose (onLoad in Global).value
更改默认项目。所以只运行sbt test
将无法正常工作。
这是一个潜在的解决方案,无需明确引用根项目。
给定以下项目结构由根项目和子项目core
和util
组成
build.sbt
core
project
src
target
util
以及build.sbt
中的以下构建定义
lazy val util = (project in file("util"))
lazy val core = (project in file("core"))
onLoad in Global := { Command.process("project util", _: State) } compose (onLoad in Global).value
ThisBuild / libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
通过定义自定义任务testAll
,使用test
范围过滤器评估inAnyProject
,我们可以在任何特定子项目中运行所有项目的测试
val testAll = taskKey[Unit]("Run tests in all projects whilst being in any particular sub-project")
ThisBuild / testAll := Def.taskDyn {
(Test / test).all(ScopeFilter(inAnyProject))
}.value
现在执行sbt
默认会加载util
子项目,但是testAll
应该运行所有项目的所有测试:
sbt:util> testAll
[info] RootSpec:
[info] The Root object
[info] - should say root hello
[info] UtilSpec:
[info] The Util object
[info] - should say util hello
[info] CoreSpec:
[info] The Core object
[info] - should say core hello
[info] Run completed in 349 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Run completed in 309 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Run completed in 403 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s, completed 11-Apr-2019 16:29:26
sbt:util>
RootSpec
,CoreSpec
和UtilsSpec
在哪里
src/test/scala/example/RootSpec.scala
core/src/test/scala/example/CoreSpec.scala
util/src/test/scala/example/UtilSpec.scala