This message was deleted.
# community-support
s
This message was deleted.
c
Place this:
if (env.get() == "prod") { … }
in a provider to the task, to be evaluated at execution time.
v
I'd say update Gradle. "Use a provider returned by 'forUseAtConfigurationTime()' instead." is long gone.
i
@Chris Lee can you share a small snippet of what that means?
@Vampire Are you saying that what I did is the correct way to do it, in versions more recent than what I have?
c
either way is doable with current Gradle versions. Generally prefer to avoid calling provider.get() at configuration time, but for straightforward properties it can be OK. Here’s pseudocode of shifting the conditional logic inside a provider to a task property:
Copy code
someProperty.set(provider { if(env.get() == "prod") { "a"} else {"b"}})
i
If using a provider at configuration time is not good, what is the correct way to configure a task based on a Gradle property/environment variable?
c
Chain the providers/properties, such as:
Copy code
someOtherProperty.set(providers.gradleProperty("env").orElse("dev"))
i
What it does is set a dozen or so system properties
c
Not sure what level of providers/property support JavaExec has; could see about mapping the environment to a Map of system properties and providing that.
v
Iirc you cannot yet use providers lazily for system properties, so the approach is probably correct and if you need to stay on that ancient version, do what the error suggestes but is deprecated and obsolete in recent versions. 🙂
i
I don't need to stay on that old version, it's just that I can't migrate soon. It will happen at some point.
v
As the Gradle properties also cannot change during the run, it doesn't really matter whether you use it lazily or not, so evaluating at configuration time is fine.
It's just that you need to call that function before getting the provider in that Gradle version.
c
Example of a provider of a map of properties. As @Vampire mentioned the JavaExec task may not use providers for system props.
Copy code
val systemProps = providers.gradleProperty("env").orElse("dev").map { 
    when(it) {
        "dev" -> mapOf("a.b.c" to "d.e.f")
        else -> mapOf("a.b.c" to "x.y.z")
    }
}
i
Thanks for the explanation