gradle中这些任务定义语法之间的区别是什么? [英] What is the difference between these task definition syntaxes in gradle?
问题描述
任务建立<< {
description =构建任务。
ant.echo('build')
}
p>
任务构建{
description =构建任务。
ant.echo('build')
}
我注意到类型B,当输入 gradle -t
时,任务内的代码似乎被执行,即使列出所有可用的任务,ant也会回应'build'。该描述实际上也以类型B显示。但是,对于类型A,列出可用任务时不执行代码,并且执行 gradle -t
时不显示该描述。 。这些文档似乎没有涉及这两种语法之间的差异(我发现),只是您可以以任何方式定义任务。
第一个语法定义了一个任务,并提供了一些任务执行时要执行的代码。第二种语法定义了一个任务,并提供了一些代码以便立即执行以配置任务。例如:
任务构建<< {println'执行构建任务时执行'}
任务构建{println'执行构建脚本时执行'}
实际上,第一个语法相当于:
任务构建{doLast {println '执行构建任务时执行'}}
因此,在上例中,对于语法A描述不会在gradle -t中显示,因为设置描述的代码在执行任务之前不会执行,当您运行gradle -t时不会发生。
对于语法B,执行ant.echo()的代码针对gradle的每次调用运行,包括gradle -t
提供要执行的操作和
任务构建(描述:'一些描述')<< {一些代码}
任务build {description ='一些描述'; doLast {一些代码}}
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 } }
这篇关于gradle中这些任务定义语法之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!