11

对于我的多项目构建,我正在尝试创建一个验证任务,它只会导致 scct:test 然后按顺序执行 scalastyle。我希望 scct:test 为所有子项目执行,但不是顶级项目。(如果它为顶级项目执行,我会从 scct 得到“等待覆盖率报告超时”,因为该项目中没有源代码也没有测试。)我想做的是创建 verify 作为任务对 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"))
}

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

4

2 回答 2

6

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

首先要注意的是,如果您希望某些任务(verifytest等)在某些项目中运行,您需要将它们定义为子项目的范围。因此,在您的情况下,最直接的做法是定义verifyTaskinsubproject_1subproject_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 线程提到了这个问题,所以我会发布我在那里所做的事情。如果您想手动聚合某些子项目,还有一种称为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
于 2018-02-03T00:24:31.550 回答
0

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

也许你可以试试:

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

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

于 2013-08-25T09:50:35.617 回答