在运行时跳过gradle中的任务

时间:2014-05-13 07:06:12

标签: gradle

我有两个简单的任务

task initdb { println 'database' }
task profile(dependsOn: initdb) << { println 'profile' }

在运行时,控制台中的结果如下所示

enter image description here

当我的任务看起来像这样

task initdb { println 'database' }
task profile() << { println 'profile' }

控制台中的结果是

enter image description here

如果initdb任务在运行时未在任务profile中使用时如何跳过? (不使用-x

1 个答案:

答案 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"
    }    
}