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

declare a task, you can set the properties or call methods on the task object. Here we add a greet property, and set the value when we declare the greeting task.

Example 50.3. A customizable hello world task

build.gradle

//Use the default greeting task hello(type: GreetingTask)

//Customize the greeting

task greeting(type: GreetingTask) { greeting = 'greetings from GreetingTask'

}

class GreetingTask extends DefaultTask {

def String greeting = 'hello from GreetingTask'

@TaskAction def greet() {

println greeting

}

}

Output of gradle -q hello greeting

> gradle -q hello greeting hello from GreetingTask greetings from GreetingTask

50.3. A standalone project

Now we will move our task to a standalone project, so we can publish it and share it with others. This project is simply a Groovy project that produces a JAR containing the task class. Here is a simple build script for the project. It applies the Groovy plugin, and adds the Gradle API as a compile-time dependency.

Example 50.4. A build for a custom task

build.gradle

apply plugin: 'groovy'

dependencies {

compile gradleApi() groovy localGroovy()

}

Note: The code for this example can be found at samples/customPlugin/plugin which is in both the binary and source distributions of Gradle.

We just follow the convention for where the source for the task class should go.

Page 300 of 343

Example 50.5. A custom task

src/main/groovy/org/gradle/GreetingTask.groovy

package org.gradle

import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction

class GreetingTask extends DefaultTask { String greeting = 'hello from GreetingTask'

@TaskAction def greet() {

println greeting

}

}

50.3.1. Using your task class in another project

To use a task class in a build script, you need to add the class to the build script's classpath. To d this, you use a buildscript { } block, as described in Section 52.5, “External dependencie for the build script”. The following example shows how you might do this when the JAR containing the task class has been published to a local repository:

Example 50.6. Using a custom task in another project

build.gradle

buildscript { repositories {

maven {

url uri('../repo')

}

}

dependencies {

classpath group: 'org.gradle', name: 'customPlugin', version: '1.0-SNA

}

}

task greeting(type: org.gradle.GreetingTask) { greeting = 'howdy!'

}

50.3.2. Writing tests for your task class

You can use the ProjectBuilder class to create Project instances to use when you test your

task class.

Page 301 of 343

Example 50.7. Testing a custom task

src/test/groovy/org/gradle/GreetingTaskTest.groovy

class GreetingTaskTest { @Test

public void canAddTaskToProject() {

Project project = ProjectBuilder.builder().build()

def task = project.task('greeting', type: GreetingTask) assertTrue(task instanceof GreetingTask)

}

}

Page 302 of 343

51

Writing Custom Plugins

A Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds. Gradle allows you to implement your own custom plugins, so you can reuse your build logic, and share it with others.

You can implement a custom plugin in any language you like, provided the implementation ends up compiled as bytecode. For the examples here, we are going to use Groovy as the implementation language. You could use Java or Scala instead, if you want.

51.1. Packaging a plugin

There are several places where you can put the source for the plugin.

Build script

You can include the source for the plugin directly in the build script. This has the benefit that the plugin is automatically compiled and included in the classpath of the build script without you having to do anything. However, the plugin is not visible outside the build script, and so you cannot reuse the plugin outside the build script it is defined in.

buildSrc project

You can put the source for the plugin in the rootProjectDir/buildSrc/src/main/gro directory. Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script. The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.

See Chapter 52, Organizing Build Logic for more details about the buildSrc project.

Standalone project

You can create a separate project for your plugin. This project produces and publishes a JAR which you can then use in multiple builds and share with others. Generally, this JAR might include some custom plugins, or bundle several related task classes into a single library. Or some combination of the two.

In our examples, we will start with the plugin in the build script, to keep things simple. Then we will look at creating a standalone project.

Page 303 of 343

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