Where to put Gradle configuration (i.e. credentials) that should not be committed?

ID : 20238

viewed : 6

Tags : gradlegradle

Top 5 Answer for Where to put Gradle configuration (i.e. credentials) that should not be committed?

vote vote

91

~/.gradle/gradle.properties:

mavenUser=admin mavenPassword=admin123 

build.gradle:

... authentication(userName: mavenUser, password: mavenPassword) 
vote vote

84

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)

vote vote

72

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.

vote vote

70

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.

vote vote

53

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

Top 3 video Explaining Where to put Gradle configuration (i.e. credentials) that should not be committed?

Related QUESTION?