Overview | Package | Class | Deprecated | Index | Help
| Groovy Documentation
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | METHOD | DETAIL: FIELD | METHOD

## org.gradle.api
[Java] Interface Task

    public interface Task
    extends java.lang.Comparable

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 {

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

  ### 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(Iterable). You can add objects of any of the following types as a dependency:

  - A `String` 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.

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

  - 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 additional 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. The properties of this scope are readable and writable.
  - The convention properties added to the task by each Plugin applied to the project. 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.

  #### Dynamic Methods

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

Authors:
  Hans Dockter

Field Summary
`static java.lang.String` | `TASK_ACTION`
`static java.lang.String` | `TASK_DEPENDS_ON`
`static java.lang.String` | `TASK_DESCRIPTION`
`static java.lang.String` | `TASK_GROUP`
`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.
`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#getDidWork()#getDidWork().
`Task` | `disableStandardOutputCapture()` Disables redirection of standard output during task execution.
`Task` | `doFirst(Action action)` Adds the given Action to the beginning of this task's action list.
`Task` | `doFirst(groovy.lang.Closure action)` Adds the given closure to the beginning of this task's action list.
`Task` | `doLast(Action action)` Adds the given Action to the end 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.
`java.util.List` | `getActions()` Returns the sequence of Action objects which will be executed by this task, in the order of execution.
`AntBuilder` | `getAnt()` Returns the `AntBuilder` for this task.
`Convention` | `getConvention()` Returns the Convention object for this task.
`java.util.Set` | `getDependsOn()` Returns the dependencies of this task.
`java.lang.String` | `getDescription()` Returns the description of this task.
`boolean` | `getDidWork()` Checks if the task actually did any work.
`boolean` | `getEnabled()` Returns if this task is enabled or not.
`java.lang.String` | `getGroup()` Returns the task group which this task belongs to.
`TaskInputs` | `getInputs()` Returns the inputs of this task.
`Logger` | `getLogger()` Returns the logger for this task.
`LoggingManager` | `getLogging()` Returns the LoggingManager which can be used to control the logging level and standard output/error capture for this task.
`java.lang.String` | `getName()` Returns the name of this task.
`TaskOutputs` | `getOutputs()` Returns the outputs 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.
`TaskState` | `getState()` Returns the execution state of this task.
`TaskDependency` | `getTaskDependencies()` Returns a TaskDependency which contains all the tasks that this task depends on.
`java.io.File` | `getTemporaryDir()` Returns a directory which this task can use to write temporary files to.
`boolean` | `hasProperty(java.lang.String propertyName)` Determines if this task has the given property.
`Task` | `leftShift(groovy.lang.Closure action)` Adds the given closure to the end of this task's action list.
`void` | `onlyIf(groovy.lang.Closure onlyIfClosure)` Execute the task only if the given closure returns true.
`void` | `onlyIf(Spec onlyIfSpec)` Execute the task only if the given spec is satisfied.
`java.lang.Object` | `property(java.lang.String propertyName)` Returns the value of the given property of this task.
`void` | `setActions(java.util.List actions)` Sets the sequence of Action objects which will be executed by this task.
`void` | `setDependsOn(java.lang.Iterable dependsOnTasks)` Sets the dependencies of this task.
`void` | `setDescription(java.lang.String description)` Sets a description for this task.
`void` | `setDidWork(boolean didWork)` Sets whether the task actually did any work.
`void` | `setEnabled(boolean enabled)` Set the enabled state of a task.
`void` | `setGroup(java.lang.String group)` Sets the task group which this task belongs to.
`void` | `setOnlyIf(groovy.lang.Closure onlyIfClosure)` Execute the task only if the given closure returns true.
`void` | `setOnlyIf(Spec onlyIfSpec)` Execute the task only if the given spec is satisfied.
`void` | `setProperty(java.lang.String name, java.lang.Object value)` Sets a property of this task.

Methods inherited from interface java.lang.Comparable
java.lang.Comparable#compareTo(java.lang.Object)

Methods inherited from class java.lang.Object
java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#notify(), java.lang.Object#notifyAll(), java.lang.Object#toString()

Field Detail

### TASK_ACTION

    public static final java.lang.String TASK_ACTION

### TASK_DEPENDS_ON

    public static final java.lang.String TASK_DEPENDS_ON

### TASK_DESCRIPTION

    public static final java.lang.String TASK_DESCRIPTION

### TASK_GROUP

    public static final java.lang.String TASK_GROUP

### TASK_NAME

    public static final java.lang.String TASK_NAME

### TASK_OVERWRITE

    public static final java.lang.String TASK_OVERWRITE

### TASK_TYPE

    public static final java.lang.String TASK_TYPE

Method Detail

### captureStandardOutput

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

  deprecated:
    Use the LoggingManager returned by getLogging() instead.
  Parameters:
    `level` - The level standard out should be logged to.
  Returns:
    this
  See Also:
    disableStandardOutputCapture()

### configure

    public 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

### deleteAllActions

    public Task deleteAllActions()

  Removes all the actions of this task.

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

  Parameters:
    `paths` - The dependencies to add to this task.
  Returns:
    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()#getDidWork().

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

### disableStandardOutputCapture

    @Deprecated
    public 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)

### doFirst

    public Task doFirst(Action action)

  Adds the given Action 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

    public 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

    public Task doLast(Action action)

  Adds the given Action 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

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

### getActions

    public java.util.List getActions()

  Returns the sequence of Action 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.

### getAnt

    public AntBuilder getAnt()

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

  Returns:
    The `AntBuilder`

### getConvention

    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.

  Returns:
    The convention object. Never returns null.

### getDependsOn

    public java.util.Set getDependsOn()

  Returns the dependencies of this task.

  Returns:
    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 this task.
  Returns:
    the description. May return null.

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

  Returns:
    true if this task did any work

### getEnabled

    public boolean getEnabled()

  Returns if this task is enabled or not.

  See Also:
    setEnabled(boolean)

### getGroup

    public java.lang.String getGroup()

  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.
  Returns:
    The task group for this task. Might be null.

### getInputs

    public TaskInputs getInputs()

  Returns the inputs of this task.

  Returns:
    The inputs. Never returns null.

### getLogger

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

### getLogging

    public LoggingManager getLogging()

  Returns 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.
  Returns:
    the LoggingManager. Never returns null.

### getName

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

### getOutputs

    public TaskOutputs getOutputs()

  Returns the outputs of this task.

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

### getProject

    public Project getProject()

  Returns the Project which this task belongs to.

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

### getState

    public TaskState getState()

  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.
  Returns:
    The execution state of this task. Never returns null.

### getTaskDependencies

    public TaskDependency getTaskDependencies()

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

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

### getTemporaryDir

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

  Returns:
    The directory. Never returns null. The directory will already exist.

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

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

### leftShift

    public Task leftShift(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. You can call this method from your build script using the << left shift operator.

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

### onlyIf

    public void onlyIf(groovy.lang.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() }`

  Parameters:
    `onlyIfClosure` - code to execute to determine if task should be run

### onlyIf

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

  Parameters:
    `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 groovy.lang.MissingPropertyException
  throws:
    MissingPropertyException When the given property is unknown.
  Parameters:
    `propertyName` - The name of the property.
  Returns:
    The value of the property, possibly null.

### setActions

    public void setActions(java.util.List actions)

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

  Parameters:
    `actions` - The actions.

### setDependsOn

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

  Parameters:
    `dependsOnTasks` - The set of task paths.

### setDescription

    public void setDescription(java.lang.String description)

  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.
  Parameters:
    `description` - The description of the task. Might be null.

### setDidWork

    public void setDidWork(boolean didWork)

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

  Parameters:
    `didWork` - indicates if the task did any work

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

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

### setGroup

    public void setGroup(java.lang.String group)

  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.
  Parameters:
    `group` - The task group for this task. Can be null.

### setOnlyIf

    public void setOnlyIf(groovy.lang.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.

  Parameters:
    `onlyIfClosure` - code to execute to determine if task should be run

### setOnlyIf

    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.

  Parameters:
    `onlyIfSpec` - specifies if a task should be run

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

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

Groovy Documentation