English 中文(简体)
What is the difference between these task definition syntaxes in gradle?
原标题:
  • 时间:2010-05-04 18:18:43
  •  标签:
  • 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.

最佳回答

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 } }
问题回答

暂无回答




相关问题
Some error characters appear during the gradle javadoc task

Some error characters appear during the gradle javaDoc build Gradle version 7.1 and I am using windows I tried to change the project encoding to utf-8 and added the idea vm parameter -Dfile.encoding=...

What are real-world examples of Gradle s dependency graph?

As noted in the documentation, Gradle uses a directed acyclic graph (DAG) to build a dependency graph. From my understanding, having separate cycles for evaluation and execution is a major feature for ...

How can I import one Gradle script into another?

I have a complex Gradle script that wraps up a load of functionality around building and deploying a number of NetBeans projects to a number of environments. The script works very well, but in essence ...

热门标签