// 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.

[[custom_tasks]]
== 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 <<tutorial_using_tasks>>. 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 <<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. Groovy, Java or Kotlin are all good choices as the language to use to implement a task class, as the Gradle API has been designed to work well with these languages. In general, a task implemented using Java or Kotlin, which are statically typed, will perform better than the same task implemented using Groovy.


[[sec:packaging_a_task_class]]
=== 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 <<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.

[[sec:writing_a_simple_task_class]]
=== Writing a simple task class

To implement a custom task class, you extend api:org.gradle.api.DefaultTask[].

++++
<sample id="customTask" dir="userguide/tasks/customTask" title="Defining a custom task">
            <sourcefile file="build.gradle" snippet="define-task"/>
        </sample>
++++

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 api:org.gradle.api.tasks.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.

++++
<sample id="customTaskWithAction" dir="userguide/tasks/customTask" title="A hello world task">
            <sourcefile file="build.gradle" snippet="add-action"/>
            <output args="-q hello"/>
        </sample>
++++

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.

++++
<sample id="customTaskWithProperty" dir="userguide/tasks/customTaskWithProperty" title="A customizable hello world task">
            <sourcefile file="build.gradle" snippet="add-property"/>
            <output args="-q hello greeting"/>
        </sample>
++++


[[sec:custom_tasks_standalone_project]]
=== 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.

++++
<sample id="customTaskStandalone" dir="customPlugin/plugin" title="A build for a custom task" includeLocation="true">
            <sourcefile file="build.gradle" snippet="use-plugin"/>
        </sample>
++++

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

++++
<sample id="customTaskStandalone" dir="customPlugin/plugin" title="A custom task">
            <sourcefile file="src/main/groovy/org/gradle/GreetingTask.groovy"/>
        </sample>
++++


[[sec:using_your_task_class_in_another_project]]
==== 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 <<sec:build_script_external_dependencies>>. The following example shows how you might do this when the JAR containing the task class has been published to a local repository:

++++
<sample id="usingCustomTask" dir="customPlugin/consumer" title="Using a custom task in another project">
                <test args="-p../plugin uploadArchives"/>
                <test args="greeting"/>
                <sourcefile file="build.gradle" snippet="use-task"/>
            </sample>
++++


[[sec:writing_tests_for_your_task_class]]
==== Writing tests for your task class

You can use the api:org.gradle.testfixtures.ProjectBuilder[] class to create api:org.gradle.api.Project[] instances to use when you test your task class.

++++
<sample id="customTaskStandalone" dir="customPlugin/plugin" title="Testing a custom task">
                <sourcefile file="src/test/groovy/org/gradle/GreetingTaskTest.groovy" snippet="test-task"/>
            </sample>
++++


[[incremental_tasks]]
=== Incremental tasks


[NOTE]
====

Incremental tasks are an <<feature_lifecycle,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 <<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 is skipped when all of its inputs and outputs are up to date (see <<sec:up_to_date_checks>>). 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_.


[[sec:implementing_an_incremental_task]]
==== 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 api:org.gradle.api.tasks.incremental.IncrementalTaskInputs[] parameter, which indicates to Gradle that the action will process the changed inputs only.

The incremental task action may supply an api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#outOfDate[] action for processing any input file that is out-of-date, and a api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#removed[] action that executes for any input file that has been removed since the previous execution.

++++
<sample id="taskDefinition" dir="userguide/tasks/incrementalTask" title="Defining an incremental task action" includeLocation="true">
                <sourcefile file="build.gradle" snippet="incremental-task"/>
            </sample>
++++

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 were 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.

[[sec:which_inputs_are_considered_out_of_date]]
==== 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 api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#outOfDate[] action will be executed for any input file that was _added_ or _modified_, and the api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#removed[] 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 api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#outOfDate[] action will be executed for every input file, and the api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#removed[] action will not be executed at all.

You can check if Gradle was able to determine the incremental changes to input files with api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#isIncremental[].

[[sec:an_incremental_task_in_action]]
==== An incremental task in action

Given the incremental task implementation <<taskDefinition,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”:

++++
<sample id="incrementalTaskFirstRun" dir="userguide/tasks/incrementalTask" title="Running the incremental task for the first time">
                <sourcefile file="build.gradle" snippet="reverse"/>
                <layout after="originalInputs">
                    build.gradle
                    inputs/
                    inputs/1.txt
                    inputs/2.txt
                    inputs/3.txt
                </layout>
                <output args="-q incrementalReverse" ignoreLineOrder="true"/>
            </sample>
++++

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:

++++
<sample id="incrementalTaskNoChange" dir="userguide/tasks/incrementalTask" title="Running the incremental task with unchanged inputs">
                <test args="-q originalInputs incrementalReverse"/>
                <output args="-q incrementalReverse"/>
            </sample>
++++

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 api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#outOfDate[]:

++++
<sample id="incrementalTaskUpdatedInputs" dir="userguide/tasks/incrementalTask" title="Running the incremental task with updated input files">
                <sourcefile file="build.gradle" snippet="updated-inputs"/>
                <test args="-q originalInputs incrementalReverse"/>
                <output args="-q updateInputs incrementalReverse" ignoreLineOrder="true"/>
            </sample>
++++

When an existing input file is removed, then re-executing the task results in that file being reported to api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#removed[]:

++++
<sample id="incrementalTaskRemovedInput" dir="userguide/tasks/incrementalTask" title="Running the incremental task with an input file removed">
                <sourcefile file="build.gradle" snippet="removed-input"/>
                <test args="-q originalInputs incrementalReverse"/>
                <output args="-q removeInput incrementalReverse" ignoreLineOrder="true"/>
            </sample>
++++

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 api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#outOfDate[] action, and no input files are reported to the api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#removed[] action:

++++
<sample id="incrementalTaskRemovedOutput" dir="userguide/tasks/incrementalTask" title="Running the incremental task with an output file removed">
                <sourcefile file="build.gradle" snippet="removed-output"/>
                <test args="-q originalInputs incrementalReverse"/>
                <output args="-q removeOutput incrementalReverse" ignoreLineOrder="true"/>
            </sample>
++++

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 api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#outOfDate[] action, and no input files are reported to the api:org.gradle.api.tasks.incremental.IncrementalTaskInputs#removed[] action:

++++
<sample id="incrementalTaskChangedProperty" dir="userguide/tasks/incrementalTask" title="Running the incremental task with an input property changed">
                <test args="-q originalInputs incrementalReverse"/>
                <output args="-q -PtaskInputProperty=changed incrementalReverse" ignoreLineOrder="true"/>
            </sample>
++++

[[sec:storing_incremental_task_state]]
==== Storing incremental state for cached tasks

Using Gradle's `IncrementalTaskInputs` is not the only way to create tasks that only works on changes since the last execution. Tools like the Kotlin compiler provide incrementality as a built-in feature. The way this is typically implemented is that the tool stores some analysis data about the state of the previous execution in some file. If such state files are <<sec:task_output_caching_inputs,relocatable>>, then they can be declared as outputs of the task. This way when the task's results are loaded from cache, the next execution can already use the analysis data loaded from cache, too.

However, if the state files are non-relocatable, then they can't be shared via the build cache. Indeed, when the task is loaded from cache, any such state files must be cleaned up to prevent stale state to confuse the tool during the next execution. Gradle can ensure such stale files are removed if they are declared via `task.localState.register()` or a property is marked with the `@LocalState` annotation.

[[worker_api]]
=== The Worker API


[NOTE]
====

The Worker API is an <<feature_lifecycle,incubating>> feature.

====

As can be seen from the discussion of <<incremental_tasks,incremental tasks>>, the work that a task performs can be viewed as discrete units (i.e. a subset of inputs that are transformed to a certain subset of outputs). Many times, these units of work are highly independent of each other, meaning they can be performed in any order and simply aggregated together to form the overall action of the task. In a single threaded execution, these units of work would execute in sequence, however if we have multiple processors, it would be desirable to perform independent units of work concurrently. By doing so, we can fully utilize the available resources at build time and complete the activity of the task faster.

The Worker API provides a mechanism for doing exactly this. It allows for safe, concurrent execution of multiple items of work during a task action. But the benefits of the Worker API are not confined to parallelizing the work of a task. You can also configure a desired level of isolation such that work can be executed in an isolated classloader or even in an isolated process. Furthermore, the benefits extend beyond even the execution of a single task. Using the Worker API, Gradle can begin to execute tasks in parallel by default. In other words, once a task has submitted its work to be executed asynchronously, and has exited the task action, Gradle can then begin the execution of other independent tasks in parallel, even if those tasks are in the same project.


[[using-the-worker-api]]
==== Using the Worker API

In order to submit work to the Worker API, two things must be provided: an implementation of the unit of work, and a configuration for the unit of work. The implementation is simply a class that extends `java.lang.Runnable`. This class should have a constructor that is annotated with `javax.inject.Inject` and accepts parameters that configure the class for a single unit of work. When a unit of work is submitted to the api:org.gradle.workers.WorkerExecutor[], an instance of this class will be created and the parameters configured for the unit of work will be passed to the constructor.

++++
<sample id="unitOfWork" dir="workerApi/noIsolation" title="Creating a unit of work implementation">
                <sourcefile file="build.gradle" snippet="unit-of-work"/>
            </sample>
++++

The configuration of the worker is represented by a api:org.gradle.workers.WorkerConfiguration[] and is set by configuring an instance of this object at the time of submission. However, in order to submit the unit of work, it is necessary to first acquire the api:org.gradle.workers.WorkerExecutor[]. To do this, a constructor should be provided that is annotated with `javax.inject.Inject` and accepts a api:org.gradle.workers.WorkerExecutor[] parameter. Gradle will inject the instance of api:org.gradle.workers.WorkerExecutor[] at runtime when the task is created.

++++
<sample id="workSubmission" dir="workerApi/noIsolation" title="Submitting a unit of work for execution">
                <sourcefile file="build.gradle" snippet="task-implementation"/>
            </sample>
++++

Note that one element of the api:org.gradle.workers.WorkerConfiguration[] is the `params` property. These are the parameters passed to the constructor of the unit of work implementation for each item of work submitted. Any parameters provided to the unit of work _must_ be `java.io.Serializable`.

Once all of the work for a task action has been submitted, it is safe to exit the task action. The work will be executed asynchronously and in parallel (up to the setting of `max-workers`). Of course, any tasks that are dependent on this task (and any subsequent task actions of this task) will not begin executing until all of the asynchronous work completes. However, other independent tasks that have no relationship to this task can begin executing immediately.

If any failures occur while executing the asynchronous work, the task will fail and a api:org.gradle.workers.WorkerExecutionException[] will be thrown detailing the failure for each failed work item. This will be treated like any failure during task execution and will prevent any dependent tasks from executing.

In some cases, however, it might be desirable to wait for work to complete before exiting the task action. This is possible using the api:org.gradle.workers.WorkerExecutor#await[] method. As in the case of allowing the work to complete asynchronously, any failures that occur while executing an item of work will be surfaced as a api:org.gradle.workers.WorkerExecutionException[] thrown from the api:org.gradle.workers.WorkerExecutor#await[] method.

[NOTE]
====

Note that Gradle will only begin running other independent tasks in parallel when a task has exited a task action and returned control of execution to Gradle. When api:org.gradle.workers.WorkerExecutor#await[] is used, execution does not leave the task action. This means that Gradle will not allow other tasks to begin executing and will wait for the task action to complete before doing so.

====

++++
<sample id="waitForCompletion" dir="workerApi/waitForCompletion" title="Waiting for asynchronous work to complete">
                <sourcefile file="build.gradle" snippet="wait-for-completion"/>
            </sample>
++++


[[isolation-modes]]
==== Isolation Modes

Gradle provides three isolation modes that can be configured on a unit of work and are specified using the api:org.gradle.workers.IsolationMode[] enum:

IsolationMode.NONE::
This states that the work should be run in a thread with a minimum of isolation. For instance, it will share the same classloader that the task is loaded from. This is the fastest level of isolation.
IsolationMode.CLASSLOADER::
This states that the work should be run in a thread with an isolated classloader. The classloader will have the classpath from the classloader that the unit of work implementation class was loaded from as well as any additional classpath entries added through api:org.gradle.workers.WorkerConfiguration#classpath[].
IsolationMode.PROCESS::
This states that the work should be run with a maximum level of isolation by executing the work in a separate process. The classloader of the process will use the classpath from the classloader that the unit of work was loaded from as well as any additional classpath entries added through api:org.gradle.workers.WorkerConfiguration#classpath[]. Furthermore, the process will be a _Worker Daemon_ which will stay alive and can be reused for future work items that may have the same requirements. This process can be configured with different settings than the Gradle JVM using api:org.gradle.workers.WorkerConfiguration#forkOptions[].



[[worker-daemons]]
==== Worker Daemons

When using `IsolationMode.PROCESS`, gradle will start a long-lived _Worker Daemon_ process that can be reused for future work items.

++++
<sample id="workerDaemon" dir="workerApi/workerDaemon" title="Submitting an item of work to run in a worker daemon">
                <sourcefile file="build.gradle" snippet="worker-daemon"/>
            </sample>
++++

When a unit of work for a Worker Daemon is submitted, Gradle will first look to see if a compatible, idle daemon already exists. If so, it will send the unit of work to the idle daemon, marking it as busy. If not, it will start a new daemon. When evaluating compatibility, Gradle looks at a number of criteria, all of which can be controlled through api:org.gradle.workers.WorkerConfiguration#forkOptions[].

executable::
A daemon is considered compatible only if it uses the same java executable.
classpath::
A daemon is considered compatible if its classpath contains all of the classpath entries requested. Note that a daemon is considered compatible if it has more classpath entries in addition to those requested.
heap settings::
A daemon is considered compatible if it has at least the same heap size settings as requested. In other words, a daemon that has higher heap settings than requested would be considered compatible.
jvm arguments::
A daemon is considered compatible if it has set all of the jvm arguments requested. Note that a daemon is considered compatible if it has additional jvm arguments beyond those requested (except for arguments treated specially such as heap settings, assertions, debug, etc).
system properties::
A daemon is considered compatible if it has set all of the system properties requested with the same values. Note that a daemon is considered compatible if it has additional system properties beyond those requested.
environment variables::
A daemon is considered compatible if it has set all of the environment variables requested with the same values. Note that a daemon is considered compatible if it has more environment variables in addition to those requested.
bootstrap classpath::
A daemon is considered compatible if it contains all of the bootstrap classpath entries requested. Note that a daemon is considered compatible if it has more bootstrap classpath entries in addition to those requested.
debug::
A daemon is considered compatible only if debug is set to the same value as requested (true or false).
enable assertions::
A daemon is considered compatible only if enable assertions is set to the same value as requested (true or false).
default character encoding::
A daemon is considered compatible only if the default character encoding is set to the same value as requested.


Worker daemons will remain running until either the build daemon that started them is stopped, or system memory becomes scarce. When available system memory is low, Gradle will begin stopping worker daemons in an attempt to minimize memory consumption.

[[sec:reusing_task_logic]]
=== Re-using logic between task classes

There are different ways to re-use logic between task classes.
The easiest case is when you can extract the logic you want to share in a separate method or class and then use the extracted piece of code in your tasks.
For example, the api:org.gradle.api.tasks.Copy[] task re-uses the logic of the api:org.gradle.api.Project#copy(org.gradle.api.Action)[] method.
Another option is to add a task dependency on the task which outputs you want to re-use.
Other options include using <<sec:task_rules,task rules>> or the <<worker_api,worker API>>.
