What is 'task' syntactically in 'task sometask(map) { ... }'?


(Thor Kummer) #1

I can understand most of the syntax of Gradle in terms of Groovy but the syntactic role of ‘task’ in this context eludes me. I assume ‘sometask’ is a method call (to an undefined method) with a map and a closure as arguments, that Gradle intercepts and uses to define ‘sometask’. But I’m not clear about the syntactic role of ‘task’. Is it a method call or something else?


(Luke Daley) #2

It’s hard to understand because it’s actually transformed at compile time. Defining tasks is such a common thing to do so we added some syntactic sugar.

If you have:

task myTask(type: Copy) {
  }

During compile time this is actually transformed to:

task("myTask", type: Copy) {
  }

Which is a syntax you can also use if you need to.


(Hans Dockter) #3

We do this with the help of Groovy’s AST transformation capability.


(Thor Kummer) #4

Ok, is this the only place where Gradle syntax deviates from Groovy’s? Or is there a list somewhere of the differences?


(Hans Dockter) #5

There is one other area where we use AST transformations. This is the buildscript block:

buildscript {
   ...
}

The syntax is groovy, but as it adds to the script classpath, it is evaluated before the actual Groovy build script is compiled.

There is no place where we explicitly document the place where we use Groovy AST’s.


(Thor Kummer) #6

Good to know. Thanks.