7

I have simple build.gradle (or any build.gradle with task that has println)

println GradleVersion.current().prettyPrint()

task task1{
    println 'task1 starting'
}

Now when I run $ gradle build I always see tasks executing or print output

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

Why there is always output from println inside tasks?

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • Duplicate: [Gradle println prints when it is not called](http://stackoverflow.com/questions/17051140/gradle-println-prints-when-it-is-not-called) – Rylander Apr 25 '14 at 16:08
  • 1
    it appears to me that both questions are asking "Why is println ALWAYS printing?". The goals of the questions are different, however the underlying question is about the same Gradle behavior. I do not think either question should be closed as a duplicate, but I think any subsequent readers will benefit from cross-linking in the comments. – Rylander Apr 28 '14 at 15:33

2 Answers2

25

If You have the following piece of code:

task task1 {
    println 'task1 starting'
}

You're in configuration phase of a task. This phase is run during script evaluation. If You'd like to print something while task is executed You need to add an action for task.

It looks like:

task task1 << {
   println 'task1 action'
}

This piece of code will be evaluated while the task is being run. << is exactly the same as invoking doLast method on Task's object. You can add many actions.

EDIT I also highly encourage you to read this blog post.

Opal
  • 81,889
  • 28
  • 189
  • 210
8

from Chapter 55. The Build Lifecycle 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.'
}

run with gradle help

output:

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
Paul Verest
  • 60,022
  • 51
  • 208
  • 332