219

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?

JJD
  • 50,076
  • 60
  • 203
  • 339
Lóránt Pintér
  • 10,152
  • 14
  • 47
  • 53

8 Answers8

290

~/.gradle/gradle.properties:

mavenUser=admin
mavenPassword=admin123

build.gradle:

...
authentication(userName: mavenUser, password: mavenPassword)
mkobit
  • 43,979
  • 12
  • 156
  • 150
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 4
    Should `gradle.properties` not be checked in to `VCS`? – theblang Sep 15 '14 at 19:43
  • 31
    Not the one in the Gradle user home (see path above). – Peter Niederwieser Sep 15 '14 at 21:34
  • For some versions of gradles... Use mavenPass instead of mavenPassword – Rodrigo Nov 04 '15 at 15:52
  • 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 '17 at 10:06
  • I had to do the same but to use 2 different environment variables:SONATYPE_NEXUS_USERNAME & SONATYPE_NEXUS_PASSWORD – Snicolas Jun 17 '17 at 17:29
  • 4
    is there a way to do this and avoid storing passwords as plain text in environment variable or text files? – Stealth Rabbi Jan 20 '21 at 12:16
  • `~/.gradle/gradle.properties` is nice, but what should I do if I work on multiple projects? Why not to keep secret properties in the project folder – Sneg Mar 09 '22 at 01:02
  • 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 '23 at 18:30
146

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)

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
questionaire
  • 2,475
  • 2
  • 14
  • 28
  • 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 '16 at 13:25
  • Sorry but I dont get your question – questionaire Nov 19 '16 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 '16 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. – questionaire Jan 03 '17 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 '18 at 16:10
  • does it possible to authorize via ssh? – Gorets Jun 13 '18 at 11:17
  • for people skimming through the answers to find answer.. create "gradle.properties" file in the project root directory (in the same folder as "build.gradle" file) – madhu_karnati Jan 10 '19 at 20:46
  • is there a way to change username and password with authenticated token? – hallz12 Feb 13 '20 at 04:17
  • How do i used " username "$mavenUser" for windows like " username "%mavenUser%" ? It's not working...get 403 error... – Nullpointer May 07 '20 at 06:15
  • Sorry but I have no idea wrt Windows. – questionaire May 20 '20 at 09:43
  • @Nullpointer the `$mavenUser` here refers not to an environment variable, but to a setting in the gradle.properties. So there is no reason to use a different syntax on Windows. – Paŭlo Ebermann Jun 02 '20 at 14:30
  • Thanks @questionaire! As supplementary info, when using the Kotlin DSL, we also have to declare `val mavenUser: String by settings` and `val mavenPassword: String by settings` (if the repository is declared in `settings.gradle.kts` ) or `val mavenUser: String by project` and `val mavenPassword: String by project` (if the repository is declared in `build.gradle.kts` ) – Svend Feb 17 '21 at 12:30
  • I configured for a private Nexus repo, it did not work under my Windows. – Hantsy Feb 26 '21 at 11:45
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.

toolforger
  • 754
  • 7
  • 22
Krishnaraj
  • 2,360
  • 1
  • 32
  • 55
  • Could you add a full working MWE. How to include the plugin in `build.gradle`. How to use credentials in the `uploadArchives` config? – koppor May 27 '20 at 13:47
  • @koppor Not sure if this still works but I have used it in my open source project - https://github.com/krishnaraj/oneclipboard/blob/master/build.gradle – Krishnaraj May 28 '20 at 17:23
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.

lucrussell
  • 5,032
  • 2
  • 33
  • 39
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']
JJD
  • 50,076
  • 60
  • 203
  • 339
David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • Thanks! However I had to set each parameter with props.getProperty("myParameterName") to make it work :) – DJTano Sep 21 '20 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/

Gabriel Kohen
  • 4,166
  • 4
  • 31
  • 46
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"
bk138
  • 3,033
  • 1
  • 34
  • 28
SergeyA
  • 4,427
  • 1
  • 22
  • 15
-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.

rinilnath
  • 136
  • 2
  • 15
  • Which file should OP put this code in? – darw Oct 19 '21 at 13:57
  • This snippet goes into build.gradle – rinilnath Oct 20 '21 at 16:13
  • Sorry, I meant original poster, a.k.a. the person who asked the question – darw Oct 20 '21 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 '21 at 16:38
  • 1
    Do you really store your credentials under the source control in your project files? – Ilya Serbis Dec 28 '21 at 20:41
  • The snippet can be altered with approved solution to take password as a variable from local gradle.properties. Main aim of my answer was to highlight the change of syntax in the gradle build file portion of pointing to repository from latest version of gradle – rinilnath Dec 30 '21 at 02:49