Gradle Configuration
Gradle Configuration Phase
The Configuration phase is when Gradle evaluates and sets up tasks, inputs, outputs, and dependencies for the project. This occurs after the Initialization phase but before any tasks are executed. The goal of this phase is to prepare tasks for potential execution.
Key Points:
Task Configuration:
- Gradle evaluates the build script and configures tasks, even if they are not executed later.
- Eagerly created tasks (e.g.,
tasks.create
) are always configured, regardless of whether they are executed. - Lazily registered tasks (e.g.,
tasks.register
) are only configured when they will be needed during task execution or if they are required by other tasks. Configuration always happens in the configuration phase, which is before any task execution.
Dependency Graph Setup:
- Gradle sets up the task dependency graph during this phase. It determines the order in which tasks will be executed, based on task dependencies.
Example:
// Eager task creation - always configured
tasks.create("eagerTask") {
println("Eager task: configuration")
doLast {
println("Eager task")
}
}
// Lazy task registration - configured only when needed
tasks.register("lazyTask") {
println("Lazy task: configuration")
doLast {
println("Lazy task")
}
}
Output: Eager task
mac> ./gradlew eagerTask
> Configure project :lib
Eager task: configuration
> Task :lib:eagerTask
Eager task
Output: lazy task (Eager task configuration still runs)
> Configure project :lib
Eager task: configuration
Lazy task: configuration
> Task :lib:lazyTask
Lazy task
Glass thought Sandbox Snapshot
Command to reproduce:
gt.sandbox.checkout.commit f72d6a7 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew lazyTask"
Recorded output of command:
> Configure project :lib
Eager task: configuration
Lazy task: configuration
> Task :lib:lazyTask
Lazy task
BUILD SUCCESSFUL in 358ms
1 actionable task: 1 executed
In the example:
eagerTask
is configured immediately during the configuration phase.lazyTask
is configured only if it is explicitly invoked or needed by another task.
Children
Backlinks