4

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" }  
}

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;

but the parameter such as intro(dependsOn: hello) or copy(type: Copy) make me confused, what it should be if add parentheses?

Question 2: why << is shorthand for doLast method? I mean there is a leftshift method in Task API ? what is diff between them?

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.

Opal
  • 81,889
  • 28
  • 189
  • 210
WesleyHsiung
  • 351
  • 2
  • 13
  • I know there are some similar question [What are gradle task definitions in groovy language?](http://stackoverflow.com/questions/25592504/what-are-gradle-task-definitions-in-groovy-language/25592665#25592665),but still I wish can find more answer about these – WesleyHsiung Jul 28 '16 at 05:07
  • Possible duplicate of [Understanding the groovy syntax in a gradle task definition](https://stackoverflow.com/questions/27584463/understanding-the-groovy-syntax-in-a-gradle-task-definition) – tkruse Jan 29 '18 at 05:32

1 Answers1

1

In this particular case:

task intro(dependsOn: hello) {
   doLast { println "I'm Gradle" }  
}

the following method will be invoked:

Task task(Map<String, ?> args, String name, Closure configureClosure);

Since gradle uses a specific DSL it may be hard to tell but:

  1. Q1

    • 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
  2. Q2

<< 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. Q3

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.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • 1
    thanks a lot, that's very helpful. I found the gradle syntax are diff from groovy syntax, and the task method in build.gradle are resolved in [`TaskDefinitionScriptTransformer`](https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/java/org/gradle/groovy/scripts/internal/TaskDefinitionScriptTransformer.java) – WesleyHsiung Jul 29 '16 at 05:50
  • @VincentHsiung, yes, that's true. Gradle uses a dedicated DSL which is utilizes groovy. If you found ma answer helpful please accept it. – Opal Jul 29 '16 at 06:16
  • thanks a lot for the explanation, got a few questions: 1. why there is no comma to separate the arguments, i remember groovy needs it 2. why the order does not match, the signature required args then the name 3. is groovy in gradle using the same syntax as "normal" groovy – HKIT Sep 05 '21 at 01:01