Reading a maven settings.xml when building with gradle?

There is an open ticket related to this that will be hopefully implemented:

http://issues.gradle.org/browse/GRADLE-2365

But as a workaround you can use some groovy scripting in the build.gradle to achieve this. In my case I needed the authentication information from settings.xml. But this could easily be adapted to get repository info.

Example:

def getMavenSettingsCredentials = {
    String userHome = System.getProperty( "user.home" );
    File mavenSettings = new File(userHome, ".m2/settings.xml")
    def xmlSlurper = new XmlSlurper()
    def output = xmlSlurper.parse(mavenSettings)
    return output."servers"."server"
}

def getCredentials = {
    def entries = getMavenSettingsCredentials()
    for (entry in entries) {
        if ( entry."id".text() == "my-server" ) {
            return [username: entry.username.text(), password: entry.password.text()]
    }
  }
}
uploadArchives {
def creds = getCredentials()
repositories.mavenDeployer {
    configuration = configurations.deployerJars
    repository(url: "http://my-release-repository/releases/") {
        authentication(userName: creds["username"], password: creds["password"])
    }
    snapshotRepository(url: "http://my-snapshot-repository/snapshots/") {
        authentication(userName: creds["username"], password: creds["password"])
    }


  }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)