Gradle Task
External
- Task API: org.gradle.api.Task
- Task DSL: https://docs.gradle.org/current/dsl/org.gradle.api.Task.html
Internal
Overview
A task represents a well defined piece of work that is executing during a build, such as compiling Java classes or creating a distribution file. The goal of a Gradle execution, also known as a Gradle build, is to execute a set of tasks, in sequence.
A tasks belongs to a project. Each project comes with a set of pre-defined, built-in tasks. Each declared plugin may add its own tasks. New simple tasks can be defined in-line in build.gradle or in script plugins. Tasks whose implementations are available on the build's classpath can be declared and configured in build.gradle, as enhanced tasks, and thus made available for use during the build. Complex tasks can even be fully defined in-line or in script plugins, by providing the task code in-line in the script, though this is not a recommended practice, as it does not encourage modularization, encapsulation and sharing.
Any task instance implements org.gradle.api.Task interface. Most tasks extend directly or indirectly org.gradle.api.DefaultTask.
Declaring, configuring and defining tasks is done via corresponding Project API methods:
Project.task(String name) Project.task(String name, Closure configureClosure) Project.task(String name, Action<? super Task> configureAction) Project.task(Map<String, ?> args, String name) Project.task(Map<String, ?> args, String name, Closure configureClosure)
A Gradle build executes the task specified on command line, including all explicit and implicit dependencies of the task.
./gradlew build
Task Structure
Task Name and Path
Each task has a name, which is unique within the context of its owning project. The name must not contain dashes. Since different projects may contain tasks with the same name, a task is uniquely identified by its path in the context of a multi-project build. The path is the concatenation of the task owning project name and the task name, separated by the ":" character.
Task Action
Each task contains a list of actions, which are executed in order in which they were added to the list. Actions can be added to the task by invoking the Task API methods doFirst(Action|Closure), which adds the action at the head of the list, and doLast(Action|Closure), which adds the action at the tail of the list. Actions can also be added in-line with corresponding DSL keywords, as shown in
Task Action Closure
Task action closures can be used to define custom simple tasks, in-line in build.gradle or in a script plugin.
@TaskAction
Task Lifecycle
The tasks are instantiated and configured. The task's actions are executed in the execution phase.
Declaring a Task
With DSL
Programmatically
Task Configuration Closure
Task Dependencies and Ordering
Explicit Dependencies
Implicit Dependencies
Defining Custom Tasks
Simple Task
A simple task is an implementation of org.gradle.api.DefaultTask that can be declared in-line in build.gradle or in script plugin using DSL keywords ("task", "doFirst", "doLast", etc.) corresponding to Task API methods:
task customSimpleTask {
doFirst {
}
doLast {
}
}
Enhanced Task
Built-in Tasks
Each build exposes a set of built-in tasks, that can be used right away without any additional configuration or loading any plugin:
- init
- wrapper
- buildEnvironment
- components
- dependencies
- dependencyInsight
- dependentComponents
- help
- model
- projects
- properties
- tasks