Task

API Documentation:Task

A Task represents a single atomic piece of work for a build, such as compiling classes or generating javadoc.

Each task belongs to a Project. You can use the various methods on TaskContainer to create and lookup task instances. For example, TaskContainer.create() creates an empty task with the given name. You can also use the 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 : character.

Task Actions

A Task is made up of a sequence of Action objects. When the task is executed, each of the actions is executed in turn, by calling Action.execute(). You can add actions to a task by calling Task.doFirst() or Task.doLast().

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 Task.doFirst() or Task.doLast() 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 StopActionException. A task action can abort execution of the task and continue to the next task by throwing a StopExecutionException. Using these exceptions allows you to have precondition actions which skip execution of the task, or part of the task, if not true.

Task Dependencies and Task Ordering

A task may have dependencies on other tasks or might be scheduled to always run after another task. Gradle ensures that all task dependencies and ordering rules are honored when executing tasks, so that the task is executed after all of its dependencies and any "must run after" tasks have been executed.

Dependencies to a task are controlled using Task.dependsOn() or Task.setDependsOn(), and Task.mustRunAfter(), Task.setMustRunAfter(), Task.shouldRunAfter() and Task.setShouldRunAfter() are used to specify ordering between tasks. You can use objects of any of the following types to specify dependencies and ordering:

  • A String, CharSequence or groovy.lang.GString task path or name. A relative path is interpreted relative to the task's Project. This allows you to refer to tasks in other projects.
  • A Task.
  • A closure. The closure may take a Task as parameter. It may return any of the types listed here. Its return value is recursively converted to tasks. A null return value is treated as an empty collection.
  • A TaskDependency object.
  • A Buildable object.
  • A Iterable, Collection, Map or array. May contain any of the types listed here. The elements of the iterable/collection/map/array are recursively converted to tasks.
  • A Callable. The call() method may return any of the types listed here. Its return value is recursively converted to tasks. A null return value is treated as an empty collection.
  • An Object. The object's toString() method is interpreted as a task path or name. The support for custom Objects has been deprecated and will be removed in the next version of Gradle.

Using a Task in a Build File

Dynamic Properties

A Task has 4 'scopes' for properties. You can access these properties by name from the build file or by calling the Task.property() method. You can change the value of these properties by calling the Task.setProperty() method.

  • The Task object itself. This includes any property getters and setters declared by the Task implementation class. The properties of this scope are readable or writable based on the presence of the corresponding getter and setter methods.
  • The extensions added to the task by plugins. Each extension is available as a read-only property with the same name as the extension.
  • The convention properties added to the task by plugins. A plugin can add properties and methods to a task through the task's Convention object. The properties of this scope may be readable or writable, depending on the convention objects.
  • The extra 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. Once defined, the properties of this scope are readable and writable.

Dynamic Methods

A Plugin may add methods to a Task using its Convention object.

Properties

PropertyDescription
actions

The sequence of Action objects which will be executed by this task, in the order of execution.

ant

The AntBuilder for this task. You can use this in your build file to execute ant tasks.

convention

The Convention object for this task. A Plugin can use the convention object to contribute properties and methods to this task.

dependsOn

The dependencies of this task.

description

The description of this task.

didWork

Checks if the task actually did any work. Even if a Task executes, it may determine that it has nothing to do. For example, a compilation task may determine that source files have not changed since the last time a the task was run.

enabled

Returns if this task is enabled or not.

extensions

The container of extensions.

finalizedBy
Incubating

Returns tasks that finalize this task.

group

The task group which this task belongs to. The task group is used in reports and user interfaces to group related tasks together when presenting a list of tasks to the user.

inputs

The inputs of this task.

logger

The logger for this task. You can use this in your build file to write log messages.

logging

The LoggingManager which can be used to control the logging level and standard output/error capture for this task. By default, System.out is redirected to the Gradle logging system at the QUIET log level, and System.err is redirected at the ERROR log level.

mustRunAfter
Incubating

Returns tasks that this task must run after.

name

The name of this task. The name uniquely identifies the task within its Project.

outputs

The outputs of this task.

path

The path of the task, which is a fully qualified name for the task. The path of a task is the path of its Project plus the name of the task, separated by :.

project

The Project which this task belongs to.

state

The execution state of this task. This provides information about the execution of this task, such as whether it has executed, been skipped, has failed, etc.

taskDependencies

Returns a TaskDependency which contains all the tasks that this task depends on.

temporaryDir

Returns a directory which this task can use to write temporary files to. Each task instance is provided with a separate temporary directory. There are no guarantees that the contents of this directory will be kept beyond the execution of the task.

Methods

MethodDescription
deleteAllActions()

Removes all the actions of this task.

dependsOn(paths)

Adds the given dependencies to this task. See here for a description of the types of objects which can be used as task dependencies.

doFirst(action)

Adds the given closure to the beginning of this task's action list. The closure is passed this task as a parameter when executed.

doFirst(action)

Adds the given Action to the beginning of this task's action list.

doLast(action)

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.

doLast(action)

Adds the given Action to the end of this task's action list.

finalizedBy(paths)
Incubating

Adds the given finalizer tasks for this task.

hasProperty(propertyName)

Determines if this task has the given property. See here for details of the properties which are available for a task.

leftShift(action)

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed. You can call this method from your build script using the << left shift operator.

mustRunAfter(paths)
Incubating

Specifies that this task must run after all of the supplied tasks.

onlyIf(onlyIfClosure)

Execute the task only if the given closure returns true. The closure will be evaluated at task execution time, not during configuration. The closure will be passed a single parameter, this task. If the closure returns false, the task will be skipped.

onlyIf(onlyIfSpec)

Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during configuration. If the Spec is not satisfied, the task will be skipped.

property(propertyName)

Returns the value of the given property of this task. This method locates a property as follows:

setProperty(name, value)

Sets a property of this task. This method searches for a property with the given name in the following locations, and sets the property on the first location where it finds the property.

Script blocks

No script blocks

Property details

List<Action<? super Task>> actions

The sequence of Action objects which will be executed by this task, in the order of execution.

AntBuilder ant (read-only)

The AntBuilder for this task. You can use this in your build file to execute ant tasks.

Convention convention (read-only)

The Convention object for this task. A Plugin can use the convention object to contribute properties and methods to this task.

Set<Object> dependsOn

The dependencies of this task.

String description

The description of this task.

boolean didWork

Checks if the task actually did any work. Even if a Task executes, it may determine that it has nothing to do. For example, a compilation task may determine that source files have not changed since the last time a the task was run.

boolean enabled

Returns if this task is enabled or not.

ExtensionContainer extensions (read-only)

The container of extensions.

TaskDependency finalizedBy

Note: This property is incubating and may change in a future version of Gradle.

Returns tasks that finalize this task.

String group

The task group which this task belongs to. The task group is used in reports and user interfaces to group related tasks together when presenting a list of tasks to the user.

TaskInputs inputs (read-only)

The inputs of this task.

Logger logger (read-only)

The logger for this task. You can use this in your build file to write log messages.

LoggingManager logging (read-only)

The LoggingManager which can be used to control the logging level and standard output/error capture for this task. By default, System.out is redirected to the Gradle logging system at the QUIET log level, and System.err is redirected at the ERROR log level.

TaskDependency mustRunAfter

Note: This property is incubating and may change in a future version of Gradle.

Returns tasks that this task must run after.

String name (read-only)

The name of this task. The name uniquely identifies the task within its Project.

TaskOutputs outputs (read-only)

The outputs of this task.

String path (read-only)

The path of the task, which is a fully qualified name for the task. The path of a task is the path of its Project plus the name of the task, separated by :.

Project project (read-only)

The Project which this task belongs to.

TaskState state (read-only)

The execution state of this task. This provides information about the execution of this task, such as whether it has executed, been skipped, has failed, etc.

TaskDependency taskDependencies (read-only)

Returns a TaskDependency which contains all the tasks that this task depends on.

File temporaryDir (read-only)

Returns a directory which this task can use to write temporary files to. Each task instance is provided with a separate temporary directory. There are no guarantees that the contents of this directory will be kept beyond the execution of the task.

Method details

Task deleteAllActions()

Removes all the actions of this task.

Task dependsOn(Object... paths)

Adds the given dependencies to this task. See here for a description of the types of objects which can be used as task dependencies.

Task doFirst(Closure action)

Adds the given closure to the beginning of this task's action list. The closure is passed this task as a parameter when executed.

Task doFirst(Action<? super Task> action)

Adds the given Action to the beginning of this task's action list.

Task doLast(Closure action)

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.

Task doLast(Action<? super Task> action)

Adds the given Action to the end of this task's action list.

Task finalizedBy(Object... paths)

Note: This method is incubating and may change in a future version of Gradle.

Adds the given finalizer tasks for this task.

task taskY {
    finalizedBy "taskX"
}

See here for a description of the types of objects which can be used to specify a finalizer task.

boolean hasProperty(String propertyName)

Determines if this task has the given property. See here for details of the properties which are available for a task.

Task leftShift(Closure action)

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed. You can call this method from your build script using the << left shift operator.

Task mustRunAfter(Object... paths)

Note: This method is incubating and may change in a future version of Gradle.

Specifies that this task must run after all of the supplied tasks.

task taskY {
    mustRunAfter "taskX"
}

For each supplied task, this action adds a task 'ordering', and does not specify a 'dependency' between the tasks. As such, it is still possible to execute 'taskY' without first executing the 'taskX' in the example.

See here for a description of the types of objects which can be used to specify an ordering relationship.

void onlyIf(Closure onlyIfClosure)

Execute the task only if the given closure returns true. The closure will be evaluated at task execution time, not during configuration. The closure will be passed a single parameter, this task. If the closure returns false, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage:myTask.onlyIf{ dependsOnTaskDidWork() }

void onlyIf(Spec<? super Task> onlyIfSpec)

Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during configuration. If the Spec is not satisfied, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage (from Java):

myTask.onlyIf(new Spec<Task>() {
   boolean isSatisfiedBy(Task task) {
      return task.dependsOnTaskDidWork();
   }
});

Object property(String propertyName)

Returns the value of the given property of this task. This method locates a property as follows:

  1. If this task object has a property with the given name, return the value of the property.
  2. If this task has an extension with the given name, return the extension.
  3. If this task's convention object has a property with the given name, return the value of the property.
  4. If this task has an extra property with the given name, return the value of the property.
  5. If not found, throw MissingPropertyException

void setProperty(String name, Object value)

Sets a property of this task. This method searches for a property with the given name in the following locations, and sets the property on the first location where it finds the property.

  1. The task object itself. For example, the enabled project property.
  2. The task's convention object.
  3. The task's extra properties.

If the property is not found, a MissingPropertyException is thrown.