如何有条件地调用 sbt 中的任务? [英] How to conditionally invoke a task in sbt?

查看:43
本文介绍了如何有条件地调用 sbt 中的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

taskA是一个繁重的任务,只有在启用时才应该调用,taskAEnabled是相应的设置键.

Say taskA is a heavy task which should only be invoked if it's enabled and taskAEnabled is the corresponding setting key.

一种幼稚的方法是:

val taskAConditional = Def.task {
  (taskAEnabled, taskA) map { (taskAEnabled, taskA) => 
    if (taskAEnabled) taskA.value
  }
}

由于 sbt 设计,这将不起作用.由于 taskA 现在成为 taskAConditional 的依赖,它会被执行而不管 if 逻辑(即 taskAEnabled 将被忽略).

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).

有什么方法可以实现我想要的吗?(我无法更改 taskA,因为它是从其他地方导入的)

Is there a way that I can achieve what I want? (I can't change taskA as it's imported from somewhere else)

推荐答案

在 sbt 0.13.x 中,您可以使用动态计算 Def.taskDyn:

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
  } 
}

这篇关于如何有条件地调用 sbt 中的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆