Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these task definition syntaxes in gradle?

Tags:

gradle

A)

task build << {     description = "Build task."     ant.echo('build')   } 

B)

task build {     description = "Build task."     ant.echo('build')   } 

I notice that with type B, the code within the task seems to be executed when typing gradle -t - ant echoes out 'build' even when just listing all the various available tasks. The description is also actually displayed with type B. However, with type A no code is executed when listing out the available tasks, and the description is not displayed when executing gradle -t. The docs don't seem to go into the difference between these two syntaxes (that I've found), only that you can define a task either way.

like image 709
bergyman Avatar asked May 04 '10 18:05

bergyman


People also ask

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.

What does Gradle use to determine the order in which task can be run?

Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.


1 Answers

The first syntax defines a task, and provides some code to be executed when the task executes. The second syntax defines a task, and provides some code to be executed straight away to configure the task. For example:

task build << { println 'this executes when build task is executed' } task build { println 'this executes when the build script is executed' } 

In fact, the first syntax is equivalent to:

task build { doLast { println 'this executes when build task is executed' } } 

So, in your example above, for syntax A the description does not show up in gradle -t because the code which sets the description is not executed until the task executed, which does not happen when you run gradle -t.

For syntax B the code that does the ant.echo() is run for every invocation of gradle, including gradle -t

To provide both an action to execute and a description for the task you can do either of:

task build(description: 'some description') << { some code } task build { description = 'some description'; doLast { some code } } 
like image 199
Adam Murdoch Avatar answered Oct 03 '22 14:10

Adam Murdoch