org.gradle.api
Interface Task

All Superinterfaces:
java.lang.Comparable<Task>

public interface Task
extends java.lang.Comparable<Task>

A Task represents a single step of a build, such as compiling classes or generating javadoc.

Each task belongs to a Project. You can use the various methods on Project to create and lookup task instances. For example, Project.createTask(String) creates an empty task with the given name.

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 TaskAction objects. When the task is executed, each of the actions is executed in turn, by calling TaskAction.execute(Task). You can add actions to a task by calling doFirst(TaskAction) or doLast(TaskAction).

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

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 dependsOn(Object[]) or setDependsOn(java.util.Set). You can add objects of any of the following types as a depedency:

Using a Task in a Build File

Dynamic Properties

A Task has 3 'scopes' for properties. You can access these properties by name from the build file or by calling the property(String) method.

Dynamic Methods

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

Author:
Hans Dockter

Field Summary
static java.lang.String AUTOSKIP_PROPERTY_PREFIX
           
static java.lang.String TASK_DEPENDS_ON
           
static java.lang.String TASK_NAME
           
static java.lang.String TASK_OVERWRITE
           
static java.lang.String TASK_TYPE
           
 
Method Summary
 Task captureStandardOutput(LogLevel level)
          Enables redirection of standard output during task execution to the logging system.
 Task configure(groovy.lang.Closure configureClosure)
          Applies the statements of the closure against this task object.
 void defineProperty(java.lang.String name, java.lang.Object value)
          Sets a property of this task.
 Task deleteAllActions()
          Removes all the actions of this task.
 Task dependsOn(java.lang.Object... paths)
          Adds the given dependencies to this task.
 Task disableStandardOutputCapture()
          Disables redirection of standard output during task execution.
 Task doFirst(groovy.lang.Closure action)
          Adds the given closure to the beginning of this task's action list.
 Task doFirst(TaskAction action)
          Adds the given TaskAction to the beginning of this task's action list.
 Task doLast(groovy.lang.Closure action)
          Adds the given closure to the end of this task's action list.
 Task doLast(TaskAction action)
          Adds the given TaskAction to the end of this task's action list.
 java.util.List<TaskAction> getActions()
          Returns the sequence of TaskAction objects which will be executed by this task, in the order of execution.
 groovy.util.AntBuilder getAnt()
          Returns the AntBuilder for this task.
 Convention getConvention()
          Returns the Convention object for this task.
 java.util.Set<java.lang.Object> getDependsOn()
          Returns the dependencies of this task.
 boolean getEnabled()
          Returns if this task is enabled or not.
 boolean getExecuted()
          Returns true if this task has been executed.
 org.slf4j.Logger getLogger()
          Returns the logger for this task.
 java.lang.String getName()
          Returns the name of this task.
 java.lang.String getPath()
          Returns the path of the task, which is a fully qualified name for the task.
 Project getProject()
          Returns the Project which this task belongs to.
 java.util.List<java.lang.String> getSkipProperties()
          Returns the list of skip properties.
 TaskDependency getTaskDependencies()
          Returns a TaskDependency which contains all the tasks that this task depends on.
 boolean hasProperty(java.lang.String propertyName)
          Determines if this task has the given property.
 boolean isDagNeutral()
          Returns whether this task is dag neutral or not.
 java.lang.Object property(java.lang.String propertyName)
          Returns the value of the given property of this task.
 void setActions(java.util.List<TaskAction> actions)
          Sets the sequence of TaskAction objects which will be executed by this task.
 void setDagNeutral(boolean dagNeutral)
          Set's the dag neutral state of the task.
 void setDependsOn(java.util.Set<?> dependsOnTasks)
          Sets the dependencies of this task.
 void setEnabled(boolean enabled)
          Set the enabled state of a task.
 
Methods inherited from interface java.lang.Comparable
compareTo
 

Field Detail

TASK_NAME

static final java.lang.String TASK_NAME
See Also:
Constant Field Values

TASK_TYPE

static final java.lang.String TASK_TYPE
See Also:
Constant Field Values

TASK_DEPENDS_ON

static final java.lang.String TASK_DEPENDS_ON
See Also:
Constant Field Values

TASK_OVERWRITE

static final java.lang.String TASK_OVERWRITE
See Also:
Constant Field Values

AUTOSKIP_PROPERTY_PREFIX

static final java.lang.String AUTOSKIP_PROPERTY_PREFIX
See Also:
Constant Field Values
Method Detail

getName

java.lang.String getName()

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

Returns:
The name of the task. Never returns null.

getProject

Project getProject()

Returns the Project which this task belongs to.

Returns:
The project this task belongs to. Never returns null.

getActions

java.util.List<TaskAction> getActions()

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

Returns:
The task actions in the order they are executed. Returns an empty list if this task has no actions.

setActions

void setActions(java.util.List<TaskAction> actions)

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

Parameters:
actions - The actions.

getTaskDependencies

TaskDependency getTaskDependencies()

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

Returns:
The dependencies of this task. Never returns null.

getDependsOn

java.util.Set<java.lang.Object> getDependsOn()

Returns the dependencies of this task.

Returns:
The dependencies of this task. Returns an empty set if this task has no dependencies.

setDependsOn

void setDependsOn(java.util.Set<?> dependsOnTasks)

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

Parameters:
dependsOnTasks - The set of task paths.

dependsOn

Task dependsOn(java.lang.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.

Parameters:
paths - The dependencies to add to this task.
Returns:
the task object this method is applied to

getExecuted

boolean getExecuted()

Returns true if this task has been executed.

Returns:
true if this task has been executed already, false otherwise.

getPath

java.lang.String getPath()

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

Returns:
the path of the task, which is equal to the path of the project plus the name of the task.

doFirst

Task doFirst(TaskAction action)

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

Parameters:
action - The action to add
Returns:
the task object this method is applied to

doFirst

Task doFirst(groovy.lang.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.

Parameters:
action - The action closure to execute.
Returns:
This task.

doLast

Task doLast(TaskAction action)

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

Parameters:
action - The action to add.
Returns:
the task object this method is applied to

doLast

Task doLast(groovy.lang.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.

Parameters:
action - The action closure to execute.
Returns:
This task.

deleteAllActions

Task deleteAllActions()

Removes all the actions of this task.

Returns:
the task object this method is applied to

getEnabled

boolean getEnabled()

Returns if this task is enabled or not.

See Also:
setEnabled(boolean)

setEnabled

void setEnabled(boolean enabled)

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.

Parameters:
enabled - The enabled state of this task (true or false)

configure

Task configure(groovy.lang.Closure configureClosure)

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

Parameters:
configureClosure - The closure to be applied (can be null).
Returns:
This task

isDagNeutral

boolean isDagNeutral()

Returns whether this task is dag neutral or not.

See Also:
setDagNeutral(boolean)

setDagNeutral

void setDagNeutral(boolean dagNeutral)

Set's the dag neutral state of the task. The concept of dag neutrality is important to improve the performance, when two primary tasks are executed as part of one build (e.g. gradle clean install). Gradle guarantees that executing two tasks at once has the same behavior than executing them one after another. If the execution of the first task changes the state of the task execution graph (e.g. if a task action changes a project property), Gradle needs to rebuild the task execution graph before the execution of the second task. If the first task plus all its dependent tasks declare themselves as dag neutral, Gradle does not rebuild the graph.


getSkipProperties

java.util.List<java.lang.String> getSkipProperties()

Returns the list of skip properties. The returned list can be used to add further skip properties. If a system property with the same key as one of the skip properties is set to a value different than false, none of the task actions are executed. It has the same effect as disabling the task. Therefore when starting gradle it is enough to say gradle -Dskip.test to skip a task. You may, but don't need to assign a value.

Returns:
List of skip properties. Returns empty list when no skip properties are assigned.

getAnt

groovy.util.AntBuilder getAnt()

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

Returns:
The AntBuilder

getLogger

org.slf4j.Logger getLogger()

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

Returns:
The logger. Never returns null.

disableStandardOutputCapture

Task disableStandardOutputCapture()
Disables redirection of standard output during task execution. By default redirection is enabled.

Returns:
this
See Also:
captureStandardOutput(org.gradle.api.logging.LogLevel)

captureStandardOutput

Task captureStandardOutput(LogLevel level)
Enables redirection of standard output during task execution to the logging system. By default redirection is enabled and the task output is redirected to the QUIET level. System.err is always redirected to the ERROR level. An exception is thrown, if this method is called during the execution of the task For more fine-grained control on redirecting standard output see StandardOutputLogging.

Parameters:
level - The level standard out should be logged to.
Returns:
this
See Also:
disableStandardOutputCapture()

property

java.lang.Object property(java.lang.String propertyName)
                          throws groovy.lang.MissingPropertyException
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 additional property with the given name, return the value of the property.
  3. If this task's convention object has a property with the given name, return the value of the property.
  4. If not found, throw MissingPropertyException

Parameters:
propertyName - The name of the property.
Returns:
The value of the property, possibly null.
Throws:
groovy.lang.MissingPropertyException - When the given property is unknown.

hasProperty

boolean hasProperty(java.lang.String propertyName)

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

Parameters:
propertyName - The name of the property to locate.
Returns:
True if this project has the given property, false otherwise.

defineProperty

void defineProperty(java.lang.String name,
                    java.lang.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 additional properties.

If the property is not found in any of these locations, it is added to the project's additional properties.

Parameters:
name - The name of the property
value - The value of the property

getConvention

Convention getConvention()

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

Returns:
The convention object. Never returns null.