|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | METHOD | DETAIL: FIELD | METHOD |
org.gradle.api.Taskorg.gradle.api.plugins.ExtensionAware
public interface Task extends Comparable, 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 TaskContainer to create and lookup task instances. For example, TaskContainer.add 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 {
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#execute. You can add actions to a task by calling doFirst(Action) 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) 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.
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:
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.
A Plugin may add methods to a Task using its Convention object.
Nested Class Summary | |
---|---|
static class |
Task.Namer
|
Field Summary | |
---|---|
static String |
TASK_ACTION
|
static String |
TASK_DEPENDS_ON
|
static String |
TASK_DESCRIPTION
|
static String |
TASK_GROUP
|
static String |
TASK_NAME
|
static String |
TASK_OVERWRITE
|
static String |
TASK_TYPE
|
Method Summary | |
---|---|
Task
|
configure(Closure configureClosure)
|
Task
|
deleteAllActions()
|
Task
|
dependsOn(Object... paths)
|
boolean
|
dependsOnTaskDidWork()
|
Task
|
doFirst(Action action)
|
Task
|
doFirst(Closure action)
|
Task
|
doLast(Action action)
|
Task
|
doLast(Closure action)
|
Task
|
finalizedBy(Object... paths)
|
List
|
getActions()
|
AntBuilder
|
getAnt()
|
Convention
|
getConvention()
|
Set
|
getDependsOn()
|
String
|
getDescription()
Returns the description of this task. |
boolean
|
getDidWork()
|
boolean
|
getEnabled()
|
TaskDependency
|
getFinalizedBy()
|
String
|
getGroup()
Returns the task group which this task belongs to. |
TaskInputs
|
getInputs()
|
Logger
|
getLogger()
|
LoggingManager
|
getLogging()
Returns the LoggingManager which can be used to control the logging level and standard output/error capture for this task. |
TaskDependency
|
getMustRunAfter()
|
String
|
getName()
|
TaskOutputs
|
getOutputs()
|
String
|
getPath()
|
Project
|
getProject()
|
TaskDependency
|
getShouldRunAfter()
|
TaskState
|
getState()
Returns the execution state of this task. |
TaskDependency
|
getTaskDependencies()
|
File
|
getTemporaryDir()
|
boolean
|
hasProperty(String propertyName)
|
Task
|
leftShift(Closure action)
|
Task
|
mustRunAfter(Object... paths)
|
void
|
onlyIf(Closure onlyIfClosure)
|
void
|
onlyIf(Spec onlyIfSpec)
|
Object
|
property(String propertyName)
|
void
|
setActions(List actions)
|
void
|
setDependsOn(Iterable dependsOnTasks)
|
void
|
setDescription(String description)
Sets a description for this task. |
void
|
setDidWork(boolean didWork)
Sets whether the task actually did any work. |
void
|
setEnabled(boolean enabled)
|
void
|
setFinalizedBy(Iterable finalizedBy)
|
void
|
setGroup(String group)
Sets the task group which this task belongs to. |
void
|
setMustRunAfter(Iterable mustRunAfter)
|
void
|
setOnlyIf(Closure onlyIfClosure)
|
void
|
setOnlyIf(Spec onlyIfSpec)
|
void
|
setProperty(String name, Object value)
|
void
|
setShouldRunAfter(Iterable shouldRunAfter)
|
TaskDependency
|
shouldRunAfter(Object... paths)
|
Methods inherited from interface Comparable | |
---|---|
compareTo |
Methods inherited from interface ExtensionAware | |
---|---|
getExtensions |
Field Detail |
---|
public static final String TASK_ACTION
public static final String TASK_DEPENDS_ON
public static final String TASK_DESCRIPTION
public static final String TASK_GROUP
public static final String TASK_NAME
public static final String TASK_OVERWRITE
public static final String TASK_TYPE
Method Detail |
---|
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.
configureClosure
- The closure to be applied (can be null).
public Task deleteAllActions()
Removes all the actions of this task.
public 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.
paths
- The dependencies to add to this task. The path can be defined by:
public boolean dependsOnTaskDidWork()
Checks if any of the tasks that this task depends on Task#getDidWork()#getDidWork().
public Task doFirst(Action action)
Adds the given Action to the beginning of this task's action list.
action
- The action to add
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.
action
- The action closure to execute.
public Task doLast(Action action)
Adds the given Action to the end of this task's action list.
action
- The action to add.
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.
action
- The action closure to execute.
@Incubating public Task finalizedBy(Object... paths)
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.
paths
- The tasks that finalize this task.
public List getActions()
Returns the sequence of Action objects which will be executed by this task, in the order of execution.
public AntBuilder getAnt()
Returns the AntBuilder
for this task. You can use this in your build file to execute ant
tasks.
AntBuilder
public Convention getConvention()
Returns the Convention object for this task. A Plugin can use the convention object to contribute properties and methods to this task.
public Set getDependsOn()
Returns the dependencies of this task.
public String getDescription()
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.
public boolean getEnabled()
Returns if this task is enabled or not.
@Incubating public TaskDependency getFinalizedBy()
Returns tasks that finalize this task.
public String getGroup()
public TaskInputs getInputs()
Returns the inputs of this task.
public Logger getLogger()
Returns the logger for this task. You can use this in your build file to write log messages.
public LoggingManager getLogging()
@Incubating public TaskDependency getMustRunAfter()
Returns tasks that this task must run after.
public String getName()
Returns the name of this task. The name uniquely identifies the task within its Project.
public TaskOutputs getOutputs()
Returns the outputs of this task.
public 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 :
.
public Project getProject()
Returns the Project which this task belongs to.
@Incubating public TaskDependency getShouldRunAfter()
Returns tasks that this task should run after.
public TaskState getState()
public TaskDependency getTaskDependencies()
Returns a TaskDependency which contains all the tasks that this task depends on.
public File getTemporaryDir()
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.
public 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.
propertyName
- The name of the property to locate.
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.
action
- The action closure to execute.
@Incubating public Task mustRunAfter(Object... paths)
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.
paths
- The tasks this task must run after.
public 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() }
onlyIfClosure
- code to execute to determine if task should be run
public void onlyIf(Spec 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(); } });
onlyIfSpec
- specifies if a task should be run
public Object property(String propertyName)
Returns the value of the given property of this task. This method locates a property as follows:
propertyName
- The name of the property.
public void setActions(List actions)
Sets the sequence of Action objects which will be executed by this task.
actions
- The actions.
public void setDependsOn(Iterable dependsOnTasks)
Sets the dependencies of this task. See here for a description of the types of objects which can be used as task dependencies.
dependsOnTasks
- The set of task paths.
public void setDescription(String description)
gradle tasks
is called.
description
- The description of the task. Might be null.
public void setDidWork(boolean didWork)
This is useful when combined with onlyIf { dependsOnTaskDidWork() }.
didWork
- indicates if the task did any work
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.
enabled
- The enabled state of this task (true or false)
@Incubating public void setFinalizedBy(Iterable finalizedBy)
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.
finalizedBy
- The tasks that finalize this task.
public void setGroup(String group)
group
- The task group for this task. Can be null.
@Incubating public void setMustRunAfter(Iterable mustRunAfter)
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.
mustRunAfter
- The set of task paths this task must run after.
public void setOnlyIf(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.
The given predicate replaces all such predicates for this task.
onlyIfClosure
- code to execute to determine if task should be run
public void setOnlyIf(Spec 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.
The given predicate replaces all such predicates for this task.
onlyIfSpec
- specifies if a task should be run
public 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.
enabled
project property.If the property is not found in any of these locations, it is added to the project's additional properties.
name
- The name of the propertyvalue
- The value of the property
@Incubating public void setShouldRunAfter(Iterable shouldRunAfter)
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
- The set of task paths this task should run after.
@Incubating public TaskDependency shouldRunAfter(Object... paths)
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.
paths
- The tasks this task should run after.
Gradle API 1.12