Say taskA
is a heavy task which should only be invoked if it's enabled and taskAEnabled
is the corresponding setting key.
A naive approach would be:
val taskAConditional = Def.task {
(taskAEnabled, taskA) map { (taskAEnabled, taskA) =>
if (taskAEnabled) taskA.value
}
}
This won't work due to sbt design. As taskA now becomes the dependency of taskAConditional, it will be executed regardless of the if logic (i.e. taskAEnabled will be ignored).
Is there a way that I can achieve what I want? (I can't change taskA as it's imported from somewhere else)
In sbt 0.13.x you can use dynamic computations Def.taskDyn:
val taskAConditional = Def.taskDyn {
if (taskAEnabled.value) Def.task {
taskA.value // this `.value` macro will extract result of taskA (execute it) only if taskAEnabled.value == true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With