Gradle build lifecycle¶
The Gradle build lifecycle refers to the distinct phases a build goes through from initialization to completion. Understanding these phases is crucial for configuring builds correctly and writing effective [[Gradle tasks]].
Phases¶
The Gradle build lifecycle is primarily divided into three sequential phases^[600-developer__gradle__gradle-build.md]:
- Initialization: Gradle determines which projects are going to take part in the build, and creates a
Projectinstance for each of them. - Configuration: The build scripts of all projects participating in the build are executed. During this phase, project objects and the task graph are configured.
- Execution: The actual task graph is executed. Only the tasks identified as necessary for the current build invocation are run.
Execution Phase and Task Actions¶
Code defined within specific action blocks, such as doFirst or doLast, is handled differently based on the lifecycle^[600-developer__gradle__gradle-build.md]. Logic defined inside these blocks is invoked specifically during the execution phase^[600-developer__gradle__gradle-build.md].
In contrast, any code placed directly in the body of the task definition runs during the configuration phase. Therefore, to ensure operations occur strictly when the task is running (rather than during the setup of the build), they must be placed inside doFirst or doLast closures^[600-developer__gradle__gradle-build.md].
Related Concepts¶
- [[Task inputs and outputs]]
- [[Build configuration]]
- [[Project dependencies]]
Sources¶
^[600-developer__gradle__gradle-build.md]