Lazy Configuration API
Gradle's lazy value model: Provider and Property, transformation chains, lazy collections, and the ObjectFactory that creates managed types. Interviewers ask because the entire modern authoring API is built on it.
on this pageshowhide
explore
- Provider & Property Basics6 questions
- map / flatMap & Provider Chaining5 questions
- ListProperty / MapProperty / SetProperty5 questions
- ObjectFactory & Managed Types5 questions
- ProjectLayout & RegularFileProperty6 questions
- ProviderFactory: Env, Sysprops, External6 questions
- Configurable File Collections5 questions
questions
page 2 of 2What does ObjectFactory.named() do, and when do you use Named types in Gradle?
basics
~10 sobjects.named(Type, "value") creates an instance of a Named interface subtype carrying that string name. It's used for attribute values like Usage, Category, or custom variant attributes in dependency resolution.
How does ObjectFactory.newInstance() work, and how does service injection into the created instance behave?
basics
~10 sobjects.newInstance(Type, args) creates an instance, generating impls for abstract managed types and supplying constructor @Inject services (like ObjectFactory) plus any extra args you pass. It's how you create nested DSL objects.
How do you run an external process during configuration in a config-cache-safe way using ProviderFactory?
basics
~10 sUse providers.exec { commandLine(...) } which returns an ExecOutput whose standardOutput is a Provider. It is lazy and tracked as a configuration-cache input, unlike project.exec.
What pitfalls arise when wiring ProviderFactory values into task inputs, and how do you keep them lazy and config-cache-safe?
basics
~10 sDon't call .get() at configuration time, don't capture Project in task actions, and wire Providers into Property inputs with .set(provider) so resolution stays deferred and tracked.
What is a ValueSource and when would you use providers.of(...) over providers.environmentVariable or providers.exec?
basics
~10 sA ValueSource is a reusable, parameterized unit of external reading. You implement obtain() and call providers.of(MySource) { parameters... } to get a lazy, config-cache-safe Provider for any custom external input.
You're modernizing an old custom task that uses plain String/File fields. How do you migrate it to Property/Provider, and what pitfalls do you watch for?
basics
~10 sReplace eager fields with Property<T> declared as abstract managed properties or created via ObjectFactory, annotate inputs/outputs, set defaults with convention(), and read values at execution (inside the action) instead of at configuration time.
Design a plugin extension that lets users aggregate values from multiple build-script blocks into a collection consumed by a task. How do collection properties make this clean?
basics
~20 sExpose a ListProperty/MapProperty on the extension, give consumers add/put helper methods, set sensible conventions, and wire the task input to the extension property. Each build block appends lazily; the task resolves the aggregate at execution.
How do you obtain ProjectLayout inside a plugin or non-Project class without referencing project, and what convention defaults would you set for output locations?
basics
~10 sInject ProjectLayout via @Inject in a task or plugin-instantiated class instead of calling project.layout. Then set conventions like outputFile.convention(layout.buildDirectory.file("...")) so outputs default under build/ but stay overridable.
showing 31–38 of 38