Groovy Documentation

org.gradle.api
Interface Task


interface Task
extends java.lang.Comparable

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

Each task belongs to a org.gradle.api.Project. You can use the various methods on org.gradle.api.tasks.TaskContainer to create and lookup task instances. For example, org.gradle.api.tasks.TaskContainer#add(String) 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 {

value:
org.gradle.api.Project#PATH_SEPARATOR} character.

Task Actions

A Task is made up of a sequence of org.gradle.api.TaskAction objects. When the task is executed, each of the actions is executed in turn, by calling TaskAction#execute. 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) 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.

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

static java.lang.String TASK_DEPENDS_ON

static java.lang.String TASK_DESCRIPTION

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(Closure configureClosure)

Applies the statements of the closure against this task object.

Task deleteAllActions()

Removes all the actions of this task.

Task dependsOn(java.lang.Object paths)

Adds the given dependencies to this task.

boolean dependsOnTaskDidWork()

Checks if any of the tasks that this task depends on {

Task disableStandardOutputCapture()

Disables redirection of standard output during task execution.

Task doFirst(TaskAction action)

Adds the given {

Task doFirst(Closure action)

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

Task doLast(TaskAction action)

Adds the given {

Task doLast(Closure action)

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

List getActions()

Returns the sequence of { execution.

AntBuilder getAnt()

Returns the AntBuilder for this task.

Convention getConvention()

Returns the {

Set getDependsOn()

Returns the dependencies of this task.

java.lang.String getDescription()

Returns the description of a task.

boolean getDidWork()

Checks if the task actually did any work.

boolean getEnabled()

Returns if this task is enabled or not.

boolean getExecuted()

Returns true if this task has been executed.

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 {

List getSkipProperties()

Returns the list of skip properties.

TaskDependency getTaskDependencies()

Returns a {

boolean hasProperty(java.lang.String propertyName)

Determines if this task has the given property.

Task leftShift(Closure action)

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

void onlyIf(Closure onlyIfClosure)

Execute the task only if the closure returns true.

void onlyIf(Spec onlyIfSpec)

Execute the task only if the spec is satisfied.

java.lang.Object property(java.lang.String propertyName)

Returns the value of the given property of this task.

void setActions(List actions)

Sets the sequence of {

void setDependsOn(Set dependsOnTasks)

Sets the dependencies of this task.

void setDescription(java.lang.String description)

Adds a text to describe what the task does to the user of the build.

void setEnabled(boolean enabled)

Set the enabled state of a task.

void setProperty(java.lang.String name, java.lang.Object value)

Sets a property of this task.

 
Methods inherited from interface java.lang.Comparable
compareTo
 
Methods inherited from class java.lang.Object
hashCode, getClass, equals, toString, wait, wait, wait, notify, notifyAll
 

Field Detail

AUTOSKIP_PROPERTY_PREFIX

static final java.lang.String AUTOSKIP_PROPERTY_PREFIX


TASK_ACTION

static final java.lang.String TASK_ACTION


TASK_DEPENDS_ON

static final java.lang.String TASK_DEPENDS_ON


TASK_DESCRIPTION

static final java.lang.String TASK_DESCRIPTION


TASK_NAME

static final java.lang.String TASK_NAME


TASK_OVERWRITE

static final java.lang.String TASK_OVERWRITE


TASK_TYPE

static final java.lang.String TASK_TYPE


 
Method Detail

captureStandardOutput

public 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 org.gradle.api.logging.StandardOutputLogging.
param:
level The level standard out should be logged to.
return:
this
see:
#disableStandardOutputCapture()


configure

public Task configure(Closure configureClosure)

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

param:
configureClosure The closure to be applied (can be null).
return:
This task


deleteAllActions

public Task deleteAllActions()

Removes all the actions of this task.

return:
the task object this method is applied to


dependsOn

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

param:
paths The dependencies to add to this task.
return:
the task object this method is applied to


dependsOnTaskDidWork

public boolean dependsOnTaskDidWork()

Checks if any of the tasks that this task depends on Task#getDidWork() didWork.

return:
true if any task this task depends on did work.


disableStandardOutputCapture

public Task disableStandardOutputCapture()
Disables redirection of standard output during task execution. By default redirection is enabled.
return:
this
see:
#captureStandardOutput(org.gradle.api.logging.LogLevel)


doFirst

public Task doFirst(TaskAction action)

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

param:
action The action to add
return:
the task object this method is applied to


doFirst

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

param:
action The action closure to execute.
return:
This task.


doLast

public Task doLast(TaskAction action)

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

param:
action The action to add.
return:
the task object this method is applied to


doLast

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

param:
action The action closure to execute.
return:
This task.


getActions

public List getActions()

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

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


getAnt

public AntBuilder getAnt()

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

return:
The AntBuilder


getConvention

public Convention getConvention()

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

return:
The convention object. Never returns null.


getDependsOn

public Set getDependsOn()

Returns the dependencies of this task.

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


getDescription

public java.lang.String getDescription()
Returns the description of a task.
see:
#setDescription(String)


getDidWork

public boolean getDidWork()

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

return:
true if this task did any work


getEnabled

public boolean getEnabled()

Returns if this task is enabled or not.

see:
#setEnabled(boolean)


getExecuted

public boolean getExecuted()

Returns true if this task has been executed.

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


getLogger

public Logger getLogger()

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

return:
The logger. Never returns null.


getName

public java.lang.String getName()

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

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


getPath

public 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 org.gradle.api.Project plus the name of the task, separated by :.

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


getProject

public Project getProject()

Returns the org.gradle.api.Project which this task belongs to.

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


getSkipProperties

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

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


getTaskDependencies

public TaskDependency getTaskDependencies()

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

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


hasProperty

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

param:
propertyName The name of the property to locate.
return:
True if this project has the given property, false otherwise.


leftShift

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

param:
action The action closure to execute.
return:
This task.


onlyIf

public void onlyIf(Closure onlyIfClosure)

Execute the task only if the closure returns true. The closure will be evaluated at the task execution time, not during configuration. The closure will be passed a single parameter, this task. The closure will be coerced to a org.gradle.api.specs.Spec of type org.gradle.api.Task.

If the closure returns false, the task will not execute, but will log a message of 'SKIPPED as onlyIf is false'.

Typical usage:

myTask.onlyIf{ dependsOnTaskDidWork() }
see:
#getDidWork()
param:
onlyIfClosure code to execute to determine if task should be run


onlyIf

public void onlyIf(Spec onlyIfSpec)

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

If the Spec is not satisfied, the task will not execute, but will log a message of 'SKIPPED as onlyIf is false'.

Typical usage (from Java):

myTask.onlyIf(new Spec() {
    boolean isSatisfiedBy(Task task) {
       return task.dependsOnTaskDidWork();
    }
 });
 
see:
#getDidWork()
param:
onlyIfSpec specifies if a task should be run


property

public java.lang.Object property(java.lang.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 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
param:
propertyName The name of the property.
return:
The value of the property, possibly null.
throws:
MissingPropertyException When the given property is unknown.


setActions

public void setActions(List actions)

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

param:
actions The actions.


setDependsOn

public void setDependsOn(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.

param:
dependsOnTasks The set of task paths.


setDescription

public void setDescription(java.lang.String description)
Adds a text to describe what the task does to the user of the build. The description will be displayed when gradle -t is called.
param:
description The description of the task. Might be null.


setEnabled

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

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


setProperty

public void setProperty(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.

param:
name The name of the property
param:
value The value of the property


 

Groovy Documentation