This message was deleted.
# community-support
s
This message was deleted.
c
Perhaps something like:
Copy code
public fun Project.resolveProperty(name : String) : String? {
    return (extra[name]?: System.getenv(name)) as String? 
}
s
Precisely doing
extra[""] ?:
Doesn't work since extra.get throws an exception, doesn't default to null when there's no such key found. Also I'd optimally like to use this inside my settings file too, where I don't have access to
Project
afaik. As a side note, where would you host such an extension function so that it's available throughout your entire project? For all modules?
c
adjust as appropriate. The point was to create an extension function that encapsulates your specific requirements. Settings will need a variation with some tweaks. Typically these type of extension functions are in a composite
build-logic
project (successor to buildSrc).
s
Alright then I'm not missing some API and I'll just have to wrap what I wrote in my original comment in my own function. Thanks for the help 🙏
v
Just use
project.findProperty
? That should automatically give you what you want. At least if the env variable does not have to be the exact same name, as there is a naming convention how to set a Gradle property.
s
But still can’t do that from inside settings.gradle.kts right, so I will have to do something special regardless. Also as far as the naming convention for the gradle property goes, I assume you’re talking about this just like in the other thread where Nuh suggested I add the
ORG_GRADLE_PROJECT
suffix right?
v
Yes, in settings script there is unfortunately no
findProperty
. And yes about the naming convention.
thank you 1
g
Or just use
val propertyName: String? by project
or
by settings
when applicable. Requires use of a naming convention (gradle property must be valid kotlin identifier then) and supports setting via env var in the same way as above.
s
In settings I opted to doing
providers.gradleProperty("KEY").getOrElse(System.getenv("KEY"))
. I didn’t know this
settings
existed. If I did this
by settings
and it only exists in the environment variables (true for our CI) would it work? Or would I still need to fallback to null, and do System.getEnv() anyway?
v
If you follow the mentioned naming convention it will work
👍 1
g
Of that I'm not sure. ORG_GRADLE_PROJECT suggests that no(
v
Hm, good point, but that is probably more a left-over from them being called "project properties" while they are now called "Gradle properties"
g
For a variant with provider factory you could use providers.gradleProperty(..).or(providers.environmentVariable(..))
v
As expected,
ORG_GRADLE_PROJECT_propertyName
works fine, also in settings script, I quickly tried
thank you 1
👍 1
g
Good to know. I'm afk right now so couldn't check
s
Aha! So it will still work if I follow the naming conventions, and I can avoid doing System.getEnv() So I can go from
providers.gradleProperty("KEY").getOrElse(System.getenv("KEY"))
to
providers.gradleProperty("KEY").getOrElse(providers.environmentVariable("KEY").get()).
Interesting! For the
by settings
case, awesome to hear that it works, thanks for checking 🙌 Is there a way I can do this more inline instead of having to assign it to a variable using
by
? Just so that it doesn’t have to do something like this
Copy code
val x: String by settings
this.foo = x
v
The point is, that it takes the name from the variable name
g
You shouldn't use provider.get when you don't need it. Just use provider.orElse to pass the fallback provider
v
And be aware that if you use
providers.gradleProperty
in a build script, you will not get the extra properties considered.
âž• 1
s
What is “extra properties” in this context?
v
The same you were talking about
s
Okay damn, quite a lot of things to consider in this one. On one hand I’d love there to be 1 option and not have too many things to choose from, but I also understand why there may be many reasons why there are all these options there in the first place. Thanks a ton for walking me through this, I really do appreciate it! I think I got the right information now do what I need to do thank you
👌 1