# Appendix A. Potential Traps

## A.1. Groovy script variables

For Gradle users it is important to understand how Groovy deals with script variables. Groovy has two types of script variables. One with a local scope and one with a script wide scope.

Example A.1. Variables scope: local and script wide

`scope.groovy`

    String localScope1 = 'localScope1'
    def localScope2 = 'localScope2'
    scriptScope = 'scriptScope'

    println localScope1
    println localScope2
    println scriptScope

    closure = {
        println localScope1
        println localScope2
        println scriptScope
    }

    def method() {
        try {localScope1} catch(MissingPropertyException e) {println 'localScope1NotAvailable' }
        try {localScope2} catch(MissingPropertyException e) {println 'localScope2NotAvailable' }
        println scriptScope
    }

    closure.call()
    method()

Output of `gradle`

    > gradle
    localScope1
    localScope2
    scriptScope
    localScope1
    localScope2
    scriptScope
    localScope1NotAvailable
    localScope2NotAvailable
    scriptScope

Variables which are declared with a type modifier are visible within closures but not visible within methods. This is a heavily discussed behavior in the Groovy community. [35]

## A.2. Configuration and execution phase

It is important to keep in mind that Gradle has a distinct configuration and execution phase (see Chapter 30, The Build Lifecycle).

Example A.2. Distinct configuration and execution phase

`build.gradle`

    classesDir = new File('build/classes')
    classesDir.mkdirs()
    task clean << {
        ant.delete(dir: 'build')
    }
    task compile(dependsOn: 'clean') << {
        if (!classesDir.isDirectory()) {
            println 'The class directory does not exist. I can not operate'
            // do something
        }
        // do something
    }

Output of `gradle -q compile`

    > gradle -q compile
    The class directory does not exist. I can not operate

As the creation of the directory happens during the configuration phase, the `clean` task removes the directory during the execution phase.

[35] One of those discussions can be found here: http://www.nabble.com/script-scoping-question-td16034724.html