Set Gradle user home from init script

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 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:

    static private File findGradleUserHomeDir() {
        String gradleUserHome = System.getProperty(GRADLE_USER_HOME_PROPERTY_KEY);
        if (gradleUserHome == null) {
            gradleUserHome = System.getenv("GRADLE_USER_HOME");
            if (gradleUserHome == null) {
                gradleUserHome = DEFAULT_GRADLE_USER_HOME.getAbsolutePath();
        return canonicalize(new File(gradleUserHome));


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?

For context, the reason I want to do this is to work around the problem in Using Gradle in a computer cluster causes daemons to lock each other out.

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.

I can get what you mean by “change the Gradle code in your custom distribution”, but what do you mean by “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.