我有简单的build.gradle
(或任何build.gradle
,其任务包含println
)
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
内部的任务总是有输出?
答案 0 :(得分:20)
如果您有以下代码:
task task1 {
println 'task1 starting'
}
您处于任务的配置阶段。此阶段在脚本评估期间运行。如果您想在执行任务时打印某些内容您需要为任务添加操作。
看起来像:
task task1 << {
println 'task1 action'
}
在运行任务时将评估此段代码。 <<
与在Task对象上调用doLast
方法完全相同。您可以添加许多操作。
答案 1 :(得分:7)
。构建生命周期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