Writing Custom Task Classes - Gradle User Guide Version 2.14.1

# Chapter 38. Writing Custom Task Classes

Gradle supports two types of task. One such type is the simple task, where you define the task with an action closure. We have seen these in Chapter 14, Build Script Basics. For this type of task, the action closure determines the behaviour of the task. This type of task is good for implementing one-off tasks in your build script.

The other type of task is the enhanced task, where the behaviour is built into the task, and the task provides some properties which you can use to configure the behaviour. We have seen these in Chapter 17, More about Tasks. Most Gradle plugins use enhanced tasks. With enhanced tasks, you don't need to implement the task behaviour as you do with simple tasks. You simply declare the task and configure the task using its properties. In this way, enhanced tasks let you reuse a piece of behaviour in many different places, possibly across different builds.

The behaviour and properties of an enhanced task is defined by the task's class. When you declare an enhanced task, you specify the type, or class of the task.

Implementing your own custom task class in Gradle is easy. You can implement a custom task class in pretty much any language you like, provided it ends up compiled to bytecode. In our examples, we are going to use Groovy as the implementation language, but you could use, for example, Java or Scala. In general, using Groovy is the easiest option, because the Gradle API is designed to work well with Groovy.

## 38.1. Packaging a task class

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

Build script

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

`buildSrc` project

  You can put the source for the task class in the ``rootProjectDir`/buildSrc/src/main/groovy` directory. Gradle will take care of compiling and testing the task class and making it available on the classpath of the build script. The task class is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the task class outside the build it is defined in. Using the `buildSrc` project approach separates the task declaration - that is, what the task should do - from the task implementation - that is, how the task does it.

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

Standalone project

  You can create a separate project for your task class. 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 task class in the build script, to keep things simple. Then we will look at creating a standalone project.

## 38.2. Writing a simple task class

To implement a custom task class, you extend `DefaultTask`.

Example 38.1. Defining a custom task

`build.gradle`

    class GreetingTask extends DefaultTask {
    }

This task doesn't do anything useful, so let's add some behaviour. To do so, we add a method to the task and mark it with the `TaskAction` annotation. Gradle will call the method when the task executes. You don't have to use a method to define the behaviour for the task. You could, for instance, call `doFirst()` or `doLast()` with a closure in the task constructor to add behaviour.

Example 38.2. A hello world task

`build.gradle`

    task hello(type: GreetingTask)

    class GreetingTask extends DefaultTask {
        @TaskAction
        def greet() {
            println 'hello from GreetingTask'
        }
    }

Output of `gradle -q hello`

    > gradle -q hello
    hello from GreetingTask

Let's add a property to the task, so we can customize it. Tasks are simply POGOs, and when you declare a task, you can set the properties or call methods on the task object. Here we add a `greeting` property, and set the value when we declare the `greeting` task.

Example 38.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 {
        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

## 38.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 38.4. A build for a custom task

`build.gradle`

    apply plugin: 'groovy'

    dependencies {
        compile gradleApi()
        compile localGroovy()
    }

Note: The code for this example can be found at `samples/customPlugin/plugin` in the ‘-all’ distribution of Gradle.

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

Example 38.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
        }
    }

### 38.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 do this, you use a `buildscript { }` block, as described in Section 41.6, “External dependencies 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 38.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-SNAPSHOT'
        }
    }

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

### 38.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.

Example 38.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)
        }
    }

## 38.4. Incremental tasks

Incremental tasks are an incubating feature.

Since the introduction of the implementation described above (early in the Gradle 1.6 release cycle), discussions within the Gradle community have produced superior ideas for exposing the information about changes to task implementors to what is described below. As such, the API for this feature will almost certainly change in upcoming releases. However, please do experiment with the current implementation and share your experiences with the Gradle community.

The feature incubation process, which is part of the Gradle feature lifecycle (see Appendix C, The Feature Lifecycle), exists for this purpose of ensuring high quality final implementations through incorporation of early user feedback.

With Gradle, it's very simple to implement a task that gets skipped when all of it's inputs and outputs are up to date (see Section 17.9, “Skipping tasks that are up-to-date”). However, there are times when only a few input files have changed since the last execution, and you'd like to avoid reprocessing all of the unchanged inputs. This can be particularly useful for a transformer task, that converts input files to output files on a 1:1 basis.

If you'd like to optimise your build so that only out-of-date inputs are processed, you can do so with an incremental task.

### 38.4.1. Implementing an incremental task

For a task to process inputs incrementally, that task must contain an incremental task action. This is a task action method that contains a single `IncrementalTaskInputs` parameter, which indicates to Gradle that the action will process the changed inputs only.

The incremental task action may supply an `IncrementalTaskInputs.outOfDate(org.gradle.api.Action)` action for processing any input file that is out-of-date, and a `IncrementalTaskInputs.removed(org.gradle.api.Action)` action that executes for any input file that has been removed since the previous execution.

Example 38.8. Defining an incremental task action

`build.gradle`

    class IncrementalReverseTask extends DefaultTask {
        @InputDirectory
        def File inputDir

        @OutputDirectory
        def File outputDir

        @Input
        def inputProperty

        @TaskAction
        void execute(IncrementalTaskInputs inputs) {
            println inputs.incremental ? "CHANGED inputs considered out of date"
                                       : "ALL inputs considered out of date"
            if (!inputs.incremental)
                project.delete(outputDir.listFiles())

            inputs.outOfDate { change ->
                println "out of date: ${change.file.name}"
                def targetFile = new File(outputDir, change.file.name)
                targetFile.text = change.file.text.reverse()
            }

            inputs.removed { change ->
                println "removed: ${change.file.name}"
                def targetFile = new File(outputDir, change.file.name)
                targetFile.delete()
            }
        }
    }

Note: The code for this example can be found at `samples/userguide/tasks/incrementalTask` in the ‘-all’ distribution of Gradle.

If for some reason the task is not run incremental, e.g. by running with --rerun-tasks, only the outOfDate action is executed, even if there where deleted input files. You should consider handling this case at the beginning, as is done in the example above.

For a simple transformer task like this, the task action simply needs to generate output files for any out-of-date inputs, and delete output files for any removed inputs.

A task may only contain a single incremental task action.

### 38.4.2. Which inputs are considered out of date?

When Gradle has history of a previous task execution, and the only changes to the task execution context since that execution are to input files, then Gradle is able to determine which input files need to be reprocessed by the task. In this case, the `IncrementalTaskInputs.outOfDate(org.gradle.api.Action)` action will be executed for any input file that was added or modified, and the `IncrementalTaskInputs.removed(org.gradle.api.Action)` action will be executed for any removed input file.

However, there are many cases where Gradle is unable to determine which input files need to be reprocessed. Examples include:

- There is no history available from a previous execution.
- You are building with a different version of Gradle. Currently, Gradle does not use task history from a different version.
- An `upToDateWhen` criteria added to the task returns `false`.
- An input property has changed since the previous execution.
- One or more output files have changed since the previous execution.

In any of these cases, Gradle will consider all of the input files to be `outOfDate`. The `IncrementalTaskInputs.outOfDate(org.gradle.api.Action)` action will be executed for every input file, and the `IncrementalTaskInputs.removed(org.gradle.api.Action)` action will not be executed at all.

You can check if Gradle was able to determine the incremental changes to input files with `IncrementalTaskInputs.isIncremental()`.

### 38.4.3. An incremental task in action

Given the incremental task implementation above, we can explore the various change scenarios by example. Note that the various mutation tasks ('updateInputs', 'removeInput', etc) are only present for demonstration purposes: these would not normally be part of your build script.

First, consider the `IncrementalReverseTask` executed against a set of inputs for the first time. In this case, all inputs will be considered “out of date”:

Example 38.9. Running the incremental task for the first time

`build.gradle`

    task incrementalReverse(type: IncrementalReverseTask) {
        inputDir = file('inputs')
        outputDir = file("$buildDir/outputs")
        inputProperty = project.properties['taskInputProperty'] ?: "original"
    }

Build layout

    incrementalTask/
      build.gradle
      inputs/
        1.txt
        2.txt
        3.txt

Output of `gradle -q incrementalReverse`

    > gradle -q incrementalReverse
    ALL inputs considered out of date
    out of date: 1.txt
    out of date: 2.txt
    out of date: 3.txt

Naturally when the task is executed again with no changes, then the entire task is up to date and no files are reported to the task action:

Example 38.10. Running the incremental task with unchanged inputs

Output of `gradle -q incrementalReverse`

    > gradle -q incrementalReverse

When an input file is modified in some way or a new input file is added, then re-executing the task results in those files being reported to `IncrementalTaskInputs.outOfDate(org.gradle.api.Action)`:

Example 38.11. Running the incremental task with updated input files

`build.gradle`

    task updateInputs() << {
        file('inputs/1.txt').text = "Changed content for existing file 1."
        file('inputs/4.txt').text = "Content for new file 4."
    }

Output of `gradle -q updateInputs incrementalReverse`

    > gradle -q updateInputs incrementalReverse
    CHANGED inputs considered out of date
    out of date: 1.txt
    out of date: 4.txt

When an existing input file is removed, then re-executing the task results in that file being reported to `IncrementalTaskInputs.removed(org.gradle.api.Action)`:

Example 38.12. Running the incremental task with an input file removed

`build.gradle`

    task removeInput() << {
        file('inputs/3.txt').delete()
    }

Output of `gradle -q removeInput incrementalReverse`

    > gradle -q removeInput incrementalReverse
    CHANGED inputs considered out of date
    removed: 3.txt

When an output file is deleted (or modified), then Gradle is unable to determine which input files are out of date. In this case, all input files are reported to the `IncrementalTaskInputs.outOfDate(org.gradle.api.Action)` action, and no input files are reported to the `IncrementalTaskInputs.removed(org.gradle.api.Action)` action:

Example 38.13. Running the incremental task with an output file removed

`build.gradle`

    task removeOutput() << {
        file("$buildDir/outputs/1.txt").delete()
    }

Output of `gradle -q removeOutput incrementalReverse`

    > gradle -q removeOutput incrementalReverse
    ALL inputs considered out of date
    out of date: 1.txt
    out of date: 2.txt
    out of date: 3.txt

When a task input property is modified, Gradle is unable to determine how this property impacted the task outputs, so all input files are assumed to be out of date. So similar to the changed output file example, all input files are reported to the `IncrementalTaskInputs.outOfDate(org.gradle.api.Action)` action, and no input files are reported to the `IncrementalTaskInputs.removed(org.gradle.api.Action)` action:

Example 38.14. Running the incremental task with an input property changed

Output of `gradle -q -PtaskInputProperty=changed incrementalReverse`

    > gradle -q -PtaskInputProperty=changed incrementalReverse
    ALL inputs considered out of date
    out of date: 1.txt
    out of date: 2.txt
    out of date: 3.txt