221

I'm trying to deploy a Gradle-built artifact to a Maven repo, and I need to specify credentials for that. This works fine for now:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://.../nexus/content/repositories/snapshots/") {
                authentication(userName: "admin", password: "admin123")
            }
        }
    }
}

But I don't like having to store the credentials in source control. With Maven, I would define a server configuration, and assign credentials in my ~/.m2/settings.xml. How do I do something similar with Gradle?

3
  • 4
    You know that using admin123 as password is bad from a security perspective, right ;)
    – James
    Sep 12, 2018 at 20:00
  • 1
    Also, posting passwords on stackoverflow would be bad, from a security perspective :-)
    – Brian
    Nov 11, 2021 at 1:39
  • 4
    I believe that the above id/pwd is just an example :D
    – Jiho
    Dec 1, 2021 at 3:55

8 Answers 8

291

~/.gradle/gradle.properties:

mavenUser=admin
mavenPassword=admin123

build.gradle:

...
authentication(userName: mavenUser, password: mavenPassword)
8
  • 4
    Should gradle.properties not be checked in to VCS?
    – theblang
    Sep 15, 2014 at 19:43
  • 31
    Not the one in the Gradle user home (see path above). Sep 15, 2014 at 21:34
  • 2
    I advise pass project properties as follows authentication(userName: project.properties.mavenUser, password: project.properties.mavenPassword) This will not fail a build when no properties mavenUser/Password are specified.
    – Dmitry
    Jan 19, 2017 at 10:06
  • 4
    is there a way to do this and avoid storing passwords as plain text in environment variable or text files? Jan 20, 2021 at 12:16
  • 1
    I needed to add the following code to my build.gradle file to include the gradle.properties in the .gradle directory. def passwordProperty = new Properties() def passwordPropertyFile = rootProject.file('.gradle/gradle.properties') if (passwordPropertyFile.exists()){ passwordPropertyFile.withReader('UTF-8') { reader -> passwordProperty.load(reader) } }
    – Harmen
    Jun 20 at 18:30
147

First answer is still valid, but the API has changed in the past. Since my edit there wasn't accepted I post it as separate answer.

The method authentication() is only used to provide the authentication method (e.g. Basic) but not any credentials.

You also shouldn't use it since it's printing the credentials plain on failure!

This his how it should look like in your build.gradle

    maven {
        credentials {
            username "$mavenUser"
            password "$mavenPassword"
        }
        url 'https://maven.yourcorp.net/'
   }

In gradle.properties in your userhome dir put:

mavenUser=admin
mavenPassword=admin123

Also ensure that the GRADLE_USER_HOME is set to ~/.gradle otherwise the properties file there won't be resolved.

See also:

https://docs.gradle.org/current/userguide/build_environment.html

and

https://docs.gradle.org/current/userguide/dependency_management.html (23.6.4.1)

13
  • Could you clarify how this would be used by the OP? i.e. where would it sit in the namespace uploadArchives { repositories { mavenDeployer {
    – Matt C
    Nov 19, 2016 at 13:25
  • Sorry but I dont get your question Nov 19, 2016 at 13:27
  • 5
    The OP uses authentication within the namespace uploadArchives > repositories > mavenDeployer > repository > authentication. They would still want to use uploadArchives I assume, so how would the OP's build config look with your solution applied? Do they need to remove authentication, and it will work?
    – Matt C
    Nov 19, 2016 at 13:32
  • Actually I cannot tell you since I never used mavenDeployer namespace. Uploadarchives is still a valid task but credentials for that task are configured within maven namespace. Jan 3, 2017 at 19:24
  • 1
    This answer is incompatible with the question. It's using the maven-publish plugin while the question's using the maven plugin.
    – Chry Cheng
    Mar 21, 2018 at 16:10
29

If you have user specific credentials ( i.e each developer might have different username/password ) then I would recommend using the gradle-properties-plugin.

  1. Put defaults in gradle.properties
  2. Each developer overrides with gradle-local.properties ( this should be git ignored ).

This is better than overriding using $USER_HOME/.gradle/gradle.properties because different projects might have same property names.

Note that the plugin actually adds gradle-${environment}.properties where the default for ${environment} is local, and has additional features. Read the link before using it.

2
21

You could also supply variables on the command line with -PmavenUser=user -PmavenPassword=password.

This might be useful you can't use a gradle.properties file for some reason. E.g. on a build server we're using Gradle with the -g option so that each build plan has it's own GRADLE_HOME.

12

You could put the credentials in a properties file and read it using something like this:

Properties props = new Properties() 
props.load(new FileInputStream("yourPath/credentials.properties")) 
project.setProperty('props', props)

Another approach is to define environment variables at the OS level and read them using:

System.getenv()['YOUR_ENV_VARIABLE']
1
  • Thanks! However I had to set each parameter with props.getProperty("myParameterName") to make it work :)
    – DJTano
    Sep 21, 2020 at 18:17
7

For those of you who are building on a MacOS, and don't like leaving your password in clear text on your machine, you can use the keychain tool to store the credentials and then inject it into the build. Credits go to Viktor Eriksson. https://pilloxa.gitlab.io/posts/safer-passwords-in-gradle/

0

build.gradle

apply from: "./build.gradle.local"
... 
authentication(userName: project.ext.mavenUserName, password: project.ext.mavenPassword)

build.gradle.local (git ignored)

project.ext.mavenUserName="admin"
project.ext.mavenPassword="admin123"
-5

As per the 7.1.1 gradle document, we have the syntax for setting repositories with credentials as below, the below code snippet should be in build.gradle file of the project

repositories {
maven {
    url "http://repo.mycompany.com"
    credentials {
        username "user"
        password "password"
    }
}

The above is to showcase the syntax changes in the gradle 7.x.x This can be altered to make password as a variable as shown in the approved solution above.

6
  • Which file should OP put this code in?
    – darw
    Oct 19, 2021 at 13:57
  • This snippet goes into build.gradle
    – rinilnath
    Oct 20, 2021 at 16:13
  • Sorry, I meant original poster, a.k.a. the person who asked the question
    – darw
    Oct 20, 2021 at 16:34
  • 1
    If I could just suggest one thing... if I were you, I'd consider editing the answer instead of replying with a comment. But in any case, thanks for the reaction
    – darw
    Oct 20, 2021 at 16:38
  • 1
    Do you really store your credentials under the source control in your project files? Dec 28, 2021 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.