Understanding the groovy syntax in a gradle task definition

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.

Tags:

Groovy

Gradle