A Task
represents a single atomic piece of work for a build, such as compiling classes or generating javadoc.
Each task belongs to a {@link Project}. You can use the various methods on {@link org.gradle.api.tasks.TaskContainer} to create and lookup task instances. For example, {@link org.gradle.api.tasks.TaskContainer#add(String)} creates an empty task with the given name. You can also use the{@code task} keyword in your build file:
task myTask task myTask { configure closure } task myType << { task action } task myTask(type: SomeType) task myTask(type: SomeType) { configure closure }
Each task has a name, which can be used to refer to the task within its owning project, and a fully qualified path, which is unique across all tasks in all projects. The path is the concatenation of the owning project's path and the task's name. Path elements are separated using the {@value org.gradle.api.Project#PATH_SEPARATOR}character.
Task Actions
A Task
is made up of a sequence of {@link Action} objects. When the task is executed, each of theactions is executed in turn, by calling {@link Action#execute}. You can add actions to a task by calling {@link #doFirst(Action)} or {@link #doLast(Action)}.
Groovy closures can also be used to provide a task action. When the action is executed, the closure is called with the task as parameter. You can add action closures to a task by calling {@link #doFirst(groovy.lang.Closure)} or{@link #doLast(groovy.lang.Closure)} or using the left-shift << operator.
There are 2 special exceptions which a task action can throw to abort execution and continue without failing the build. A task action can abort execution of the action and continue to the next action of the task by throwing a {@link org.gradle.api.tasks.StopActionException}. A task action can abort execution of the task and continue to the next task by throwing a {@link org.gradle.api.tasks.StopExecutionException}. Using these exceptions allows you to have precondition actions which skip execution of the task, or part of the task, if not true.
Dependencies
A task may have dependencies on other tasks. Gradle ensures that tasks are executed in dependency order, so that the dependencies of a task are executed before the task is executed. You can add dependencies to a task using {@link #dependsOn(Object)} or {@link #setDependsOn(Iterable)}. You can add objects of any of the following types as a dependency:
- A {@code String} task path or name. A relative path is interpreted relative to the task's {@link Project}. This allows you to refer to tasks in other projects.
- A {@link Task}.
- A closure. The closure may take a {@code Task} as parameter. It may return any of the types listed here. Itsreturn value is recursively converted to tasks. A {@code null} return value is treated as an empty collection.
- A {@link TaskDependency} object.
- A {@link Buildable} object.
- A {@code Collection}, {@code Map} or an array. May contain any of the types listed here. The elements of thecollection/map/array are recursively converted to tasks.
- A {@code Callable}. The {@code call()} method may return any of the types listed here. Its return value isrecursively converted to tasks. A {@code null} return value is treated as an empty collection.
- An {@code Object}. The object's {@code toString()} method is interpreted as a task path or name.
Using a Task in a Build File
Dynamic Properties
A {@code Task} has 3 'scopes' for properties. You can access these properties by name from the build file or bycalling the {@link #property(String)} method.
- The {@code Task} object itself. This includes any property getters and setters declared by the {@code Task}implementation class. The properties of this scope are readable or writable based on the presence of the corresponding getter and setter methods.
- The additional properties of the task. Each task object maintains a map of additional properties. These are arbitrary name -> value pairs which you can use to dynamically add properties to a task object. The properties of this scope are readable and writable.
- The convention properties added to the task by each {@link Plugin} applied to the project. A {@link Plugin} can add properties and methods to a task through the task's {@link Convention} object. The properties ofthis scope may be readable or writable, depending on the convention objects.
Dynamic Methods
A {@link Plugin} may add methods to a {@code Task} using its {@link Convention} object.
@author Hans Dockter