Build scripts are Groovy code

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

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

task numbers << { 
    // To define a range of numbers 
    // we can use the following syntax: 
    // start..end. 
    // The each method executes the code 
    // in the closure for each element 
    // in a collection, like a range. 
    (1..4).each { number -> 
        // def is short for define. 
        // Used to define a variable without 
        // an explicit type of the variable. 
        def squared = number * number 
 
        // Use GString with ${} expression 
        // to get a new string value where 
        // the value references are replaced 
        // with the actual values. 
        println "Square of ${number} = ${squared}" 
    } 
} 
 
task list { 
    doFirst { 
        // Simple notation to define a list of values 
        // in Groovy using square brackets. 
        def list = ['Groovy', 'Gradle'] 
 
        // Groovy makes working with collections 
        // easy and adds utility methods to work 
        // with elements in the collection. 
        // The collect method transform each element 
        // in the original collection with the return 
        // value of the closure. Here all string elements 
        // are turned into lower case values. 
        // The join method joins all elements separated by 
        // the given character. The end result is 
        // groovy&gradle 
        println list.collect { it.toLowerCase() }.join('&') 
    } 
} 

When we run the script, we get the following output:

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