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

20.3. Ant properties and references

There are several ways to set an Ant property, so that the property can be used by Ant tasks. You can set the property directly on the AntBuilder instance. The Ant properties are also available as a Map which you can change. You can also use the Ant property task. Below are some examples of how to do this.

Example 20.12. Setting an Ant property

build.gradle

ant.buildDir = buildDir ant.properties.buildDir = buildDir ant.properties['buildDir'] = buildDir

ant.property(name: 'buildDir', location: buildDir)

build.xml

<echo>buildDir = ${buildDir}</echo>

Many Ant tasks set properties when they execute. There are several ways to get the value of these properties. You can get the property directly from the AntBuilder instance. The Ant properties are also available as a Map. Below are some examples.

Example 20.13. Getting an Ant property

build.xml

<property name="antProp" value="a property defined in an Ant build"/>

build.gradle

println ant.antProp

println ant.properties.antProp println ant.properties['antProp']

There are several ways to set an Ant reference:

Example 20.14. Setting an Ant reference

build.gradle

ant.path(id: 'classpath', location: 'libs') ant.references.classpath = ant.path(location: 'libs') ant.references['classpath'] = ant.path(location: 'libs')

build.xml

<path refid="classpath"/>

Page 124 of 343

There are several ways to get an Ant reference:

Example 20.15. Getting an Ant reference

build.xml

<path id="antPath" location="libs"/>

build.gradle

println ant.references.antPath println ant.references['antPath']

20.4. API

The Ant integration is provided by AntBuilder.

[8] In Groovy you can execute Strings. To learn more about executing external processes with Groovy have a look in 'Groovy in Action' 9.3.2 or at the Groovy wiki

Page 125 of 343

21

Gradle Plugins

Gradle at its core intentionally provides little useful functionality for real world automation. All of the useful features, such as the ability to compile Java code for example, are added by plugins. Plugins add new tasks (e.g. Compile), domain objects (e.g. SourceSet), conventions (e.g. main Java source is located at src/main/java) as well as extending core objects and objects from other plugins.

In this chapter we will discuss how to use plugins and the terminology and concepts surrounding plugins.

21.1. Applying plugins

Plugins are said to be applied, which is done via the Project.apply() method.

Example 21.1. Applying a plugin

build.gradle

apply plugin: 'java'

Plugins advertise a short name for themselves. In the above case, we are using the short name ‘ja

’ to apply the JavaPlugin.

We could also have used the following syntax:

Example 21.2. Applying a plugin by type

build.gradle

apply plugin: org.gradle.api.plugins.JavaPlugin

Thanks to Gradle's default imports (seeAppendix D, Existing IDE Support and how to cope without it) you could also write:

Page 126 of 343

Example 21.3. Applying a plugin by type

build.gradle

apply plugin: JavaPlugin

The application of plugins is idempotent. That is, a plugin can be applied multiple times. If the plugin has previously been applied, any further applications will have no effect.

A plugin is simply any class that implements the Plugin interface. Gradle provides the core plugins as part of its distribution so simply applying the plugin as above is all you need to do. For 3rd party plugins however, you need to make the plugins available to the build classpath. For more information on how to do this, see Section 52.5, “External dependencies for the build scrip.t”

For more on writing your own plugins, see Chapter 51, Writing Custom Plugins.

21.2. What plugins do

Applying a plugin to the project allows the plugin to extend the project's capabilities. It can d things such as:

Add tasks to the project (e.g. compile, test) Pre-configure added tasks with useful defaults.

Add dependency configurations to the project (see Section 8.3, “Dependency configurations ).

Add new properties and methods to existing type via extensions.

Let's check this out:

Example 21.4. Tasks added by a plugin

build.gradle

apply plugin: 'java'

task show << {

println relativePath(compileJava.destinationDir) println relativePath(processResources.destinationDir)

}

Output of gradle -q show

> gradle -q show build/classes/main build/resources/main

The Java plugin has added a compileJava task and a processResources task to the project

and configured the destinationDir property of both of these tasks.

Page 127 of 343

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