Skip to content

Gradle build lifecycle phases

The Gradle build lifecycle is a foundational concept in Gradle that dictates the sequence of operations during a build. Understanding these phases is critical for configuring projects correctly and creating tasks that behave as expected.^[600-developer-gradle-gradle-build.md]

Initialization Phase

The Initialization phase is the first step of the lifecycle where Gradle determines which projects are going to take part in the build^[600-developer-gradle-gradle-build.md]. For a single-project build, this phase involves creating a single Project instance^[600-developer-gradle-gradle-build.md].

Configuration Phase

During the Configuration phase, Gradle constructs a model of the build by executing the build scripts for all projects participating in the build^[600-developer-gradle-gradle-build.md]. This phase is responsible for evaluating the code and building the task graph, which includes resolving dependencies and determining the execution order of tasks^[600-developer-gradle-gradle-build.md].

It is important to note that logic defined directly in the task closure runs during this configuration phase, before any tasks actually execute^[600-developer-gradle-gradle-build.md].

Execution Phase

The Execution phase is the final stage where the actual work of the build is performed^[600-developer-gradle-gradle-build.md]. During this phase, Gradle executes the subset of tasks that were created and configured in the previous phase and that were explicitly requested (or are dependencies of requested tasks)^[600-developer-gradle-gradle-build.md].

Code specifically added to a task's doFirst or doLast blocks is executed during this phase^[600-developer-gradle-gradle-build.md].

Task Execution and Phases

Understanding the lifecycle is essential for proper task configuration. While code inside a task definition runs during the Configuration phase, actions added via doFirst and doLast are reserved for the Execution phase^[600-developer-gradle-gradle-build.md].

Gradle also manages the order of task execution automatically based on dependencies defined in the task graph^[600-developer-gradle-gradle-build.md].

Sources

  • 600-developer-gradle-gradle-build.md