1

Problem

In a build.gradle script I get that:

 task 'myTestStr', {}         // is the same as
 Project.task('myTestStr, {}) // <-- this

But I don't get what:

task myTest(){} //invokes on the Project instance?

I'm learning Gradle and Groovy coming from a Java background so I'm pretty sure I'm missing some groovy-ness that explains this magic.

Additional Details

Using Intellij to Find Declaration (Ctrl + b) over myTest(){} says that it invokes TaskContainer#create(java.lang.String)

ksrb
  • 1,797
  • 12
  • 22

1 Answers1

1

It invokes exactly the same. If in a method declaration closure is the last argument it can be passed after the closing paren.

For instance inject method:

assert 1*1*2*3*4 == [1,2,3,4].inject(1) { acc, val -> acc * val }
assert 1*1*2*3*4 == [1,2,3,4].inject(1, { acc, val -> acc * val })
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Cool, I saw that in the groovy closure [docs](http://groovy.codehaus.org/Closures#Closures-ClosuresasMethodArguments). So is the myTest in myTest(){} casted to a String? – ksrb Dec 29 '14 at 18:42
  • Yes, it is casted to string. See this answer: http://stackoverflow.com/questions/27584463/understing-the-groovy-syntax-in-a-gradle-task-definition/27584555#27584555 to get some details. – Opal Dec 29 '14 at 18:44
  • Thanks, I had a feeling someone had asked the same question but I couldn't figure out the right terms to search for it. – ksrb Dec 29 '14 at 18:46
  • You're welcome. It's a bit different question. The answer under the link I provided explains gradle DSL internals. If the problem is solved, please accept. – Opal Dec 29 '14 at 18:48
  • That's fine I had a feeling that this kind of question would lead me down the rabbit hole, here we go :). – ksrb Dec 29 '14 at 18:50