首页 > 解决方案 > fullRunTask 与 sbt 中的非默认项目

问题描述

build.sbt用不同的来源定义了几个项目。我想保持根项目保持原样,使用它自己的源集(和类路径),但添加一个 sbt 任务来编译和运行非默认项目。

这是(缩短的)build.sbt

lazy val root = project in file(".")

lazy val anotherModule = project in file("modules/another")

lazy val runAnother = taskKey[Unit]("Run a task from anotherModule")
fullRunTask(initialImport, Compile, "another.module.Main")

another.moduleis,事实上, in modules/another/srcand so 不包含在根项目中。

运行时,在类路径中sbt runAnother找不到another.module

我不希望root项目依赖,anotherModule因为除此任务外,根项目不需要其中的代码。

如何使用指定模块的类路径运行此任务?

标签: scalasbt

解决方案


runner可以用来代替fullRunTask这样

lazy val anotherModule = project in file("modules/another")

lazy val runAnother = taskKey[Unit]("Run task with the classpath of runAnother sub-project")
runAnother := {
  (runner in Compile).value.run(
    mainClass = "another.module.Main",
    classpath = (anotherModule / Compile / fullClasspath).value.files,
    options = Array[String](),
    log = streams.value.log
  )
}

注意我们如何访问anotherModule子项目的类路径

classpath = (anotherModule / Compile / fullClasspath).value.files

现在sbt runAnother应该能够anotherModule在默认项目中运行子项目root


推荐阅读