Build scripts are Groovy code

We must keep in mind that Gradle scripts use Groovy. This means we can use all the Groovy's good stuff in our scripts. We already saw in our sample script the use of the so-called Groovy GString. The GString is defined as a string with double quotes and can contain references to variables defined in a ${...} section. The variabled reference is resolved when we get the value of the GString.

But other great Groovy constructs can also be used in Gradle scripts. The following sample script shows some of these constructs:

task numbers << {
    (1..4).each { number ->
        def squared = number * number
        println "Square of ${number} = ${squared}"
    }
}

task list {
    doFirst {
        def list = ['Groovy', 'Gradle']
        println list.collect { it[0].toLowerCase() }.join('&')
    }
}

And when we run the script we get the following output:

$ gradle -q numbers list
:numbers
Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
Square of 4 = 16
:list
g&g

BUILD SUCCESSFUL

Total time: 2.129 secs
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.86