【问题标题】:Gradle always does println from any taskGradle 总是从任何任务中执行 println
【发布时间】:2014-06-10 21:26:45
【问题描述】:

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

【问题讨论】:

  • 在我看来,这两个问题都在问“为什么 println 总是打印?”。问题的目标不同,但基本问题是关于相同的 Gradle 行为。我认为这两个问题都不应该重复关闭,但我认为任何后续读者都会从 cmets 中的交叉链接中受益。

标签: java gradle println


【解决方案1】:

如果你有以下代码:

task task1 {
    println 'task1 starting'
}

您正处于任务的配置阶段。此阶段在脚本评估期间运行。如果您想在任务执行时打印一些东西,您需要为任务添加一个 action

看起来像:

task task1 << {
   println 'task1 action'
}

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

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

【讨论】:

    【解决方案2】:

    来自第 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多