Gradle 将在配置阶段执行所有未使用<< 声明的任务。如果您想将任务的执行延迟到执行阶段,那么您只需添加<<
在你的build.gradle
task helloConfiguration { task ->
println "Hello configuration phase task! $task.name"
}
/* Notice the `<<` this denotes to gradle to not execute
* the closure during the configuration phase. Instead
* delay closure's execution till the execution phase.
*/
task helloExecution << { task ->
println "Hello execution phase task! $task.name"
}
helloExecution.dependsOn helloConfiguration
然后在执行helloExecution 任务时,我们看到两者都在运行,并确保了顺序。接下来,如果我们只想运行配置构建的任务,我们可以单独执行,并且只运行单个任务。
$ gradle helloExecution
Hello configuration phase task! helloConfiguration
Hello execution phase task! helloExecution
:helloConfiguration UP-TO-DATE
:helloExecution UP-TO-DATE
BUILD SUCCESSFUL
Total time: 0.64 secs
$ gradle helloConfiguration
Hello configuration phase task! helloConfiguration
:helloConfiguration UP-TO-DATE
BUILD SUCCESSFUL
Total time: 0.784 secs
即使没有提供任何任务,在配置阶段运行的任务也将始终执行,这是我希望您看到的行为。所以给出上面的例子。请注意配置任务已运行但未执行。
$ gradle
Hello configuration phase task! helloConfiguration
:help
Welcome to Gradle 2.10.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
Total time: 0.651 secs
因此,如果您有 5 个任务在配置阶段运行,那么无论命令行 args 尝试为执行阶段的目标调用哪个任务,您都会看到它们全部执行。