I'm reading about dynamic tasks in gradle and want to understand the syntax and meaning of the following code:
4.times { counter ->
task "task$counter" << {
println "I'm task number $counter"
}
}
task0.dependsOn task2, task3
4.times mean? Why 4?-> mean? Now I understand it as a way to pass a parameter to a task. I'm sure its correct strictly speaking about groovy.4 is just a random value, you can pick 10 or 15 as well. This value is just used to create multiple tasks. Under the hood this method is invoked for times - think of it as if it was an iterator.
-> is used to indicate a closure param name. Without it, it would look like this:
4.times {
task "task$it" << {
println "I'm task number $counter"
}
}
task0.dependsOn task2, task3
Since it is default closure param name. If multiples closures are used (as in the example - for times and for adding an action), it's good practice to use dedicated names for closures. Here are the docs about closures.
Gradle based on Groovy and you can programming in gradle like in groovy or java. For your questions:
Look this http://mrhaki.blogspot.de/2009/09/groovy-goodness-looping-in-different.html
Look this http://www.groovy-lang.org/closures.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With