What is the difference between these task definition syntaxes in 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 inside the task seems to run when you type gradle -t - ant expels the "build" even with a simple listing of all available available tasks. The description is also actually displayed with type B. However, with type A, no code is executed when listing the available tasks, and the description is not displayed when gradle -t executed. The docs don't seem to delve into the difference between the two syntaxes (which I found), only so that you can define the task anyway.

+42
gradle
May 04 '10 at 18:18
source share
1 answer

The first syntax defines the task and provides some code that will be executed when the task is performed. The second syntax defines the task and provides some code for the immediate execution of 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:

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

So, in your example above for syntax A, the description does not appear in gradle -t, because the code that sets the description does not execute before the task completes, which does not happen when gradle -t is started.

For syntax B, the code executing ant.echo () is run for every gradle call, including gradle -t

To provide both an action for execution and a description of the task, you can perform any of the following actions:

 task build(description: 'some description') << { some code } task build { description = 'some description'; doLast { some code } } 
+54
May 05 '10 at 5:12
source share



All Articles