Slackbot
09/29/2022, 5:34 PMChris Lee
09/29/2022, 5:36 PM/**
* Looks up the specified property from the following sources, in order:
*
* -Gradle property
* -System property
* -Environment variable (with name transformed to uppercase, '.' replaced with '_'
*/
public fun ProviderFactory.property(name: Provider<String>): Provider<String> {
return gradleProperty(name)
.orElse(systemProperty(name))
.orElse(environmentVariable(name.map { it.uppercase().replace(".", "_") }))
.forUseAtConfigurationTime()
}
Caleb Cushing
09/29/2022, 5:48 PMChris Lee
09/29/2022, 5:49 PMCaleb Cushing
09/29/2022, 5:49 PMCaleb Cushing
09/29/2022, 5:50 PMChris Lee
09/29/2022, 5:50 PMChris Lee
09/29/2022, 5:51 PM.get()
, etc on properties.Caleb Cushing
09/29/2022, 5:51 PMCaleb Cushing
09/29/2022, 5:51 PMChris Lee
09/29/2022, 5:52 PMCaleb Cushing
09/29/2022, 5:52 PMCaleb Cushing
09/29/2022, 5:52 PMCaleb Cushing
09/29/2022, 5:53 PMChris Lee
09/29/2022, 5:53 PMCaleb Cushing
09/29/2022, 6:08 PMChris Lee
09/29/2022, 6:11 PMnamed<Test>("test") {
prop.set(
providers.gradleProperty("some.gradle.property")
.orElse(providers.systemProperty("some.system.property"))
)
}
Chris Lee
09/29/2022, 6:12 PMprop
on the test
task; within the task you would use prop.get()
to resolve it’s value.Caleb Cushing
09/29/2022, 6:14 PMChris Lee
09/29/2022, 6:15 PM.convention
(or add another orElse). If you really want to check if it has a value use prop.isPresent
inside the task action.Chris Lee
09/29/2022, 6:16 PMpublic fun ProviderFactory.gradlePropertyPresent(name: Provider<String>): Provider<Boolean> {
return gradleProperty(name).map { true }.forUseAtConfigurationTime()
}
Caleb Cushing
09/29/2022, 6:18 PMCaleb Cushing
09/29/2022, 6:18 PMChris Lee
09/29/2022, 6:18 PMCaleb Cushing
09/29/2022, 6:19 PMCaleb Cushing
09/29/2022, 6:20 PMChris Lee
09/29/2022, 6:20 PMProvider<String> gradleProperty(Provider<String> propertyName);
on ProviderFactory since Gradle 6.2Chris Lee
09/29/2022, 6:21 PMProvider<String> systemProperty(String propertyName);
on ProviderFactory since Gradle 6.1Caleb Cushing
09/29/2022, 6:24 PMChris Lee
09/29/2022, 6:25 PMproviders
on the project that should be used.Caleb Cushing
09/29/2022, 6:34 PMChris Lee
09/29/2022, 6:35 PM.get()
inside the task action on the proeprty.Caleb Cushing
09/29/2022, 6:40 PMChris Lee
09/29/2022, 6:41 PM.get()
should only be called at execution time (inside task action).Caleb Cushing
09/29/2022, 6:41 PMChris Lee
09/29/2022, 6:41 PMCaleb Cushing
09/29/2022, 6:47 PMChris Lee
09/29/2022, 6:49 PMnamed<Test>("test") {
prop.set(
providers.gradleProperty("some.gradle.property")
.orElse(providers.systemProperty("some.system.property"))
)
}
that create a provider that will first try a gradle property then a system property.
In your task action use prop.get()
to resolve.Caleb Cushing
09/29/2022, 7:01 PMCaleb Cushing
09/29/2022, 7:01 PMChris Lee
09/29/2022, 7:01 PMChris Lee
09/29/2022, 7:02 PMCaleb Cushing
09/29/2022, 7:02 PMChris Lee
09/29/2022, 7:02 PMCaleb Cushing
09/29/2022, 7:02 PMCaleb Cushing
09/29/2022, 7:02 PMCaleb Cushing
09/29/2022, 7:03 PMCaleb Cushing
09/29/2022, 7:07 PMCaleb Cushing
09/29/2022, 7:08 PMtasks.test { useJunitPlatform() providers.gradleProperty("ci").orNull?.run { systemProperty("spring.profiles.active, "ci") } ... }
Chris Lee
09/29/2022, 7:10 PMCaleb Cushing
09/29/2022, 7:11 PMCaleb Cushing
09/29/2022, 7:12 PMChris Lee
09/29/2022, 7:12 PMtasks.named<Test>("test") {
useJUnitPlatform()
systemProperty( "spring.profiles.active", providers.gradleProperty("test.spring.profiles.active").orElse(""))
}
Chris Lee
09/29/2022, 7:13 PM-Ptest.spring.profiles.active=ci
Caleb Cushing
09/29/2022, 7:13 PMCaleb Cushing
09/29/2022, 7:14 PMCaleb Cushing
09/29/2022, 7:14 PMCaleb Cushing
09/29/2022, 7:17 PMChris Lee
09/29/2022, 7:17 PMCaleb Cushing
09/29/2022, 7:18 PMChris Lee
09/29/2022, 7:19 PMCaleb Cushing
09/29/2022, 7:20 PMCaleb Cushing
09/29/2022, 7:21 PMChris Lee
09/29/2022, 7:22 PM.map {}
to add test into what was passed in the property.
The difference is that Gradle will invoke the provider internally, if and when required, removing .get() or .orNull from your code. Imperative code in build scripts is a code smell, makes it noisy and hard to maintain.Caleb Cushing
09/29/2022, 7:23 PMCaleb Cushing
09/29/2022, 7:25 PMChris Lee
09/29/2022, 7:25 PMCaleb Cushing
09/29/2022, 7:25 PMCaleb Cushing
09/29/2022, 7:26 PMorElse
is just as imperative 😉Caleb Cushing
09/29/2022, 7:28 PMCaleb Cushing
09/29/2022, 7:29 PMChris Lee
09/29/2022, 7:29 PMCaleb Cushing
09/29/2022, 7:29 PMCaleb Cushing
09/29/2022, 7:29 PMCaleb Cushing
09/29/2022, 7:29 PMCaleb Cushing
09/29/2022, 7:30 PMCaleb Cushing
09/29/2022, 7:30 PMifPresent
Caleb Cushing
09/29/2022, 7:30 PMOptional
Chris Lee
09/29/2022, 7:31 PM.orNull
or .get()
during configuration (such as configuring a task) that subverts lazy configuration. Providers are generally either passed to Gradle to be later resolve, or resolved in your custom task actions (during execution of the task, if the task is even executed).Chris Lee
09/29/2022, 7:32 PMisPresent
on Provider but that falls into the same lazy configuration trap.Chris Lee
09/29/2022, 7:32 PMCaleb Cushing
09/29/2022, 7:32 PMif
not is
Caleb Cushing
09/29/2022, 7:32 PMCaleb Cushing
09/29/2022, 7:34 PMCaleb Cushing
09/29/2022, 7:35 PMChris Lee
09/29/2022, 7:35 PMprop1.set(prop2)
, without having to resolve the values.Chris Lee
09/29/2022, 7:35 PMCaleb Cushing
09/29/2022, 7:38 PMCaleb Cushing
09/29/2022, 7:38 PMChris Lee
09/29/2022, 7:39 PMscmVersion {
localOnly.set(true)
tag {
prefix.set("release")
versionSeparator.set("/")
}
}
Caleb Cushing
09/29/2022, 7:42 PMChris Lee
09/29/2022, 7:42 PMCaleb Cushing
09/29/2022, 7:52 PMsystemProperty
doesn't take a Provider, and orElse
returns a providerCaleb Cushing
09/29/2022, 7:52 PMCaleb Cushing
09/29/2022, 8:05 PMChris Lee
09/29/2022, 8:06 PMVampire
09/30/2022, 7:02 AMsystemProperty
would properly handle providers, the difference would not only be style, but a Chris version would reuse CC even if the value changes, yours notCaleb Cushing
09/30/2022, 12:38 PMCaleb Cushing
09/30/2022, 1:04 PMCaleb Cushing
09/30/2022, 1:04 PMCaleb Cushing
09/30/2022, 1:04 PMCaleb Cushing
09/30/2022, 1:05 PMCaleb Cushing
09/30/2022, 1:05 PMCaleb Cushing
09/30/2022, 1:05 PMCaleb Cushing
09/30/2022, 1:32 PMVampire
09/30/2022, 1:37 PMIn this case is mine good enough?If the test task cannot get the value from a provider anyway (didn't check), probably yes.