Skip to content

Gradle Task configuration and execution

A Task in Gradle represents a discrete unit of work to be performed, such as compiling code or copying files.^[600-developer__gradle__gradle-build.md] Tasks are the fundamental building blocks of a Gradle build and can be configured, linked together, and executed in specific orders.

Task Configuration

Configuration logic defines the behavior of a task. This includes setting properties, defining inputs and outputs, and arranging the execution order.^[600-developer__gradle__gradle-build.md]

Inputs and Outputs

Tasks can declare inputs and outputs using the TaskInputs and TaskOutputs APIs.^[600-developer__gradle__gradle-build.md] This declaration allows Gradle to perform Up-to-date checks (UP-TO-DATE); if a task's inputs and outputs have not changed since the last execution, Gradle will skip the execution of that task to save time.^[600-developer__gradle__gradle-build.md]

Task Dependencies

Tasks often rely on other tasks running first. This ordering is managed through dependencies.^[600-developer__gradle__gradle-build.md] You can specify that a task depends on another task or explicitly configure a task to run only after a different task has completed.^[600-developer__gradle__gradle-build.md]

Execution Actions

Gradle provides specific hooks to add actions to a task's lifecycle: * doFirst: Code defined here executes at the very beginning of the task's execution phase.^[600-developer__gradle__gradle-build.md] * doLast: Code defined here executes at the very end of the task's execution phase.^[600-developer__gradle__gradle-build.md]

Only code placed within doFirst or doLast blocks is guaranteed to run during the actual execution phase of the build lifecycle.^[600-developer__gradle__gradle-build.md]

Creation and Definition

There are multiple ways to create and define tasks within a build.gradle script.^[600-developer__gradle__gradle-build.md]

Dynamic Extension with metaClass

Gradle allows you to dynamically extend the functionality of the project or objects using the metaClass property.^[600-developer__gradle__gradle-build.md] This enables the injection of third-party features or custom methods into existing types at runtime.^[600-developer__gradle__gradle-build.md]

Sources

^[600-developer__gradle__gradle-build.md]