Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Gradle dynamic tasks

Tags:

gradle

groovy

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
  1. What does 4.times mean? Why 4?
  2. What does -> mean? Now I understand it as a way to pass a parameter to a task. I'm sure its correct strictly speaking about groovy.
like image 813
Maksim Dmitriev Avatar asked Jan 28 '26 12:01

Maksim Dmitriev


2 Answers

  1. 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.

  2. -> 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.

like image 68
Opal Avatar answered Jan 30 '26 13:01

Opal


Gradle based on Groovy and you can programming in gradle like in groovy or java. For your questions:

  1. Look this http://mrhaki.blogspot.de/2009/09/groovy-goodness-looping-in-different.html

  2. Look this http://www.groovy-lang.org/closures.html

like image 33
CyberAleks Avatar answered Jan 30 '26 15:01

CyberAleks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!