Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gradle.pdf
Скачиваний:
9
Добавлен:
24.03.2015
Размер:
1.4 Mб
Скачать

16.4. Declaring variables

There are two kinds of variables that can be declared in a build script: local variables and extra properties.

16.4.1. Local variables

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language.

Example 16.2. Using local variables

build.gradle

def dest = "dest"

task copy(type: Copy) { from "source"

into dest

}

16.4.2. Extra properties

All enhanced objects in Gradle's domain model can hold extra user-defined properties. Thi includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object'sext property.

Example 16.3. Using extra properties

build.gradle

apply plugin: "java"

sourceSets.all { ext.purpose = null }

sourceSets { main {

purpose = "production"

}

test {

purpose = "test"

}

plugin {

ext.purpose = "production"

}

}

task printProductionSourceDirs << {

sourceSets.matching { it.purpose == "production" }.each { println it.java.

}

In this example, a property named purpose is added to all source sets by setting ext.purpose to null (null is a permissible value). Once the property has been added, it can be read and set

Page 82 of 343

like a predefined property. Alternatively, ext. can be used as well.

By requiring special syntax for adding a property, Gradle can fail fast when an attempt is made to set a (predefined or extra) property but the property is misspelled or does not exist. [6] Extra properties can be accessed from anywhere their owning object can be accessed, giving them a wider scope than local variables. Extra properties on a parent project are visible from subprojects.

For further details on extra properties and their API, see ExtraPropertiesExtension.

16.5. Some Groovy basics

Groovy provides plenty of features for creating DSLs, and the Gradle build language takes advantage of these. Understanding how the build language works will help you when you write your build script, and in particular, when you start to write customs plugins and tasks.

16.5.1. Groovy JDK

Groovy adds lots of useful methods to JVM classes. For example, Iterable gets an each method, which iterates over the elements of the Iterable:

Example 16.4. Groovy JDK methods

build.gradle

// Iterable gets an each() method configurations.runtime.each { File f -> println f }

Have a look at http://groovy.codehaus.org/groovy-jdk/ for more details.

16.5.2. Property accessors

Groovy automatically converts a property reference into a call to the appropriate getter or setter method.

Example 16.5. Property accessors

build.gradle

//Using a getter method println project.buildDir

println getProject().getBuildDir()

//Using a setter method project.buildDir = 'target' getProject().setBuildDir('target')

16.5.3. Optional parentheses on method calls

Parentheses are optional for method calls.

Page 83 of 343

Example 16.6. Method call without parentheses

build.gradle

test.systemProperty 'some.prop', 'value' test.systemProperty('some.prop', 'value')

16.5.4. List and map literals

Groovy provides some shortcuts for defining List and Map instances.

Example 16.7. List and map literals

build.gradle

// List literal

test.includes = ['org/gradle/api/**', 'org/gradle/internal/**']

List<String> list = new ArrayList<String>() list.add('org/gradle/api/**') list.add('org/gradle/internal/**') test.includes = list

// Map literal

apply plugin: 'java'

Map<String, String> map = new HashMap<String, String>() map.put('plugin', 'java')

apply(map)

16.5.5. Closures as the last parameter in a method

The Gradle DSL uses closures in many places. You can find out more about closures here. When the last parameter of a method is a closure, you can place the closure after the method call:

Example 16.8. Closure as method parameter

build.gradle

repositories {

println "in a closure"

}

repositories() { println "in a closure" } repositories({ println "in a closure" })

16.5.6. Closure delegate

Each closure has a delegate object, which Groovy uses to look up variable and method references which are not local variables or parameters of the closure. Gradle uses this for configuration closures, where the delegate object is set to the object to be configured.

Page 84 of 343

Example 16.9. Closure delegates

build.gradle

dependencies {

assert delegate == project.dependencies compile('junit:junit:4.8.2') delegate.compile('junit:junit:4.8.2')

}

[6] As of Gradle 1.0 milestone 9, using ext to add extra properties is strongly encouraged but not yet enforced. Therefore, Gradle will not fail when an unknown property is set. However, it will print a warning.

Page 85 of 343

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]