Provider and Property Tools

In Gradle 6.1, the ProviderFactory introduced methods for obtaining Gradle properties, system properties and environment variables as providers. Grolifant offers additional features above and beyond the ones by the Gradle API. This is offered via the ProviderTools object has some methods to obtain project information.

An instance of it can be obtained via ConfigCacheSafeOptions.providerTools() or ProjectOperations.getProviderTools(). This instance is configuration cache compatible and can be called from within a task action.

Property Providers

Using these methods, can safeguard your plugin aginst cusage on a Gradle version where configuration-cache is enabled.

Methods
  • environmentVariable(Object) - returns an environment variable as a provider. The name of the variable is lazy-evaluated and can be anything that stringize in StringTools will resolve.

  • gradleProperty(Object) - returns a Gradle property as a provider. The name of the variable is lazy-evaluated and can be anything that stringize in StringTools will resolve.

  • systemProperty(Object) - returns a Gradle property as a provider. The name of the variable is lazy-evaluated and can be anything that stringize in StringTools will resolve.

  • resolveProperty(Object) - Attempts to resolve a Gradle property by that name first. If it does not exist, then attempt a system property and finally en environment variable. The name of the variable is lazy-evaluated and can be anything that stringize in StringTools will resolve.

  • resolveProperty(Object,Object) - Similar to the previous method, but if none of the three resolves, apply a default value. The latter value is lazily-converted to a string.

For resolveProperty, if the provided named is ab.cd.ef, then the corresponding environment variable will be called AB_CD_EF. Although this approach is modelled upon Spring Boot & Micronaut property naming, it is not nearly as comprehensive in converting in naming formats.

Other Provider methods

Create a Provider from a Callable

A convenience method to create a Gradle Provider from a Java Callable. Use provider if you already have an instance of ProviderTools, but not Gradle’s `ProviderFactory.

Create a list or map property

Methods

Orderly resolve three providers

Creates a provider that can resolve three providers in order. If the first is not present, it will attempt to resolve the second and then the third.

Use resolveOrderly to execute this common pattern.

Resolve a Provider to Optional

Need to convert Gradle’s Provider to Java’s Optional? Then use resolveToOptional

Miscellaneous

newInstance is a shortcut to the similarly named method on Gradle’s ObjectFactory.