Reading a maven settings.xml when building with gradle?

前端 未结 6 2057
孤独总比滥情好
孤独总比滥情好 2021-01-30 18:21

I have a maven settings.xml located in:

 /home/u123/.m2/settings.xml

where I specify a remote maven repository:



        
6条回答
  •  野的像风
    2021-01-30 18:30

    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"])
        }
    
    
      }
    }
    

提交回复
热议问题