ProviderFactory

API Documentation:ProviderFactory

A factory for creating instances of Provider.

An instance of the factory can be injected into a task, plugin or other object by annotating a public constructor or property getter method with javax.inject.Inject. It is also available via Project.getProviders() and Settings.getProviders().

Properties

No properties

Methods

MethodDescription
absent()
Incubating

Returns a Provider that is always absent, that is, one that never has a value.

credentials(credentialsType, identity)

Creates a Provider for the given Credentials type.

credentials(credentialsType, identity)

Creates a Provider for the given Credentials type.

environmentVariable(variableName)

Creates a Provider whose value is fetched from the environment variable with the given name.

environmentVariable(variableName)

Creates a Provider whose value is fetched from the environment variable with the given name.

environmentVariablesPrefixedBy(variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the environment variables with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

environmentVariablesPrefixedBy(variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the environment variables with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

exec(action)

Allows lazy access to the output of the external process.

fileContents(file)

Allows lazy access to the contents of the given file.

fileContents(file)

Allows lazy access to the contents of the given file.

gradlePropertiesPrefixedBy(propertyNamePrefix)

Creates a Provider whose value is a name-to-value map of the Gradle properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

gradlePropertiesPrefixedBy(propertyNamePrefix)

Creates a Provider whose value is a name-to-value map of the Gradle properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

gradleProperty(propertyName)

Creates a Provider whose value is fetched from the Gradle property of the given name.

gradleProperty(propertyName)

Creates a Provider whose value is fetched from the Gradle property of the given name.

javaexec(action)

Allows lazy access to the output of the external java process.

of(valueSourceType, configuration)

Creates a Provider whose value is obtained from the given ValueSource.

present(value)
Incubating

Returns a PresentProvider that is always present, providing the given value.

presentIfNotNull(value)
Incubating

Returns a Provider that is always present when the given value is non-null, or always absent otherwise, as if created by <UNHANDLED-LINK>#present(Object)</UNHANDLED-LINK> or ProviderFactory.absent() respectively.

provider(value)

Creates a Provider whose value is calculated using the given Callable.

systemPropertiesPrefixedBy(variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the system properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

systemPropertiesPrefixedBy(variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the system properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

systemProperty(propertyName)

Creates a Provider whose value is fetched from system properties using the given property name.

systemProperty(propertyName)

Creates a Provider whose value is fetched from system properties using the given property name.

zip(first, second, combiner)

Returns a provider which value will be computed by combining a provider value with another provider value using the supplied combiner function.

Script blocks

No script blocks

Method details

Provider<T> absent()

Note: This method is incubating and may change in a future version of Gradle.

Returns a Provider that is always absent, that is, one that never has a value.

The returned provider is never Provider.isPresent(): querying its value with Provider.get() always fails and Provider.getOrNull() always returns null.

Provider<T> credentials(Class<T> credentialsType, String identity)

Creates a Provider for the given Credentials type.

The provider returned by this method should be attached to a task's input property. This way, the presence of credentials will be validated before any of the tasks are executed if and only if the task with credentials property is to be executed.

Values for the requested Credentials type will be sourced from the project's properties using the pattern "identity" + credentials field. For example, PasswordCredentials provider with identity "myService" will look for properties named "myServiceUsername" and "myServicePassword".

The following credential types are currently supported:

Provider<T> credentials(Class<T> credentialsType, Provider<String> identity)

Creates a Provider for the given Credentials type.

The provider returned by this method should be attached to a task's input property. This way, the presence of credentials will be validated before any of the tasks are executed if and only if the task with credentials property is to be executed.

Values for the requested Credentials type will be sourced from the project's properties using the pattern "identity" + credentials field. For example, PasswordCredentials provider with identity "myService" will look for properties named "myServiceUsername" and "myServicePassword".

The following credential types are currently supported:

Provider<String> environmentVariable(String variableName)

Creates a Provider whose value is fetched from the environment variable with the given name.

Provider<String> environmentVariable(Provider<String> variableName)

Creates a Provider whose value is fetched from the environment variable with the given name.

Provider<Map<String, String>> environmentVariablesPrefixedBy(String variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the environment variables with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

Provider<Map<String, String>> environmentVariablesPrefixedBy(Provider<String> variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the environment variables with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

ExecOutput exec(Action<? super ExecSpec> action)

Allows lazy access to the output of the external process.

When the process output is read at configuration time it is considered as an input to the configuration model. Consequent builds will re-execute the process to obtain the output and check if the cached model is still up-to-date.

The process input and output streams cannot be configured.

For more sophisticated use cases, like handling execution exceptions, supplying input streams or processing the output in a streaming way, use ValueSource.

FileContents fileContents(RegularFile file)

Allows lazy access to the contents of the given file.

When the file contents are read at configuration time the file is automatically considered as an input to the configuration model.

FileContents fileContents(Provider<RegularFile> file)

Allows lazy access to the contents of the given file.

When the file contents are read at configuration time the file is automatically considered as an input to the configuration model.

Provider<Map<String, String>> gradlePropertiesPrefixedBy(String propertyNamePrefix)

Creates a Provider whose value is a name-to-value map of the Gradle properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

Properties are resolved at the build level from the same sources as ProviderFactory.gradleProperty(java.lang.String).

More information on project properties.

Provider<Map<String, String>> gradlePropertiesPrefixedBy(Provider<String> propertyNamePrefix)

Creates a Provider whose value is a name-to-value map of the Gradle properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

Properties are resolved at the build level from the same sources as ProviderFactory.gradleProperty(java.lang.String).

More information on project properties.

Provider<String> gradleProperty(String propertyName)

Creates a Provider whose value is fetched from the Gradle property of the given name.

The property value is resolved from the following sources, in order of priority:

  1. Command-line project properties (-P arguments)
  2. System properties with the org.gradle.project. prefix
  3. Environment variables with the ORG_GRADLE_PROJECT_ prefix
  4. gradle.properties in the Gradle User Home directory
  5. gradle.properties in the build root directory
  6. gradle.properties in the Gradle installation directory

Note that this method resolves properties at the build level. It does not include properties from gradle.properties files in subproject directories, nor does it include extra properties or other properties set dynamically on individual Project instances.

More information on project properties.

Provider<String> gradleProperty(Provider<String> propertyName)

Creates a Provider whose value is fetched from the Gradle property of the given name.

The property value is resolved from the following sources, in order of priority:

  1. Command-line project properties (-P arguments)
  2. System properties with the org.gradle.project. prefix
  3. Environment variables with the ORG_GRADLE_PROJECT_ prefix
  4. gradle.properties in the Gradle User Home directory
  5. gradle.properties in the build root directory
  6. gradle.properties in the Gradle installation directory

Note that this method resolves properties at the build level. It does not include properties from gradle.properties files in subproject directories, nor does it include extra properties or other properties set dynamically on individual Project instances.

More information on project properties.

ExecOutput javaexec(Action<? super JavaExecSpec> action)

Allows lazy access to the output of the external java process.

When the process output is read at configuration time it is considered as an input to the configuration model. Consequent builds will re-execute the process to obtain the output and check if the cached model is still up-to-date.

The process input and output streams cannot be configured.

For more sophisticated use cases, like handling execution exceptions, supplying input streams or processing the output in a streaming way, use ValueSource.

Provider<T> of(Class<? extends ValueSource<T, P>> valueSourceType, Action<? super ValueSourceSpec<P>> configuration)

Creates a Provider whose value is obtained from the given ValueSource.

PresentProvider<T> present(T value)

Note: This method is incubating and may change in a future version of Gradle.

Returns a PresentProvider that is always present, providing the given value.

Unlike ProviderFactory.provider(java.util.concurrent.Callable), the value is not computed on demand: querying the returned provider simply returns the given value. Prefer this method over ProviderFactory.provider(java.util.concurrent.Callable) when the value is already known.

Provider<T> presentIfNotNull(T value)

Note: This method is incubating and may change in a future version of Gradle.

Returns a Provider that is always present when the given value is non-null, or always absent otherwise, as if created by <UNHANDLED-LINK>#present(Object)</UNHANDLED-LINK> or ProviderFactory.absent() respectively.

Provider<T> provider(Callable<? extends T> value)

Creates a Provider whose value is calculated using the given Callable.

The provider is live and will call the Callable each time its value is queried. The Callable may return null, in which case the provider is considered to have no value.

Configuration Cache

This provider is always computed and its value is cached by the Configuration Cache. If this provider is created at configuration time, the Callable may call configuration-time only APIs and capture objects of arbitrary types.

This can be useful when you need to lazily compute some value to use at execution time based on configuration-time only data. For example, you can compute an archive name based on the name and the version of the project:

  tasks.register("createArchive") {
      def archiveNameProvider = providers.provider { project.name + "-" + project.version + ".jar" }
      doLast {
          def archiveName = new File(archiveNameProvider.get())
          // ... create the archive and put in its contents.
      }
  }

Provider<Map<String, String>> systemPropertiesPrefixedBy(String variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the system properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

Provider<Map<String, String>> systemPropertiesPrefixedBy(Provider<String> variableNamePrefix)

Creates a Provider whose value is a name-to-value map of the system properties with the names starting with the given prefix. The prefix comparison is case-sensitive. The returned map is immutable.

Provider<String> systemProperty(String propertyName)

Creates a Provider whose value is fetched from system properties using the given property name.

Provider<String> systemProperty(Provider<String> propertyName)

Creates a Provider whose value is fetched from system properties using the given property name.

Provider<R> zip(Provider<A> first, Provider<B> second, BiFunction<? super A, ? super B, ? extends R> combiner)

Returns a provider which value will be computed by combining a provider value with another provider value using the supplied combiner function.

The resulting provider will be live, so that each time it is queried, it queries both supplied providers and applies the combiner to the results. Whenever any of the providers has no value, the new provider will also have no value and the combiner will not be called.

If the supplied providers represents a task or the output of a task, the resulting provider will carry the dependency information.