# Appendix B. Potential Traps

## B.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 B.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. [27]

## B.2. Configuration and execution phase

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

Example B.2. Distinct configuration and execution phase

`build.gradle`

    def classesDir = file('build/classes')
    classesDir.mkdirs()
    task clean(type: Delete) {
        delete '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.

[27] One of those discussions can be found here: http://groovy.329449.n5.nabble.com/script-scoping-question-td355887.html