Gradle Task: Difference between revisions
Line 30: | Line 30: | ||
Actions can be anonymous or named. | Actions can be anonymous or named. | ||
=Task Failure= | |||
Any task failure stops the build, with two exceptions: | |||
# If the task action throws [https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/StopActionException.html StopActionException], it aborts the current action, and continues at the ''next action''. | |||
# If the task action throws [https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/StopExecutionException.html StopExecutionException], it aborts the current action, and continues at the ''next task''. | |||
=Task Sources= | =Task Sources= |
Revision as of 23:55, 17 May 2018
External
Internal
Overview
A task is a core Gradle concept. It represents a single atomic piece of work for a build, such as compiling classes or generating javadoc. Each task belongs to a specific project. Each task has a name, which can be used to refer to the task within its project, and a fully qualified path, which is unique across all projects. The path is the concatenation of the owning project's path and the task name, separated by the ":" character.
A build consists in executing a sequence of tasks in succession. Gradle computes the Directed Acyclic Graph of to be executed in order to fulfill the tasks specified on command line, and then executes them honoring inter-task dependencies and insuring the fact that a task is executed only once. Gradle builds the complete dependency graph before any tasks is executed.
All tasks known to a build can be displayed with:
gradle tasks
The tasks known to a specific project can be displayed with:
gradle :<project-path>:tasks
Task Action
Each task consists of a sequence of Action objects, which are executed in the order of their declaration when the tasks is executed. Actions can be added to a task by calling Task's doFirst() or doLast() methods with an action closure. When the action is executed, the closure is called with the task as parameter.
doFirst|doLast { action-closure }
The last doFirst() invocation will configure the action that is executed first. The last doLast() invocation will configure the action that is executed last. The execution order is easy to figure out if you think of an action list to which doFirst() and doLast() add actions.
Actions can be anonymous or named.
Task Failure
Any task failure stops the build, with two exceptions:
- If the task action throws StopActionException, it aborts the current action, and continues at the next action.
- If the task action throws StopExecutionException, it aborts the current action, and continues at the next task.
Task Sources
Default Tasks
Any build comes with a number of pre-defined default tasks, regardless on whether plugins have been applied or not. They are available below:
gradle dependencies
Displays all dependencies (classpath) for various dependency configurations of the root project or a sub-project. More details in:
gradle dependencyInsight
Displays the insight into a specific dependency. More details in:
gradle dependentComponents
Displays the dependent components of components in project ':subproject-A'.
gradle model
Displays the configuration model of project ':subproject-A'.
gradle projects
Displays the sub-projects of project ':subproject-A'.
gradle properties
Displays the properties of project ':subproject-A'.
gradle tasks
Displays the tasks runnable from project ':subproject-A'.
gradle buildEnvironment
Displays all buildscript dependencies declared in project ':subproject-A'.
gradle components
Displays the components produced by project ':subproject-A'.
Tasks From Plugins
Plugins that have been applied to the project may come with their own tasks. This is a non-exhaustive list:
Explicit Task Declaration
Tasks may be explicitly declared in build.gradle using the "task" keyword (Project's task() method). There are four forms to declare custom tasks:
task <task-name> // for empty tasks
task <task-name> { configuration-closure }
task <task-name>(type: <SomeTaskType>) // empty task of a specific type
task <task-name>(type: <SomeTaskType>) { configuration-closure }
The configuration closures [#Task_Action|should add actions]], as described above.
As an example, a task can be declared as follows:
task sample {
println 'this is not an action, but a statement to be executed when the task is declared, in the build configuration phase'
doFirst {
println 'action executed second'
}
doFirst {
println 'action executed first'
}
doLast {
println 'action executed last'
}
}
Note that the statements declared outside doFirst {} and doLast {} in the task configuration closure will be executed in the build configuration phase, when the task is declared, not in the build execution phase.
Task Types
TODO: https://docs.gradle.org/current/dsl/#N10376
To Deplete
build.gradle allows declaring closures to react to various events related to tasks lifecycle. Also see Task Container.
task configured {
// will be run during the configuration phase
}
task doFirst {
}
task doLast {
}