sbt在Dynamic Task中设置java选项

时间:2014-06-06 16:22:53

标签: scala sbt

我正在尝试编写接受输入参数的任务,将其传递给testOnly任务并执行。它工作得很好,但我无法在此任务中设置系统属性,这将在测试中可见。此属性不应影响其他任务,并且只应在此自定义任务中可见。

这是我的代码示例

lazy val myTestOnly = InputKey[Unit]("myTestOnly", "test only with special sys prop")

val myTestOnlyTask = myTestOnly := {
  Def.inputTaskDyn {
    val args: Seq[String] = spaceDelimited("").parsed

    javaOptions in Test ++= Seq("-Dfoo=bar") // does not work
    testOptions += Tests.Setup(() => System.setProperty("foo", "bar")) //does not work
    System.setProperty("foo", "bar") // does not work

    (testOnly in Test).toTask(" " + args.head)
  }.evaluated
}

当我在sbt控制台中运行时

 myTestOnly com.sampleTest

testOnly执行但“foo”属性不可见

所以qustion是如何在任务定义中设置属性,它只在当前任务的测试运行时中可见?

1 个答案:

答案 0 :(得分:0)

build.sbt中的

val currentTimestamp: String =
  new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
val targetDir
  : String = baseDirectory + "/project/target/mtd/" + currentTimestamp

javaOptions in Test ++= Seq("-Ddynamic_dir_to_set=" + targetDir)

sbt test将保证dynamic_dir_to_set env var的值。

一些理论联系到了低谷:

http://www.scala-sbt.org/0.13.2/docs/Detailed-Topics/Forking.html#change-working-directory http://www.scala-sbt.org/0.13/docs/Plugins-and-Best-Practices.html