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

Dea*_*ler 35 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')
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*ser 80

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

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"] }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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