Access "project properties" (-P) from Plugin<Settings>

In settings.gradle files, you can use the method hasProperty, etc to access -P properties. However, this is something that gets “added” for Groovy. How can I do that from a Settings Plugin written in Java?

2 Likes

Actually I don’t know.

But at least since the changes for configuration cache, it should work via the ProviderFactory I think.
So let an instance of that be injected and then do ProviderFactory#gradleProperty("asdf").forUseAtConfigurationTime().get().
Then it also is configuration cache safe right away.

1 Like

Thanks @Vampire

I’ve also noticed settings.getStartParameter().getProjectProperties() which I am playing around with. Will let you know once I get it to a working state (need to get to 7.0 to leverage included-builds providing Settings-based plugins)

That will work, but only for the ones given as start parameter -P as that is where you get it from. This does not include gradle.properties files anywhere.

To make sure I understand… This creates or finds a property named “asdf” and makes it available at config time? And this will match -P as well as any gradle.properties and other locations?

It will not create it. If it is not there, the provider will have no value, so if it is valid that the value is absent you need things like getOrElse or getOrNull or isPresent.

Yes, it considers all places for Gradle properties, including the files and start parameters and taking precedence into account properly. It is the configuration cache safe way to use Gradle properties, so that the cache is invalidated if the value changes.

The provider is queryable at execution time, but you can also call forUseAtConfigurationTime on it to use it at configuration time, but Gradle will tell you if you need that call and didn’t make it.

That’s really awesome to know. Thanks!