There is a similar question here: 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'.
Open gradle-wrapper. properties(go to Gradle > wrapper > gradle-wrapper. properties and manually change the distributionUrl property in the file.
The top-level build.gradle file, located in the root project directory, defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project.
The problem was that I made an assumption that wasn't true. If you look at section 14.2 of the gradle documentation, it says:
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.
My incorrect assumption was that USER_HOME just defaulted to the standard linux HOME environment variable. This is not true.
As soon as I export USER_HOME=$HOME
in my ~/.bashrc
everything works
By default, without setting GRADLE_USER_HOME
, it should work. I tested it in v3.5
.
But make sure if your are running it as the right user. For ex, if you do your ./gradlew build
using sudo
, then gradle.properties
in your home folder will not be picked up.
To make sure the default gradle user home, you can run gradle with the --debug
option and look out for the below line,
[DEBUG] [org.gradle.internal.buildevents.BuildLogger] Gradle user home:
A quick and dirty solution is just to simply put the gradle.properties right next to your build.gradle, that will guarantee it will be read. I realize this doesn't solve a single centralized source of common properties, but at least it's something.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With