Why is a task executed even if no one calls it?

I reduced the problem I have to the following code (smallest I could get to reproduce the issue):

task clean {
                                                                                                                                                                                      println "Executing clean"
}
   task generateFile(dependsOn: 'clean') {
    println "Executing generateFile"
}

The output I get when I run:

gradle -q clean

is:

Executing clean
Executing generateFile

And the output I expect is:

Executing clean

What am I missing? Why is that generateFile task executed even if I didn’t tell it to?!? Is this supposed to happen?

Note: I’m using gradle version 1.2 (but could reproduce it on 1.3)

Gradle has a configuration phase and an execution phase. In the configuration phase, all tasks will get configured. In the execution phase, only a subset of tasks will get executed. Your ‘println’ statements are configuring the tasks. Everything that the task is supposed to do at execution time needs to be wrapped in ‘doFirst { … }’ or ‘doLast { … }’. (For the latter, there is a ‘<<’ shortcut syntax, but using ‘doLast { … }’ is less error prone.) The Gradle User Guide has more on this.

Thanks for the good answer. I was wondering about that.

Would it be possible, or even practical, to turn off the standard output of the configuration phase through a config option?

‘println’'s are logged on level ‘quiet’, hence they are even shown when running with ‘-q’. You can log using a lesser logging level with ‘log.debug’, ‘log.info’, or ‘log.lifecycle’.