我有两个简单的任务
task initdb { println 'database' }
task profile(dependsOn: initdb) << { println 'profile' }
在运行时,控制台中的结果如下所示
当我的任务看起来像这样
task initdb { println 'database' }
task profile() << { println 'profile' }
控制台中的结果是
如果initdb
任务在运行时未在任务profile
中使用时如何跳过? (不使用-x
)
答案 0 :(得分:2)
此行为的原因是initDb
未正确声明。它缺少<<
,因此println
语句在配置时而不是执行时间运行。这也意味着语句总是运行。这并不意味着任务被执行(在第二个例子中它没有执行)。
为避免此类错误,我建议使用更明确,更常规的doLast
语法,以支持<<
:
task profile {
// code in here is about *configuring* the task;
// it *always* gets run (unless `--configuration-on-demand` is used)
dependsOn initdb
doLast { // adds a so-called "task action"
// code in here is about *executing* the task;
// it only gets run if and when Gradle decides to execute the task
println "profile"
}
}