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

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'.

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:

I am using Windows and I was experiencing the same issue. I noticed that my ~/.gradle/gradle.properties file had a byte-order-marker (BOM) at the beginning and I think it had windows line endings (\r\n). I recreated my ~/.gradle/gradle.properties file by deleting it and then making a new one with vim with unix line endings (\n) with the same content as before and then it started working. I don't know if it was the BOM, line-endings, or the combination of the two that was causing the issue, but I thought I should share this in case someone else has the same cause for this issue.

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.

I did check Proxy Settings in Android Studio and i had No Proxy selected.
I did Invalidate Cache and Restart with no luck.
I added an exception in Firewall, still no luck.
In project gradle.properties i have no proxy configurations. Its empty, only comments.
In a previous version of Android Studio i had enabled Global Proxy Setting and it was a file stored in my user profile %userprofile%\.gradle\gradle.properties
Removing proxy lines in this file, solved my issue. I had to restart Android Studio to make it work.
:)

I myself stumbled upon this question when I was struggling with apk building via gradlew.
I tried setting Gradle_user_home earlier for the mentioned error in question, but it was of no use, below solution worked for me.
I have below configuration for gradle-wrapper.properties :
*distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip*
gradlew itself at run time downloads the correct distribution of gradle from above config (distributionUrl). Nothing else is required.

I faced the same issue and solved it by using gradle binary, to build, instead of wrapper that comes with android project. You can install gradle from here: https://docs.gradle.org/current/userguide/installation.html#installation
Then used system-wide gradle to build, while on project root, instead of wrapper, like ex:
$ gradle installDebug

I made a different mistake. Why did i set up this field in "intelligent idea"? :) However when i wrote correct value in this field I get correct reading gradle.properties from .gradle
some setting in Idea

Oh I noted that the "project property file does not exists. We continue!" is shown because the script will search for gradle properties in different routes.
it will check both in root/gradle.properties and in root/app/gradle.properties, so it will only add the ones from the one found and display the "error" from the other one.
However in the log you can actually see the properties that were added from the read files:
Adding project properties (if not overwritten by user properties): [android.nonTransitiveRClass, kotlin.code.style, org.gradle.jvmargs, android.useAndroidX]

Related

Vscode with custom settings.xml Maven file per project in a workspace

I have a Vscode workspace with a bunch of Maven projects, and which one has its own Maven settings.xml file.
As each project has its own settings file with the same name but in its own root folder I got success configuring both the workspace as each of the projects with such an option: {"maven.executable.options": "-s my_settings.xml"}.
Although, I can run successfully any of Maven commands for all projects, Vscode reports problems in the pom.xml files about missing artifacts and the same errors are reported when I run tests from the "Testing" in the Side Bar.
When I copy one of the project's settings file to the ~/.m2/settings.xml that given project works fine, no errors at all.
So, I conclude Vscode doesn't respect the "maven.executable.options" for all of project operations inside IDE, like tests and maybe others operations but only for directly launched Maven commands.
I don't want to merge all those settings.xml files into a big one in my local ~/.m2/settgins.xml each time I have to open a given set of projects.
Is there a way to teach Vscode to use different settings.xml for each project and make it work properly in all Vscode operations (not only Maven commands)?
VSCode setting ID: java.configuration.maven.userSettings
In VSCode, go to Preferences > Settings > Workspace
Search 'maven'
Set 'Java > Configuration > Maven: User Settings' to ./settings.xml to make VSCode use the local settings.xml of the project
Restart VSCode if needed
Screenshot
you can use
mvn --settings settings.xml clean install
to individually point respective settings.xml for each java app
First, check whether the terminal executes the command "mvn ... -s my_settings.xml", which indicates the correctness of vscode-maven plugin. If so, the problem comes from the command itself.
A workaround is to delete the global maven settings.xml, typically in $MAVEN_HOME\conf\settings.xml. Then mvn ... -s my_settings.xml should work. It works fine for me.
The reason may be that when global maven settings file is found, mvn CLI may ignore the settings file in mvn option.

Change default option of Intellij IDEA gradle plugin

Everytime I check out and open a project in Intellij I need to change the gradle plugin setting "Use Gradle from" from the default "'gradle-wrapper.properties' file" to "'wrapper' task in Gradle build script"
Is there a way in Intellij 2020 to make the shown option the default?
Reason I am asking: Generated gradle-wrapper files are not in our VCS, but the build.gradle is, and it has the wrapper task configured with the desired version. So when I check out a project, Intellij does not find gradle.properties (and neither can download gradle from web, as that is blocked). I then have to change the option "Use Gradle From" manually each time.
Another solution I tried is to automatically run a script when opening a module, which would run gradle wrapper with a fixed gradle version for the module, hence generating the missing wrapper files. But I find no possibility in Intellij to trigger that upon import..
It is not possible to change this setting for all projects. Please vote for this request: IDEA-215792.

Gradle - Can properties be placed in a settings.gradle.kts

Is it possible to place these settings which I currently have in gradle.properties in my settings.gradle.kts file?
org.gradle.parallel=true
org.gradle.caching=true
Thanks
No, this is not possible since gradle.properties configures the JVM that runs the Gradle build and settings.gradle.kts configures the project once the JVM has started and the build starts up. See the documentation on the build environment
In my experience you can't do it.
You can check the gradle properties in the official doc.
The configuration is applied in following order (if an option is configured in multiple locations the last one wins):
gradle.properties in project root directory.
gradle.properties in GRADLE_USER_HOME directory.
system properties, e.g. when -Dgradle.user.home is set on the command line.
These properties are used to sep up the environment for your build:
org.gradle.caching=(true,false)
org.gradle.caching.debug=(true,false)
org.gradle.configureondemand=(true,false)
org.gradle.console=(auto,plain,rich,verbose)
org.gradle.daemon=(true,false)
org.gradle.daemon.idletimeout=(# of idle millis)
org.gradle.debug=(true,false)
org.gradle.java.home=(path to JDK home)
org.gradle.jvmargs=(JVM arguments)
org.gradle.logging.level=(quiet,warn,lifecycle,info,debug)
org.gradle.parallel=(true,false)
org.gradle.warning.mode=(all,none,summary)
org.gradle.workers.max=(max # of worker processes)
org.gradle.priority=(low,normal)
Also you can apply the same rules to settings.gradle and settings.gradle.kts.In the documentation:
Gradle defines a settings file. The settings file is determined by Gradle via a naming convention. The default name for this file is settings.gradle.
The settings file is executed during the initialization phase.
And looking at the Settings class in the API documentation
There is a one-to-one correspondence between a Settings instance and a settings.gradle settings file.
You can check the properties that you can initialize with this file.

Maven downloading dependencies into project root instead of home/.m2

Whenever I run mvn in my project dir it always downloads the dependencies into that dir... I want it to download them into the main repo (home/.m2 )
After a quick google search : how-to-get-the-maven-local-repo.
Run mvn -X and check on the first the line the one that start with:
[DEBUG] Using local repository at *YOUR_LOCAL_REPOSITORY*
If it is not the folder you want, you can change the value in your global settings or your user settings.
[DEBUG] Reading global settings from *YOUR_GLOBAL_SETTINGS*
[DEBUG] Reading user settings from *YOUR_USER_SETTINGS*
To change the value you have to add or update the element "localRepository" in one or both of the above settings file.
<localRepository>/path/to/local/repo/</localRepository>
Maven - Guide to Configuring Maven

How to properly write a gradle-wrapper.properties file?

Can anyone please tell me if there is any advantage of gradlew over gradle?
Also I have some doubts on the gradle-wrapper.properties file.
Initially when I ran
C:\project_basedir>gradle wrapper
I found the following content in gradle-wrapper.properties file.
#Thu Jun 12 17:06:10 IST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip
To stop downloading the zip file I have followed the steps as suggested in chapter 61.1. Configuration of gradle documentation and added the gradle-1.12-bin.zip file from my local gradle installation to <Project_Base>\gradle\wrapper directory. and modified the properties files as follows:
#Thu Jun 12 17:06:10 IST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=gradle-1.12.zip
It saved the downloading time.
My Gradle installtion structure is as below:
C:\Users\sanjeev\Softwares\Gradle\gradle-1.12\bin
I have set system property GRADLE_USER_HOME to my local installtion dir i.e. C:\Users\sanjeev\Softwares\Gradle\gradle-1.12. But the distributionBase & distributionPath options are not picking the value.
I even tried to modify the values as:
distributionBase=file\:///c:/Users\sanjeev\Softwares\Gradle\gradle-1.12.zip
It did not work.
Exception in thread "main" java.lang.RuntimeException: Base: file:/C:/Users/sanjeev/Softwares/Gradle/gradle-1.12 is unknown
Can someone please suggest what am I missing here?
Or in a nutshell how to define distributionBase,distributionPath,zipStoreBase and zipStorePath properly?
The advantage of gradlew over gradle is that:
You don't have to have gradle installed on your machine in order to build your project.
You can control which version of gradle is needed to build your project.
I'm not sure of the point of changing the distribution url in the properties file. The whole point of the wrapper is that it downloads gradle. If you get it locally you might as well install and use gradle instead. The wrapper will only download the distribution once anyway and store it for each user (hence the use of GRADLE_USER_HOME).
GRADLE_USER_HOME is an environment property, not a system property. If you want to change the gradle user home using system properties you need to set 'gradle.user.home'.
The best way to get a proper gradle-wrapper.properties is to run the wrapper task using gradle wrapper. But that needs a local gradle installation. When editing the content of that file manually you need to be careful with escaping. Try this distributionUrl:
distributionUrl=file\://gradle/wrapper/gradle-1.12-bin.zip
One more comment: It might be easier to maintain if you put your gradle installation in a local bin repository (e.g in your nexus or artifactory repository) and let gradlew download the referenced gradle bin from there. The benefit is, that you just need to update the distributionURL.
If you're trying to change your wrapper distributionUrl to point at your local Gradle.zip file.
(Without spaces) Change it to file \ : ///C:\Users\sanjeev\Softwares\Gradle\gradle-1.12.zip (the filename gradle-1.12.zip here must be the same as how it is in your PC.)
Remove spaces as shown below, or it wont work :
distributionUrl= file\:///c:/Users\sanjeev\Softwares\Gradle\gradle-1.12.zip

Resources