问题描述
我是gradle和groovy的新手,正在阅读Gradle的usr指南,并对任务方法有一些语法问题:
I am new to gradle and groovy,I am reading the usr guide of Gradle, and have some syntax questions on task method:
task intro(dependsOn: hello) {
doLast { println "I'm Gradle" }
}
问题1:在上面的代码中,Project
API中调用了哪种方法?我知道API中有四个重载:
Question 1:in above code, which method is called in Project
API ? I know there are four overload in API:
Task task(String name, Closure configureClosure);
Task task(Map<String, ?> args, String name, Closure configureClosure);
Task task(Map<String, ?> args, String name) throws InvalidUserDataException;
Task task(String name) throws InvalidUserDataException;
但是像intro(dependsOn: hello)
或copy(type: Copy)
这样的参数使我感到困惑,如果加上括号应该是什么?
but the parameter such as intro(dependsOn: hello)
or copy(type: Copy)
make me confused, what it should be if add parentheses?
问题2:为什么<<是doLast
方法的简写?我的意思是Task
API中有一个leftshift
方法吗?他们之间有什么区别?
Question 2: why << is shorthand for doLast
method? I mean there is a leftshift
method in Task
API ? what is diff between them?
问题3:为什么可以在build.gradle 17.1中使用tasks.create()
方法.定义任务,我没有在Project
API或AbstractProject
源代码中看到tasks
属性.
Question 3: why can use tasks.create()
method in build.gradle 17.1. Defining tasks,I did not see tasks
property in Project
API or in AbstractProject
source code.
推荐答案
在这种情况下:
task intro(dependsOn: hello) {
doLast { println "I'm Gradle" }
}
将调用以下方法:
Task task(Map<String, ?> args, String name, Closure configureClosure);
由于gradle使用特定的DSL,因此可能很难分辨,但是:
Since gradle uses a specific DSL it may be hard to tell but:
-
第一季度
Q1
-
intro
是String name
自变量 等同于 -
dependsOn: hello
是Map<String, ?> args
-
{ doLast { println "I'm Gradle" } }
是Closure configureClosure
[dependsOn: hello]
(a Map
)的intro
is aString name
argumentdependsOn: hello
which is equivalent to[dependsOn: hello]
(aMap
) isMap<String, ?> args
{ doLast { println "I'm Gradle" } }
isClosure configureClosure
第二季度
<<
是doLast
的简写,只是为了使其更简洁.您可以使用doLast
,<<
,leftShift
-都一样. leftShift
被覆盖-请参见这里
<<
is a shorthand for doLast
just to make it more concise. You can use doLast
, <<
, leftShift
- it's all the same. leftShift
is overridden - see here
- 第三季度
除了getTasks
之外,没有其他方法tasks
,请参见此处.这就是常规的工作原理-如果method是getter,则()
和get
可以省略,因此project.getTasks()
等效于project.tasks
.
There's no such method tasks
but getTasks
, see here. This is how groovy works - if method is a getter ()
and get
can be omitted, so project.getTasks()
is equivalent to project.tasks
.
这篇关于gradle任务方法在build.gradle中的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!