SBT:如何使一个任务依赖于多项目构建中的另一个任务,而不是在根项目中运行?

时间:2013-08-24 23:39:31

标签: scala sbt

对于我的多项目构建,我尝试创建一个只导致scct:test的验证任务,然后按顺序执行scalastyle。我想scct:test来执行所有子项目,但不是顶层项目。 (如果它是针对顶级项目执行的,我会等待覆盖报告"来自scct,因为该项目中没有来源且没有测试。)我曾想过做的是创建验证作为依赖于scct:test和scalastyle的任务。事实证明这是相当巴洛克式的。这是我的顶级项目/目录中的Build.scala:

object MyBuild extends Build {
  val verifyTask = TaskKey[Unit]("verify", "Compiles, runs tests via scct:test and then runs scalastyle")
  val scctTestTask = (test in ScctPlugin.Scct).scopedKey
  val scalastyleTask = PluginKeys.scalastyleTarget.scopedKey

  lazy val root = Project("rootProject",
                      file("."),
                      settings =  Defaults.defaultSettings ++
                                  ScalastylePlugin.Settings ++
                                  ScctPlugin.instrumentSettings ++
                                  ScctPlugin.mergeReportSettings ++
                                  Seq(
                                    verifyTask in Global := {},
                                    verifyTask <<= verifyTask.dependsOn(scctTestTask, scalastyleTask)
                                  )
              ) aggregate(lift_webapp, selenium_tests)

  lazy val subproject_1 = Project(id = "subproject_1", base = file("subproject_1"))

  lazy val subproject_2 = Project(id = "subproject_2", base = file("subproject_2"))
}

但是,验证任务似乎只存在于根项目中;当我运行它时,我看不到在子项目中运行相同的任务。这与我想要的完全相反;我想发布sbt verify并在每个子项目中运行scct:test和scalastyle,但不在顶级项目中运行。我该怎么做呢?

2 个答案:

答案 0 :(得分:6)

解决方案1:在子项目中定义verifyTask

首先要注意的是,如果您想在某些项目中运行某个任务(verifytest等),则需要将它们定义为子项目的范围。因此,在您的情况下,最直接的做法是在verifyTasksubproject_1中定义subproject_2

lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.4"

lazy val verify = taskKey[Unit]("verify")
def verifySettings = Seq(
  skip in verify := false,
  verify := (Def.taskDyn {
    val sk = (skip in verify).value
    if (sk) Def.task { println("skipping verify...") }
    else (test in Test)
  }).value
)

lazy val root = (project in file("."))
  .aggregate(sub1, sub2)
  .settings(
    verifySettings,
    scalaVersion in ThisBuild := "2.12.4",
    skip in verify := true
  )

lazy val sub1 = (project in file("sub1"))
  .settings(
    verifySettings,
    libraryDependencies += scalaTest % Test
  )

lazy val sub2 = (project in file("sub2"))
  .settings(
    verifySettings,
    libraryDependencies += scalaTest % Test
  )

解决方案2:ScopeFilter

最近Reddit thread提到了这个问题,所以我发布了我在那里所做的事情。 如果您想手动聚合某些子项目,还有一种名为ScopeFilter的技术。

请注意,我在这里使用的是sbt 1.x,但它应该与sbt 0.13一起进行一些小改动。

lazy val packageAll = taskKey[Unit]("package all the projects")
lazy val myTask = inputKey[Unit]("foo")

lazy val root = (project in file("."))
  .aggregate(sub1, sub2)
  .settings(
    scalaVersion in ThisBuild := "2.12.4",
    packageAll := {
      (packageBin in Compile).all(nonRootsFilter).value
      ()
    },
    myTask := {
      packageAll.value
    }
  )

lazy val sub1 = (project in file("sub1"))

lazy val sub2 = (project in file("sub2"))

def nonRootsFilter = {
  import sbt.internal.inc.ReflectUtilities
  def nonRoots: List[ProjectReference] =
    allProjects filter {
      case LocalProject(p) => p != "root"
      case _               => false
    }
  def allProjects: List[ProjectReference] =
    ReflectUtilities.allVals[Project](this).values.toList map { p =>
      p: ProjectReference
    }
  ScopeFilter(inProjects(nonRoots: _*), inAnyConfiguration)
}

在上文中,myTask取决于packageAll,它会为所有非根子项目聚合(packageBin in Compile)

sbt:root> myTask
[info] Packaging /Users/xxx/packageall/sub1/target/scala-2.12/sub1_2.12-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Packaging /Users/xxx/packageall/sub2/target/scala-2.12/sub2_2.12-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[success] Total time: 0 s, completed Feb 2, 2018 7:23:23 PM

答案 1 :(得分:0)

我可能错了,但您只是为当前项目定义了验证任务依赖项。

也许你可以试试:

Seq(
   verifyTask in Global := {},
   verifyTask <<= (verifyTask in Global).dependsOn(scctTestTask, scalastyleTask)
)

或者您可以将verifyTask设置添加到所有模块中。