8

I'm completely new to both gradle and groovy and I'm having trouble to find information about what the below actually is in the groovy language

task myTask(dependsOn: 'compile') << {
   println 'I am not affected'
}

AFAIK the {...} part is a closure which seems to be passed to whatever is defined before <<.

Is task myTask() a call to a constructor?

And what is the thing with the colon that looks like a parameter?

What does << do? Is it an operator that was overloaded by gradle or is it standard groovy?

ben
  • 5,671
  • 4
  • 27
  • 55

1 Answers1

12

dependsOn: 'compile' is a named argument. << is an overloaded operator that adds a task action to the task. (See Gradle User Guide for more information.) { ... } is a closure that implements the task action. myTask is syntactically a nested method call (task(myTask(dependsOn: 'compile') << ...)), but gets rewritten to a String using a Groovy compiler plugin (task('myTask', dependsOn: 'compile') << ...).

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 2
    Project has a mehod: task(Map args, String name), rather than task(String name, Map args) . So it should be task( dependsOn: 'compile', 'myTask') << ... – qhdwangnan May 24 '16 at 11:39
  • Thanks Peter for the pointer. Is that Groovy compiler plugin's behaviour described somehere, or do you perhaps have a link to its sourcecode? – jfrantzius Mar 21 '18 at 09:32