Gradle总是从任何任务中进行println输出

7

我有一个简单的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的输出?

Gradle println在未被调用时打印的问题 - Mike Rylander
1
我认为这两个问题都在问“为什么println总是打印?”。虽然问题的目标不同,但根本问题是关于相同的Gradle行为。我不认为任何一个问题应该被关闭为重复,但我认为任何后续读者都会从评论中的交叉链接中受益。 - Mike Rylander
2个回答

26

如果您有以下代码:

task task1 {
    println 'task1 starting'
}

你处于任务的配置阶段。该阶段在脚本评估期间运行。如果你想要在任务执行时打印一些内容,需要为任务添加一个操作

看起来像这样:


看起来像这样:

task task1 << {
   println 'task1 action'
}

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

编辑 我也强烈建议您阅读博客文章。


8

来自第55章。构建生命周期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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接