Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the groovy syntax in a gradle task definition

Tags:

gradle

groovy

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?

like image 340
ams Avatar asked Dec 20 '14 21:12

ams


People also ask

What is Groovy in Gradle?

gradle is a Groovy script. Thus it can execute arbitrary code and access any Java library, build-specific Gradle DSL and the Gradle API.

Why Groovy is used in Gradle?

Because Gradle's build language is based on Groovy, and parts of Gradle are implemented in Groovy, Gradle already ships with a Groovy library. Nevertheless, Groovy projects need to explicitly declare a Groovy dependency. This dependency will then be used on compile and runtime class paths.

What is Groovy task?

A custom task type is a simple Groovy class which extends DefaultTask – the class which defines standard task implementation. There are other task types which we can extend from, but in most cases, the DefaultTask class is the appropriate choice.


1 Answers

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.

like image 83
Mark Vieira Avatar answered Oct 06 '22 11:10

Mark Vieira