api / org.gradle.api.provider / Provider

Provider

@Incubating @NonExtensible interface Provider<T : Any>

A container object that provides a value of a specific type. The value can be retrieved by the method #get() or #getOrNull().

A provider may not always have a value available, for example when the value may not yet be known but will be known at some point in the future. When a value is not available, #isPresent() returns false and retrieving the value will fail with an exception.

A provider may not always provide the same value. Although there are no methods on this interface to change the value, the provider implementation may be mutable or use values from some changing source.

A provider may provide a value that is mutable and that changes over time.

A typical use of a provider is to pass values from one DSL element to another, e.g. from an extension to a task. Providers also allow expensive computations to be deferred until their value is actually needed, usually at task execution time.

For a provider whose value can be mutated, see Property.

Do not use Provider<File>. Use org.gradle.api.file.Directory or org.gradle.api.file.RegularFile instead.

Note: This interface is not intended for implementation by build script or plugin authors. An instance of this class can be created through the factory methods org.gradle.api.Project#provider(java.util.concurrent.Callable) or org.gradle.api.provider.ProviderFactory#provider(java.util.concurrent.Callable).

Parameters

- Type of value represented by provider

Since
4.0

Functions

get

abstract fun get(): T

Returns the value of this provider if it has a value present, otherwise throws java.lang.IllegalStateException.

getOrElse

abstract fun getOrElse(defaultValue: T): T

Returns the value of this provider if it has a value present. Returns the given default value if a value is not available.

getOrNull

abstract fun getOrNull(): T

Returns the value of this provider if it has a value present. Returns null a value is not available.

isPresent

abstract fun isPresent(): Boolean

Returns true if there is a value present, otherwise false.

map

abstract fun <S : Any> map(transformer: Transformer<out S, in T>): Provider<S>

Returns a new Provider whose value is the value of this provider transformed using the given function.

The new provider will be live, so that each time it is queried, it queries this provider and applies the transformation to the result. Whenever this provider has no value, the new provider will also have no value.

Note that the new provider may cache the result of the transformations and so there is no guarantee that the transformer is called on every query of the new provider. The new provider will apply the transformation lazily, and calculate the value for the new provider when queried.

Inheritors

BinaryProvider

interface BinaryProvider<T : Any> : Provider<T>

Represents a binary that is created and configured as required.

DirectoryProperty

interface DirectoryProperty : Provider<Directory>, Property<Directory>

Represents some configurable directory location, whose value is mutable and is not necessarily currently known until later.

Note: This interface is not intended for implementation by build script or plugin authors. An instance of this class can be created using the ProjectLayout#directoryProperty() method.

ListProperty

interface ListProperty<T : Any> : Provider<MutableList<T>>, HasMultipleValues<T>

Represents a property whose type is a List of elements of type T.

Note: This interface is not intended for implementation by build script or plugin authors. An instance of this class can be created through the factory method org.gradle.api.model.ObjectFactory#listProperty(Class).

Property

interface Property<T : Any> : Provider<T>

A Provider representation for capturing the state of a property. The value can be provided by using the method #set(Object) or #set(Provider).

Note: This interface is not intended for implementation by build script or plugin authors. An instance of this class can be created through the factory method org.gradle.api.model.ObjectFactory#property(Class). There are also several specialized subtypes of this interface that can be created using various other factory methods.

RegularFileProperty

interface RegularFileProperty : Provider<RegularFile>, Property<RegularFile>

Represents some configurable regular file location, whose value is mutable and not necessarily currently known until later.

Note: This interface is not intended for implementation by build script or plugin authors. An instance of this class can be created using the ProjectLayout#fileProperty() method.

SetProperty

interface SetProperty<T : Any> : Provider<MutableSet<T>>, HasMultipleValues<T>

Represents a property whose type is a Set of elements of type T. Retains iteration order.

Note: This interface is not intended for implementation by build script or plugin authors. An instance of this class can be created through the factory method org.gradle.api.model.ObjectFactory#setProperty(Class).

TaskProvider

interface TaskProvider<T : Task> : Provider<T>

Providers a task of the given type.