Harold L. Brown
Harold L. Brown

Reputation: 9956

Best practice in Gradle to configure Maven repositories globally

In Maven there is the settings.xml file in which I configure repositories (like Maven repository on a Sonatype Nexus server).

In my Gradle project the URL of the Maven repository is configured directly in my build.gradle file.

What is best practice in Gradle to configure repositories globally and outside the build file?

Upvotes: 3

Views: 1453

Answers (1)

lance-java
lance-java

Reputation: 27984

I put the following in %GRADLE_USER_HOME%/gradle.properties

nexus.user=somecoolguy
nexus.password=guessme

Then I use this snippet

allprojects {
    repositories {
        def repoUrls = [
            'https://mynexus:8081/nexus/content/groups/foo',
            'https://mynexus:8081/nexus/content/groups/bar',
            'https://mynexus:8081/nexus/content/groups/baz'
        }
        repoUrls.each { String repoUrl ->
            maven {
                url repoUrl
                credentials {
                    username project.properties['nexus.user']
                    password project.properties['nexus.password']
                }
            }
        }
    }
}

You could easliy turn the snippet into a custom plugin and perhaps improve it to

  1. Use an encrypted password
  2. Fail when project.hasProperty('nexus.user') returns false

Upvotes: 3

Related Questions