api / org.gradle.api / Task

Task

interface Task : Comparable<Task>, ExtensionAware

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 to create and lookup task instances. For example, 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 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 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 or #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 #doFirst(groovy.lang.Closure) or #doLast(groovy.lang.Closure).

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 org.gradle.api.tasks.StopActionException. A task action can abort execution of the task and continue to the next task by throwing a 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.

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 #dependsOn(Object...) or #setDependsOn(Iterable), and #mustRunAfter(Object...), #setMustRunAfter(Iterable), #shouldRunAfter(Object...) and #setShouldRunAfter(Iterable) are used to specify ordering between tasks. You can use objects of any of the following types to specify dependencies and ordering:

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 #property(String) method. You can change the value of these properties by calling the #setProperty(String, Object) method.

Dynamic Methods

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

Parallel Execution

By default, tasks are not executed in parallel unless a task is waiting on asynchronous work and another task (which is not dependent) is ready to execute. Parallel execution can be enabled by the --parallel flag when the build is initiated. In parallel mode, the tasks of different projects (i.e. in a multi project build) are able to be executed in parallel.

Types

Namer

open class Namer : Namer<Task>

A org.gradle.api.Namer namer for tasks that returns #getName().

Properties

TASK_ACTION

static val TASK_ACTION: String

TASK_CONSTRUCTOR_ARGS

static val TASK_CONSTRUCTOR_ARGS: String

Constructor arguments for the Task

TASK_DEPENDS_ON

static val TASK_DEPENDS_ON: String

TASK_DESCRIPTION

static val TASK_DESCRIPTION: String

TASK_GROUP

static val TASK_GROUP: String

TASK_NAME

static val TASK_NAME: String

TASK_OVERWRITE

static val TASK_OVERWRITE: String

TASK_TYPE

static val TASK_TYPE: String

Functions

configure

abstract fun configure(configureClosure: Closure<Any>): Task

Applies the statements of the closure against this task object. The delegate object for the closure is set to this task.

deleteAllActions

abstract fun deleteAllActions(): Task

Removes all the actions of this task.

dependsOn

abstract fun dependsOn(vararg paths: Any): Task

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

dependsOnTaskDidWork

abstract fun dependsOnTaskDidWork(): Boolean

Checks if any of the tasks that this task depends on didWork.

doFirst

abstract fun doFirst(action: Action<in Task>): Task
abstract fun doFirst(actionName: String, action: Action<in Task>): Task

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

abstract fun doFirst(action: Closure<Any>): Task

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

doLast

abstract fun doLast(action: Action<in Task>): Task
abstract fun doLast(actionName: String, action: Action<in Task>): Task

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

abstract fun doLast(action: Closure<Any>): Task

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

finalizedBy

abstract fun finalizedBy(vararg paths: Any): Task

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.

getActions

abstract fun getActions(): MutableList<Action<in Task>>

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

getAnt

abstract fun getAnt(): AntBuilder

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

getConvention

abstract fun getConvention(): Convention

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

getDependsOn

abstract fun getDependsOn(): MutableSet<Any>

Returns the dependencies of this task.

getDescription

abstract fun getDescription(): String

Returns the description of this task.

getDestroyables

abstract fun getDestroyables(): TaskDestroyables

Returns the destroyables of this task.

getDidWork

abstract fun getDidWork(): Boolean

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.

getEnabled

abstract fun getEnabled(): Boolean

Returns if this task is enabled or not.

getFinalizedBy

abstract fun getFinalizedBy(): TaskDependency

Returns tasks that finalize this task.

getGroup

abstract fun getGroup(): String

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

getInputs

abstract fun getInputs(): TaskInputs

Returns the inputs of this task.

getLocalState

abstract fun getLocalState(): TaskLocalState

Returns the local state of this task.

getLogger

abstract fun getLogger(): Logger

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

getLogging

abstract fun getLogging(): LoggingManager

Returns the org.gradle.api.logging.LoggingManager which can be used to receive logging and to control the 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.

getMustRunAfter

abstract fun getMustRunAfter(): TaskDependency

Returns tasks that this task must run after.

getName

abstract fun getName(): String

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

getOutputs

abstract fun getOutputs(): TaskOutputs

Returns the outputs of this task.

getPath

abstract fun getPath(): String

Returns 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 :.

getProject

abstract fun getProject(): Project

Returns the Project which this task belongs to.

getShouldRunAfter

abstract fun getShouldRunAfter(): TaskDependency

Returns tasks that this task should run after.

getState

abstract fun getState(): TaskState

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

getTaskDependencies

abstract fun getTaskDependencies(): TaskDependency

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

getTemporaryDir

abstract fun getTemporaryDir(): File

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.

hasProperty

abstract fun hasProperty(propertyName: String): Boolean

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

leftShift

abstract fun leftShift(action: Closure<Any>): Task

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

abstract fun mustRunAfter(vararg paths: Any): Task

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.

onlyIf

abstract fun onlyIf(onlyIfClosure: Closure<Any>): Unit

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() }

abstract fun onlyIf(onlyIfSpec: Spec<in Task>): Unit

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(); } }); 

property

abstract fun property(propertyName: String): Any

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

setActions

abstract fun setActions(actions: MutableList<Action<in Task>>): Unit

Sets the sequence of Action objects which will be executed by this task.

setDependsOn

abstract fun setDependsOn(dependsOnTasks: MutableIterable<*>): Unit

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

setDescription

abstract fun setDescription(description: String): Unit

Sets a description for this task. This should describe what the task does to the user of the build. The description will be displayed when gradle tasks is called.

setDidWork

abstract fun setDidWork(didWork: Boolean): Unit

Sets whether the task actually did any work. Most built-in tasks will set this automatically, but it may be useful to manually indicate this for custom user tasks.

This is useful when combined with onlyIf { dependsOnTaskDidWork() }.

setEnabled

abstract fun setEnabled(enabled: Boolean): Unit

Set the enabled state of a task. If a task is disabled none of the its actions are executed. Note that disabling a task does not prevent the execution of the tasks which this task depends on.

setFinalizedBy

abstract fun setFinalizedBy(finalizedBy: MutableIterable<*>): Unit

Specifies the set of finalizer tasks for this task.

 task taskY { finalizedBy = ["taskX1", "taskX2"] } 

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

setGroup

abstract fun setGroup(group: String): Unit

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

setMustRunAfter

abstract fun setMustRunAfter(mustRunAfter: MutableIterable<*>): Unit

Specifies the set of tasks that this task must run after.

 task taskY { mustRunAfter = ["taskX1", "taskX2"] } 

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.

setOnlyIf

abstract fun setOnlyIf(onlyIfClosure: Closure<Any>): Unit

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.

The given predicate replaces all such predicates for this task.

abstract fun setOnlyIf(onlyIfSpec: Spec<in Task>): Unit

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.

The given predicate replaces all such predicates for this task.

setProperty

abstract fun setProperty(name: String, value: Any): Unit

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 groovy.lang.MissingPropertyException is thrown.

setShouldRunAfter

abstract fun setShouldRunAfter(shouldRunAfter: MutableIterable<*>): Unit

Specifies the set of tasks that this task should run after.

 task taskY { shouldRunAfter = ["taskX1", "taskX2"] } 

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.

shouldRunAfter

abstract fun shouldRunAfter(vararg paths: Any): TaskDependency

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

 task taskY { shouldRunAfter "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.

Inherited Functions

getExtensions

abstract fun getExtensions(): ExtensionContainer

The container of extensions.

Inheritors

ObjectFilesToBinary

interface ObjectFilesToBinary : Task

A task that combines a set of object files into a single binary.