Task
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, create 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 execute. You can add actions to a task by calling doFirst or doLast.
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 or doLast.
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 or setDependsOn, and mustRunAfter, setMustRunAfter, shouldRunAfter and setShouldRunAfter are used to specify ordering between tasks. You can use objects of any of the following types to specify dependencies and ordering:
- A
String
,CharSequence
orgroovy.lang.GString
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. These task references will not cause task creation. - A Task.
- A TaskDependency object.
- A org.gradle.api.tasks.TaskReference object.
- A Buildable object.
- A org.gradle.api.file.RegularFileProperty or org.gradle.api.file.DirectoryProperty.
- A Provider object. May contain any of the types listed here.
- 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
. Thecall()
method may return any of the types listed here. Its return value is recursively converted to tasks. Anull
return value is treated as an empty collection. - A Groovy
Closure
or Kotlin function. The closure may take aTask
as parameter. The closure or function may return any of the types listed here. Its return value is recursively converted to tasks. Anull
return value is treated as an empty collection. - Anything else is treated as an error.
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 method. You can change the value of these properties by calling the setProperty method.
- The
Task
object itself. This includes any property getters and setters declared by theTask
implementation class. The properties of this scope are readable or writable based on the presence of the corresponding getter and setter methods. - The extensions added to the task by plugins. Each extension is available as a read-only property with the same name as the extension.
- The convention properties added to the task by plugins. A plugin can add properties and methods to a task through the task's org.gradle.api.plugins.Convention object. The properties of this scope may be readable or writable, depending on the convention objects.
- The extra 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. Once defined, the properties of this scope are readable and writable.
A Plugin may add methods to a Task
using its org.gradle.api.plugins.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.
Inheritors
Types
Properties
Functions
AntBuilder
for this task.Returns the extension of the specified type.
Returns the extension of the specified extensionType.