Understanding the groovy syntax in a gradle task definition

I am new to Gradle and Groovy and trying to understand what is happening at the level of groovy when a gradle task is defined.

task hello  { 
   println "configuring task hello" 
   doLast {
     println "hello there" 
   }
}

From reading the "Gradle In Action" book I know that the task hello {} is a really a call to the task() method of the groovy Project interface. On page 77 it shows that there are 4 methods called task on the Project interface

task(args: Map<String,?>, name:String)
task(args: Map<String,?>, name:String, c:Closure)
task(name: String)
task(name: String, c:Closure)

I understand that the {} is the closure body.

What I don't understand is how does groovy interpret hello in task hello { } according to https://stackoverflow.com/a/25592665/438319 there is a groovy compiler plugin that converts task hello { } into task('hello', { })

My Questions:

  • Where can I find information about the Gradle Groovy Compiler plugin that does the conversion?

  • Is the claim that Gradle scripts are groovy programs technically incorrect since gradle somehow extends the Groovy programming language?

  • Is there a way to get the gradle command to print out the base groovy code that is generated after the compiler plugin has run?


Gradle uses AST Transformations to extend the Groovy syntax. The task definition syntax you mention is just one of the transformations Gradle applies. You can find the implementation for that transform here. To answer your specific questions:

  • The individual transforms that Gradle applies are not specifically documented anywhere that I'm aware of. You could however look at the other classes in the same package of the link above.

  • Gradle scripts support a super-set of Groovy syntax. Any valid Groovy is also valid in a Gradle script, however, a Gradle script is not necessarily (and typically not) valid "default" Groovy.

  • There isn't a way to get an output of the equivalent Groovy code since it's the actual abstract syntax tree that is being manipulated in-memory.


If you want to know more about it check transformVariableExpression function in the gradle source code in TaskDefinitionScriptTransformer class

private void transformVariableExpression(MethodCallExpression call, int index) {
        ArgumentListExpression args = (ArgumentListExpression) call.getArguments();
        VariableExpression arg = (VariableExpression) args.getExpression(index);
        if (!isDynamicVar(arg)) {
            return;
        }

        // Matches: task args?, <identifier>, args? or task(args?, <identifier>, args?)
        // Map to: task(args?, '<identifier>', args?)
        String taskName = arg.getText();
        call.setMethod(new ConstantExpression("task"));
        args.getExpressions().set(index, new ConstantExpression(taskName));
    }

it converts task args?, <identifier>, args? or task(args?, <identifier>, args?) to task(args?, '<identifier>', args?) it finds the task definition in the build.gradle and add quotes around the identifier(task name) so groovy can compile it without problem.