How to translate task keyword in DSL into groovy call?

(Guojun Zhu) #1

I am a java programmer. I understand that most of the DSL magic happens with the groovy syntax sugar over java. I am trying to understand the one with task. For example

task myTask (type:Tar, dependsOn anotherTask){

How is this translated? I guess it first go to ‘Project.task(myTask)’, which gives you a Task object. But how to handle the next section ‘(type:Tar, dependsOn anotherTask)’?

(René Groeschke) #2

Hi, the snippet above is resolved to a method call

Project#task(“myTask”, type:Jar, dependsOn: anotherTask)

to get this working with the snippet above, AST transformation is used. See for more details

cheers, René

(Guojun Zhu) #3


But here is the API info I found for Project:

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

Which one fits this call?

(Peter Niederwieser) #4

The second one.

(Stig Kleppe-Jørgensen) #5

But what about the closure? Is it the second because the closure is empty?

(Peter Niederwieser) #6

My comment referred to Rene’s code. The original code corresponds to ‘Task task(Map<String, ?> args, String name, Closure configureClosure)’.

(Guojun Zhu) #7

Why is that? Groovy can automatically guess the wrong order? Should my original one be ‘Task task(String name,Map args, Closure configureClosure)’ ?

(Peter Niederwieser) #8

It’s due to how named arguments work in Groovy. They are “collected” into a leading parameter of type ‘Map’ at compile time.

(Guojun Zhu) #9

Thank you very much. I read this post and now I understand. Very interesting. So in principle, I can have code like the following?

task type:Tar myTask dependsOn:anotherTask {