~/.gradle/gradle.properties file not being read

There is a similar question here: http://stackoverflow.com/questions/26331397/gradle-properties-not-being-read-from-gradle-gradle-properties but it does not solve my problem.

It seems to me that gradle is NOT reading my ‘~/.gradle/gradle.properties’ file.

I have a gradle.properties file in ‘~/.gradle’, and it has properties needed to sign artifacts before uploading to maven central. It looks like this:

signing.keyId=12345678

signing.password=myPassword

signing.secretKeyRingFile=/home/me/.gnupg/secring.gpg

sonatypeUsername=me

sonatypePassword=myOtherPassword

When I try to build my project, it complains that there’s no sonatypeUsername property, thus:

Could not find property ‘sonatypeUsername’ on root project ‘yourProject’.

Here’s the relevant portion of my project’s build.gradle:

uploadArchives {

repositories {

mavenDeployer {

// lots of non-interesting things here

repository(url: “https://oss.sonatype.org/service/local/staging/deploy/maven2/”) {

authentication(userName: project.property(“sonatypeUsername”), password: project.property(“sonatypePassword”))

}

}

}

}

When I try to build the project with debugging, here’s what I see regarding properties:

$ ./gradlew --stacktrace --debug build

[INFO] [o.g.BuildLogger] Starting Build

[DEBUG] [o.g.BuildLogger] Gradle user home: /home/me

[DEBUG] [o.g.BuildLogger] Current dir: /home/me/dev/yourProject

[DEBUG] [o.g.BuildLogger] Settings file: null

[DEBUG] [o.g.BuildLogger] Build file: null

[DEBUG] [o.g.i.b.BuildSourceBuilder] Starting to build the build sources.

[DEBUG] [o.g.i.b.BuildSourceBuilder] Gradle source dir does not exist. We leave.

[DEBUG] [o.g.i.DefaultGradlePropertiesLoader] Found env project properties: []

[DEBUG] [o.g.i.DefaultGradlePropertiesLoader] Found system project properties: []

[DEBUG] [o.g.a.i.a.m.DefaultLocalMavenRepositoryLocator] No local repository in Settings file defined. Using default path: /home/me/.m2/repository

[DEBUG] [o.g.i.ScriptEvaluatingSettingsProcessor] Timing: Processing settings took: 0.286 secs

[INFO] [o.g.BuildLogger] Settings evaluated using empty settings script.

[DEBUG] [o.g.i.ProjectPropertySettingBuildLoader] Looking for project properties from: /home/me/dev/yourProject/gradle.properties

[DEBUG] [o.g.i.ProjectPropertySettingBuildLoader] project property file does not exists. We continue!

[INFO] [o.g.BuildLogger] Projects loaded. Root project using build file ‘/home/me/dev/yourProject/build.gradle’.

The problem is the following:

[DEBUG] [o.g.BuildLogger] Gradle user home: /home/me

Are you explicitly setting your gradle user home directory? Either via the CLI or an environment variable. It should be pointing to /home/me/.gradle.

Mark,

I am not setting it. According to section 14.2 of the gradle documentation,

You can place a gradle.properties file in the Gradle user home directory (defined by the “GRADLE_USER_HOME” environment variable, which if not set defaults to USER_HOME/.gradle) or in your project directory.

I am making the assumption that USER_HOME is the equivalent of HOME, but I do not have USER_HOME set either. Should I set USER_HOME to HOME in my .bashrc?

export USER_HOME=$HOME

Thanks for the response, please let me know what you think

USER_HOME in this case should be /home/me.

FYI - setting USER_HOME=$HOME fixes my problem. That raises the question why you don’t just use $HOME to begin with? If you did, things would have “just work”.

I think perhaps seeing the output of “env | grep -i gradle” from the shell where you run this build might help.

$ env | grep -i gradle

$

I’m not sure why it would be necessary to explicitly set the USER_HOME environment variable. Gradle is actually using the Java ‘user.home’ system property to make this determination. What is the output of the following?

System.props.get(‘user.home’)

using the groovy console, the output is ‘/home/me’

Is it the same if you remove the explicit USER_HOME environment variable?

Yes, still showing ‘/home/me’

Basically Gradle determines the gradle user home directory like so:

  1. Look for Java system property named ‘gradle.user.home’. If it exists, use that.

  2. Look for environment variable named ‘GRADLE_USER_HOME’. If it exists, use that.

  3. Otherwise set to default, which is value of Java ‘user.home’ property + /.gradle

I believe you. Still, I observed the behavior that I did. It started working for me when I exported USER_HOME=$HOME.

By the way… once I built it once successfully? I didn’t need to export it any longer. the export line is commented out in my ‘~/.bashrc’ and everything continues to build. How about that for a mystery?

When you change settings in your .bashrc you need to completely restart a new shell to run your tests. You should then review the resulting environment variable settings before you run the build. Also, checking the value of Java system properties really only makes sense inside your Gradle script, not in a separate groovy shell.

Sure. The important variable is whether or not USER_HOME has been set. Once I set it, I was able to build my project. As a test, I stopped setting it and rebooted. Now it builds without it being set.

I know this sounds like the moon has feet, but I can only report what I’ve observed. :frowning:

And what do you see in the console log next to “Gradle user home”?

‘10:09:05.424 [DEBUG] [org.gradle.BuildLogger] Gradle user home: /home/me/.gradle’

If you look up at my original post, you’ll see that my gradle user home had been ‘/home/me’, until I exported USER_HOME=$HOME, at which time, it became ‘/home/me/.gradle’, after which it is now always ‘/home/me/.gradle’ whether or not USER_HOME is set.

That is definitely odd. If you find a way to recreate the scenario please don’t hesitate to reply.