SBT 0.13.x 中的触发执行 [英] Triggered Execution in SBT 0.13.x
问题描述
我正在使用 SBT 0.13.2,我想通过执行编译任务的方式来触发我的任务.我知道我可以通过使用 triggeredBy
方法来实现这一点,如 taskDefinition.triggeredBy(compile in Compile)
.
I am using SBT 0.13.2 and I want to trigger my task by the way of execution of the compile task. I know that I can achieve this by using the triggeredBy
method, as in taskDefinition.triggeredBy(compile in Compile)
.
我有一个 build.sbt
定义了我的自定义任务.
I have a build.sbt
with my custom task defined.
问题是我无法理解为什么以下内容按预期工作
The problem is that I cannot understand why the following works as expected
val triggeredTask = taskKey[Unit]("Triggered by compile")
triggeredTask <<= Def.task {
println("TRIGGERED BY COMPILE")
}.triggeredBy(compile in Compile)
但下一个不起作用(compile
执行得很好但我的任务没有被触发)
but the next does not work (compile
executes just fine but my task is not triggered)
val triggeredTask = taskKey[Unit]("Triggered by compile")
triggeredTask := Def.task {
println("TRIGGERED BY COMPILE")
}.triggeredBy(compile in Compile).value
我的理解是 SBT 0.13 使 <<=
过时了,:=
应该足够了.
My understanding was that SBT 0.13 made <<=
obsolete and :=
should be sufficient.
推荐答案
我可能误解了你的意思.
I might misunderstand what you are trying to do.
如果我让你的例子更冗长,它会变成这样:
If I make your example more verbose it becomes like this:
val triggeredTaskKey = taskKey[Unit]("Triggered by compile")
val anonymousTask = Def.task {
println("TRIGGERED BY COMPILE")
}.triggeredBy(compile in Compile)
// I imagine the macro doing something like this
triggeredTaskKey <<= anonymousTask map (identity)
这也不会触发compile
上的匿名任务.
This also does not trigger the anonymous task on compile
.
请注意,<<=
是提取位置信息的 set
的荣耀版本.而 set
只是 Def.setting(key, value, position)
的别名,它反过来又创建了一个新的 Setting
实例.请注意,内部创建的设置实例具有这三个参数的构造函数.
Note that <<=
is a gloried version of set
that extracts position information. And set
is simply an alias for Def.setting(key, value, position)
which in turn creates a new Setting
instance. Note that the setting instance that is created internally has a constructor of those 3 parameters.
我认为 sbt 没有办法在没有密钥的情况下声明设置(我可能在这里错了).然而,您似乎想要创建一个没有键的设置(即触发).
I do not think there is a way in sbt to declare a setting without a key (I might be wrong here). It seems however that you want to create a setting (that is triggered) without a key.
这可能是由错误引起的,我不确定 sbt 是否应该递归遍历依赖项以查找在其 info
中具有 triggeredBy
键的任务.然而,它目前不是(如 trigger
方法.
This might be caused by a bug, I am not sure if sbt should recursively walk the dependencies to find tasks that have a triggeredBy
key in their info
. It currently however is not (as seen in the trigger
method.
从理论上讲,您可以通过将正确的触发器绑定到某个任意键来创建设置.我不确定这会如何运作.
Theoretically you could make a work around that creates settings with the correct triggers bound to some arbitrary key. I am not sure how that would work.
这篇关于SBT 0.13.x 中的触发执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!