Understanding groovy syntax in gradle task definition

I am new to Gradle and Groovy and trying to understand what happens at the Groovy level when a Gradle task is given.

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

From reading the book "Gradle In Action", I know that task hello {} is really a call to the task() method of the Groovy Project interface. On page 77 shows that in the Project interface

There are 4 methods called task
 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 {} is a closure body.

I don’t understand how Groovy interprets hello in task hello { } in accordance with https://stackoverflow.com/a/166609/ ... there is a plugin for the Groovy compiler that converts task hello { } to task('hello', { })

My questions:

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

  • Is the claim that Grodle Groovy scripts are technically wrong because Gradle extends Groovy programming language in some way?

  • Is there a way to get the gradle command gradle print the Groovy base code that is generated after the compiler plugin starts?

+70
groovy gradle
Dec 20 '14 at 21:23
source share
2 answers

Gradle uses AST Transformations to extend Groovy syntax. The syntax for defining the task you are talking about is just one of the Gradle transformations. You can find an implementation of this conversion here . To answer your specific questions:

  • The individual conversions that Gradle applies are not described anywhere I know of. However, you can look at other classes in the same link package above.

  • Gradle scripts support Groovy's super-syntax set. Any valid Groovy is also valid in the Gradle script, however the Gradle script is optionally (and usually not) valid for Groovy's "default".

  • It is not possible to get the output of the equivalent Groovy code, as this is the actual abstract syntax tree that is processed in memory.

+81
Dec 20 '14 at 21:37
source share

If you want to know more about this, check out the transformVariableExpression function in the source code of the TaskDefinitionScriptTransformer class of the TaskDefinitionScriptTransformer

 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)); } 

does it convert task args?, <identifier>, args? or task(args?, <identifier>, args?) for task(args?, '<identifier>', args?) finds the definition of the task in build.gradle and adds quotes around the identifier (task name), so it's very cool I can compile it without any problems.

0
Jul 23 '19 at 6:54
source share



All Articles