How to understand task creation syntax?


(Ryan Nelson) #1

I’ve been over the task docs (Ch6, Ch17, “Task” API), and I’m afraid I still don’t fully understand the syntax of basic task creation. Given the following build file:

task a << {
 println "in a"
}
  task b {
 println "in b"
}
  task (c, type: Copy) << {
 println "in c"
}
  task (d, type: Copy) {
 println "in d"
}

I get the following output when running each task:

--> gradle a b c d
in b
in d
:a
in a
:b UP-TO-DATE
:c UP-TO-DATE
:d UP-TO-DATE

So in fact, defining task “b” and “d” in this manner makes it so they ALWAYS get run, no matter what task I put on the command-line. Conversely, task “c” never runs. I’m new to Groovy, and so perhaps there’s something about the syntax I’m not understanding, but this seems very confusing to me. Can someone clarify?

PS. I should add that I do understand that “<<” is an analog for doLast(), and that there are a few syntactical equivalents of my examples. What I do not understand is what’s happening under-the-hood in cases b, c, and d. Or why, if something there is incorrect, Gradle doesn’t warn me about it.


(Luke Daley) #2

The key concept you are missing is the distinction between configuration and execution.

There’s [a bit in the userguide](http://gradle.org/docs/current/userguide/userguide_single.html#sec:configuration_and_execution_phase_ about this.


(Ryan Nelson) #3

You’re right, that was a bit I was unclear on. (Your link has a bad char in it, FYI, corrected link here.) Thanks!

I was also a little shaky on the difference between setting properties and actions on a task. This blog post helped clear that up as well.