I would like to set the default Gradle user home from an init script to a different value. The exact value of the directory is a bit more complicated to compute, so it’s not just a matter of setting a simple entry in gradle.properties. I also already have a custom Gradle distribution that maintains other tweaks, so tweaking the value of the Gradle user home would fit very well here.
I tried the following: System.setProperty('gradle.user.home', '/some/machine/local/dir/my-user/.gradle'). This seems to be too late, as the value isn’t taken. I had a look in the Github repo and found the code responsible for setting the user home:
I’m guessing the code is already executed and system props, environment variables and command line arguments are already evaluated before the init scripts are executed.
Is there any other way to set the Gradle user home?
Probably just within the custom distribution.
Init scripts are too late, init scripts can also come from the Gradle user home.
So I guess you actually have to change the Gradle code in your custom distribution,
or inject it from outside.
For example setting GRADLE_USER_HOME environment variable in the machines where Gradle is run. This is actually more an environment specific setting, than a project or distribution specific setting.
W.r.t. “distribution specific”, I want to set it from the distribution to a value that is “safe” to use for the specific environment.
In the meantime, I’ll probably add a check in the custom distribution to at least warn users if they have the Gradle user home set to locations that can cause conflicts (e.g. in NFS mounted directories, like they user homes, etc.).
Yeah, as I said, you probably need to change the actual classes of Java to achieve that, or use something like AspectJ to weave some changes into the classes if you don’t want to rebuild the whole Gradle distribution.