If I want to set a property in my gralde.propertie...
# gradle
h
If I want to set a property in my gralde.properties file, how do I get it in my module (multi-module project)? I'm using latest gradle and kotlin DSL. property is:
a.b.c=Hello World
I've found this solution online but I'm wondering if there is a better way: https://stackoverflow.com/questions/55799287/how-to-read-a-dotted-gradle-property-in-kotlin-dsl
m
In every module, there's a
rootProject
available which you can access to retrieve extra properties. For example
Copy code
val pluginsDir: File by rootProject.extra
But usually the regular
project
should work, too:
Copy code
val ktorVersion: String by project
Is a line from the top of a one of my modules'
build.gradle.kts
a
I prefer using Gradle properties, accessed using
providers.gradleProperty("a.b.c")
1. the values are available via the Provider API, which has a lot of benefits 2. the values can have sensible defaults in the project's
gradle.properties
file, but can be overridden easily, which is useful for CI/CD or on other developer's machines
h
Yes saw that String by project but that doesn't work with dots properties
m
I wasn't aware of the Provider API 👍 Always used the delegated properties to access
gradle.properties
values. Thanks for the tip, having a dedicated mechanism (apart from shunting in a new
gradle.properties
) to override in CI/CD is certainly valuable.
👍 1
@Humphrey ah, right. But `
Copy code
providers.gradleProperty("a.b.c").get()
should work?
v
having a dedicated mechanism (apart from shunting in a new
gradle.properties
) to override in CI/CD is certainly valuable
That has nothing to do with
by ...
or
gradleProperty
, the same overrides work either way.
🙌 1
h
I’ll try the provider.
v
All these work for example:
Copy code
println(project.findProperty("a.b.c"))
println(providers.gradleProperty("a.b.c").get())
println(project.property("a.b.c"))
👍 2
Just the
by ...
does not work as even with back-ticks, dots are not valid in property names.
h
I find the providers.gradleProperty("my.property") the cleanest way to go. Thanks for the tips.
👌 2
106 Views