为什么我的Gradle任务总是在运行?

时间:2013-12-23 05:31:27

标签: gradle

如果我运行./gradlew clean./gradlew tasks --all,它总是运行我的编译任务(我在gradle构建脚本中覆盖了如下所示)

task eclipse(overwrite: true) {
    exec { commandLine = ["./play1.3.x/play", "eclipsify"] }
}

task compileJava(overwrite: true) {
    exec { commandLine = ["./play1.3.x/play", "precompile"] }
}

task deleteDirs(type: Delete) {
    delete 'precompiled', 'tmp'
}

//NOW, assemble needs to zip up directories precompiled, public, lib, and conf
clean.dependsOn('deleteDirs')

我不明白为什么每次都没有运行eclipse并且看起来工作得很好而覆盖编译器不起作用。

2 个答案:

答案 0 :(得分:74)

了解任务配置与任务执行之间的区别非常重要:

task eclipsify {
    // Code that goes here is *configuring* the task, and will 
    // get evaluated on *every* build invocation, no matter
    // which tasks Gradle eventually decides to execute.
    // Don't do anything time-consuming here.
    doLast {
        // `doLast` adds a so-called *task action* to the task.
        // The code inside the task action(s) defines the task's behavior.
        // It will only get evaluated if and when Gradle decides to 
        // execute the task.
        exec { commandLine = ["./play1.3.x/play", "eclipsify"] }
    }
}

// Improving on the previous task declaration, let's now use a *task type* 
// (see `type: Exec` below). Task types come with a predefined task action, 
// so it's typically not necessary to add one yourself. Also, many task types 
// predefine task inputs and outputs, which allows Gradle to check if the task 
// is up-to-date. Another advantage of task types is that they allow for 
// better tooling support (e.g. auto-completion of task properties).
task precompile(type: Exec) {
    // Since this task already has a task action, we only
    // need to configure it.
    commandLine = ["./play1.3.x/play", "precompile"] }
}

如果你没有得到正确的配置与执行,你会看到一些症状,例如很长的启动时间和任务似乎在不应该执行时执行。

要了解哪些任务类型可用以及如何配置它们,请查看Gradle Build Language Reference。此外,还有一个不断增长的第三方插件和任务类型列表。

PS:我更改了任务名称并删除了overwrite: True(这应该仅作为最后的手段使用),以免分散我回答的主要信息。

答案 1 :(得分:3)

Gradle不知道您的来源未被更改。对于任何未知状态,它将任务标记为不是最新的。由于您的任务是100%替换compile,因此您有责任提供任务的状态。

Writing Custom Task Classes章节提供了有关如何开始增量任务的详细信息。

使用--info标记运行项目,以了解为什么Gradle将compile任务标记为不是最新的。

希望它有所帮助。