本文介绍了gradle任务方法在build.gradle中的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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:

  1. 第一季度

  1. Q1

  • introString name自变量
  • 等同于[dependsOn: hello](a Map)的
  • dependsOn: helloMap<String, ?> args
  • { doLast { println "I'm Gradle" } }Closure configureClosure
  • intro is a String name argument
  • dependsOn: hello which is equivalent to [dependsOn: hello] (a Map) is Map<String, ?> args
  • { doLast { println "I'm Gradle" } } is Closure 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

  1. 第三季度

除了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中的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:57