How to use a user-local gradle.properties instead of the project one

I have a Java project which happens to be built in a recent version (4.13) of the Tridium Niagara framework and is therefore based on Gradle 7.3.1. I’m using Windows 11.

It so happens that Niagara 4.13 has now switched away from using Groovy to Kotlin DSL, so I’m updating my various Gradle-related files accordingly. That update process has gone reasonably smoothly apart from one aspect where I’m having an issue: I want the the copy of gradle.properties in the root of the project to be under git control for sharing with the rest of my team, so I want that file to exclude properties which will be user-dependent and move those elsewhere. It would appear that the accepted advice for the “elsewhere” should be a copy of gradle.properties that each user can put in their own Gradle user home; as long as there are no duplicate properties defined, this should work, yes ? However, I can’t seem to read any properties from that user-local copy.

Here’s what I’ve tried so far.

I place the following property in my project’s copy of gradle.properties:

myProp="CatsMiaow"

At the end of my build.gradle.kts, I have the following code:

val myProp: String = providers.gradleProperty("myProp").get()
println("myProp: " + myProp)

Sure enough, when I execute my build, it prints out the value of ‘myProp’ as CatsMiaow.

I now create gradle.properties in my Gradle user home, i.e.

C:\Users\Gary\.gradle\gradle.properties

and give it just one line:

myProp="Dogs Bark"

I comment out the myProp property in my project’s gradle.properties, add a system environment variable for GRADLE_USER_HOME, and restart everything. I edit the previous build.gradle.kts to add some code to confirm my Gradle user home:

println(providers.environmentVariable("GRADLE_USER_HOME").get())
val myProp: String = providers.gradleProperty("myProp").get()
println("myProp: " + myProp)

When I run this, it correctly picks up my Gradle user home from the Windows environment. However, I get:

Cannot query the value of this provider because it has no value available.

The error disappears if I move the myProp property back to the main gradle.properties.

It appears that the user-local copy of gradle.properties is never being parsed OR I need to read its properties in a different way ? Please - what am I doing wrong ?

Thanks

Besides that the quotes in a properties file are wrong, unless you really want them as part of the value, what you do seems fine.

Besides that you do not need to set GRADLE_USER_HOME if you want the default location to be used which is .gradle/ in your USER_HOME.

Maybe make sure by using println(gradle.gradleUserHomeDir.absoluteFile) instead of just reading the environment variable what Gradle user home you have set. Using providers.gradleProperty is fine to get those properties from the user-specific file.

1 Like

Thank you ever so much !

The extra quotes were my fault when I posted my question: they are not really there in the properties files !

I knew about the default setting for GRADLE_USER_HOME but I wanted to try setting it explicitly because I couldn’t get it to work.

The problem was something in the end that I should have checked and completely forgot: my IntelliJ project setting for the Gradle user home was an old bespoke one which had an explicit (wrong) path. As soon as I restored it to the default user home, everything worked.

My apologies, but thanks again for telling me to print out the absolute path !

Gary

1 Like

Bjorn,

Can I ask one other thing ?

The approach of adding a gradle.properties to user home is fine, but if there’s a property inside there which is used by MANY projects that the user is going to work on, then that property will affect all such projects.

Instead, is there a way of having user-specific Gradle properties in a project so that they affect ONLY that one project but can still be separated out into a separate git-ignored file which is located inside the project hierarchy ?

Thanks

Can I ask one other thing ?

Sure, if you stop raping my name.
It is Björn or Bjoern, not Bjorn.

if there’s a property inside there which is used by MANY projects that the user is going to work on, then that property will affect all such projects.

Correct

is there a way of having user-specific Gradle properties in a project so that they affect ONLY that one project

No.
If you have own properties, you can name-space them with the project name for example.
But if it is about standard properties or properties of 3rd party plugins, no.

The feature request, though imho more a bug that it is missing, was at Add official local.properties support · Issue #12283 · gradle/gradle · GitHub but was denied. And any 3rd party plugin cannot help with that, as there is not yet any way to set a project property by code. You can only set extra properties which override project properties, but only if project.getProperty and friends are used. If providers.gradleProperty is used, those are not considered.

Understood, thank you.

Should have gone to Charset to produce the o with added diaeresis and I didn’t - that was lazy of me, and I owe you an apology. Sorry :face_with_open_eyes_and_hand_over_mouth:

1 Like