How to translate task keyword in DSL into groovy call?

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)’?

1 Like

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é


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?

The second one.

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

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

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

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

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 {