我正在尝试编写接受输入参数的任务,将其传递给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是如何在任务定义中设置属性,它只在当前任务的测试运行时中可见?
答案 0 :(得分:0)
:
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