Gradle总是从任何任务中完成println

我有简单的build.gradle (或任何有println任务的build.gradle

 println GradleVersion.current().prettyPrint() task task1{ println 'task1 starting' } 

现在当我运行$ gradle build我总是看到任务执行或打印输出

 task1 starting :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar :assemble :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build BUILD SUCCESSFUL Total time: 1.291 secs 

为什么总是从println里面输出任务?

如果您有以下一段代码:

 task task1 { println 'task1 starting' } 

您正处于任务的configuration阶段。 这个阶段在脚本评估过程中运行。 如果您希望在执行任务时打印某些内容,则需要为任务添加一个操作

看起来像:

 task task1 << { println 'task1 action' } 

这段代码将在任务运行时进行评估。 <<与在Task的对象上调用doLast方法完全相同。 您可以添加许多操作。

来自第55章。构build生命周期http://www.gradle.org/docs/current/userguide/build_lifecycle.html

 // in `settings.gradle` // println 'This is executed during the initialization phase.' println 'This is executed during the configuration phase.' task configure { println 'This is also executed during the configuration phase.' } task execute << { println 'This is executed during the execution phase.' } 

gradle help运行

输出:

 This is executed during the initialization phase. This is executed during the configuration phase. This is also executed during the configuration phase. :help Welcome to Gradle 1.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 BUILD SUCCESSFUL Total time: 1.882 secs