DevOps / Gradle8 Interview Questions
Why doesn't a lazily-configured task run during the configuration phase?
A task registered with tasks.register(...) is deliberately not fully created or configured at the moment that line executes — Gradle only stores a lightweight reference and the configuration block as a deferred action, and actually realizes (fully configures) the task only if something in the current build invocation actually needs it: it was explicitly requested, or another task that will run depends on it.
tasks.register('expensiveReport') { println "Configuring expensiveReport" // only prints if this task is actually needed doLast { /* ... */ } }
Contrast this with the older, eager task expensiveReport { ... } syntax, which fully configures the task immediately during the configuration phase, on every single build invocation, regardless of whether that task ends up running. Running ./gradlew compileJava with the lazy version above never prints "Configuring expensiveReport" at all, since that task was never requested and nothing else pulled it in — with the eager version, it would print every time, wasting configuration-phase time on a task that never actually executes. This is precisely the mechanism that makes large builds with hundreds of potential tasks configure quickly.
More Related questions...
