题
一种)
task build << {
description = "Build task."
ant.echo('build')
}
b)
task build {
description = "Build task."
ant.echo('build')
}
我注意到,使用B类,任务中的代码似乎在键入时执行 gradle -t
- 即使仅列出了所有各种可用任务,Ant也会回响“构建”。该描述实际上也在类型B中显示。但是,在列出可用任务时,使用A类型A无代码,并且执行时未显示描述 gradle -t
. 。这些文档似乎并没有涉及这两个语法之间的区别(我发现),只是您可以以任何一种方式定义任务。
解决方案
第一个语法定义了一个任务,并提供了任务执行时要执行的一些代码。第二个语法定义了一个任务,并提供了一些要立即执行以配置任务的代码。例如:
task build << { println 'this executes when build task is executed' }
task build { println 'this executes when the build script is executed' }
实际上,第一个语法等同于:
task build { doLast { println 'this executes when build task is executed' } }
因此,在上面的示例中,对于语法a,描述不会在gradle -t中显示,因为设置描述的代码直到执行任务后才执行,这是在运行gradle -t时不会发生的。
对于语法B
为了提供执行的措施和对任务的描述,您可以执行以下操作:
task build(description: 'some description') << { some code }
task build { description = 'some description'; doLast { some code } }
不隶属于 StackOverflow