SBT 如何将输入从一个 inputTask 传递到另一个 inputTask [英] SBT How to pass input from one inputTask to another inputTask
问题描述
我在使用 sbt v0.13.2 输入任务时遇到问题
I have problem with input tasks using sbt v0.13.2
我想要一个接受输入的任务,将该输入设置为系统属性,然后运行另一个任务,例如使用该系统属性的测试.
I want to have a task which accepts input, sets that input as system property and run another task after all that, for example tests which use that system property.
我需要在单独的任务中设置系统属性,因为在测试期间过度属性不可见.但是,它在同一任务的后续运行期间可见.
I need to set system property in seperate task because overwise property is not visible during test. It is however visible during subsequent run of the same task.
lazy val setEnvironmentTask = taskKey[Unit]("Sets environment variable")
lazy val integrationTest = taskKey[Unit]("Runs integration test")
lazy val runIntegrationTest = taskKey[Unit]("Runs everything")
setEnvironmentTask := {
System.setProperty("checkEnv", "production")
}
integrationTest := {
(testOnly in Test).toTask(" integrations.MatchModelsIntegrationTest").value
}
runIntegrationTest := {
setEnvironmentTask.value
integrationTest.value
}
这有效,但正如您在 setEnvironmentTask 中看到的第二个参数是固定的.
This works but as you can see in the setEnvironmentTask second argument is fixed.
我未能找到解决方案.一些方法导致 error: Illegal dynamic reference
,如下所示,其中两个任务更改为 inputTasks 并添加了导入:
I have failed finding solutions. Some of approaches resulted in error: Illegal dynamic reference
, like below where two tasks are changed to inputTasks and import is added:
import sbt.complete.Parsers.spaceDelimited
lazy val setEnvironmentTask = inputKey[Unit]("Sets environment variable")
lazy val runIntegrationTest = inputKey[Unit]("Runs everything")
setEnvironmentTask := {
val env = spaceDelimited("<arg>").parsed.head
System.setProperty("checkEnv", env)
}
runIntegrationTest := {
val env = spaceDelimited("<arg>").parsed.head
setEnvironmentTask.toTask(env).value // <<<<< this is there is fails with "error: Illegal dynamic reference: env"
integrationTest.value
}
应该像sbt runIntegrationTest production
总而言之,如何创建将输入传播到其他任务的 inputTasks?
In summary, how to create inputTasks which propagate input to other tasks?
推荐答案
import sbt.complete.Parsers.spaceDelimited
lazy val setEnvironmentTask = inputKey[Unit]("Sets environment variable")
lazy val runIntegrationTest = inputKey[Unit]("Runs everything")
lazy val integrationTest = taskKey[Unit]("Runs integration test")
lazy val setEnvTask: Def.Initialize[InputTask[Unit]] = Def.inputTask{
val env = spaceDelimited("<arg>").parsed.head
System.setProperty("checkEnv", env)
}
setEnvironmentTask <<= setEnvTask
runIntegrationTest := setEnvironmentTask.parsed.flatMap{ _ =>
integrationTest.taskValue
}.value
integrationTest := {
println("integration test")
}
这篇关于SBT 如何将输入从一个 inputTask 传递到另一个 inputTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!