DevOps / Gradle8 Interview Questions
How does the Provider API enable lazy configuration?
The Provider API (Provider<T>, Property<T>) wraps a value so it's computed only when actually needed, rather than being resolved eagerly the moment a build script line executes during the configuration phase. This matters because configuration runs on every single build invocation, so eagerly computing an expensive or environment-dependent value there wastes time even when the task using it never actually runs.
def versionProvider = providers.gradleProperty('app.version').orElse('dev') tasks.register('printVersion') { doLast { println versionProvider.get() // resolved only now, at execution time } }
Because a Provider represents "a value that will be computed later" rather than the value itself, providers can also be chained (.map(), .flatMap()) to build up derived values without forcing early evaluation at any point in the chain, and task properties built on Property types automatically wire into up-to-date checks and the configuration cache. This laziness is also what allows one task's output to be wired directly as another task's input (outputFile.set(otherTask.outputFile)) without needing either task to have actually run yet.
More Related questions...
