// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[potential_traps]]
= Avoiding traps

[[sec:groovy_script_variables]]
== Groovy script variables

For users of the Groovy DSL 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: Variables scope: local and script wide

[source,groovy]
.scope.groovy
----
include::{snippetsPath}/tutorial/groovyScope/groovy/src/main/groovy/scope.groovy[]
----

.Output of `groovy scope.groovy`
----
> groovy scope.groovy
include::{snippetsPath}/tutorial/groovyScope/tests/scope.out[]
----

Variables which are declared with a type modifier are visible within closures but not visible within methods.

[[sec:configuration_and_execution_phase]]
== Configuration and execution phase

It is important to keep in mind that Gradle has a distinct configuration and execution phase (see <<build_lifecycle.adoc#build_lifecycle,Build Lifecycle>>).

.Distinct configuration and execution phase
====
include::sample[dir="snippets/tutorial/mkdirTrap/groovy", files="build.gradle[]"]
include::sample[dir="snippets/tutorial/mkdirTrap/kotlin", files="build.gradle.kts[]"]
====

.Output of **`gradle -q compile`**
----
> gradle -q compile
include::{snippetsPath}/tutorial/mkdirTrap/tests/mkdirTrap.out[]
----

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