Reading a maven settings.xml when building with gradle?

前端 未结 6 2053
孤独总比滥情好
孤独总比滥情好 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:21

    You have to declare all repositories in your Gradle build script. settings.xml is only used to find the location of the local Maven repository, for example when resolving repositories { mavenLocal() }.

    0 讨论(0)
  • 2021-01-30 18:21

    See: https://github.com/ci-and-cd/maven-settings-decoder

    I use it in gradle build script to avoid expose nexus password in build.gradle or environment variable.

    buildscript {
      repositories {
        ...
        mavenCentral()
      }
      dependencies {
        ...
        classpath 'cn.home1.tools:maven-settings-decoder:1.0.5.OSS'
      }
    }
    ...
    ext.mavenSettings = new cn.home1.tools.maven.SettingsDecoder();
    ext.nexusSnapshotsUser = mavenSettings.getText("//server[id='${nexus}-snapshots']/username/text()")
    ext.nexusSnapshotsPass = mavenSettings.getText("//server[id='${nexus}-snapshots']/password/text()")
    println "${nexus}-snapshots username: " + mavenSettings.getText("//server[id='${nexus}-snapshots']/username/text()")
    println "${nexus}-snapshots password: " + mavenSettings.getText("//server[id='${nexus}-snapshots']/password/text()")
    ...
    
    0 讨论(0)
  • 2021-01-30 18:23

    I used maven-publish, maven-publish-auth plugins to accomplish this without parsing the settings by hand

    How can Gradle Use Repository Settings From Maven's Settings.xml to publish artifacts

    Hope it is of use.

    Peter

    0 讨论(0)
  • 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"])
        }
    
    
      }
    }
    
    0 讨论(0)
  • 2021-01-30 18:37

    gradle-maven-settings-plugin works for me (at least in my Windows environment)

    One should add

    buildscript {
        repositories {
            maven {
                url "https://plugins.gradle.org/m2/"
            }
        }
    }
    
    plugins {
        id 'net.linguica.maven-settings' version '0.5'
    }
    

    to the build.gradle and then can add repository like this:

    repositories {
        maven {
            name = 'myRepo' // should match <id>myRepo</id> of appropriate <server> in Maven's settings.xml
            url = 'https://intranet.foo.org/repo'
        }
    }
    

    which will use myRepo credentials from the Maven's settings.xml to access the https://intranet.foo.org/repo repository

    0 讨论(0)
  • 2021-01-30 18:48

    Please use mavenLocal() in your repositories section of build.gradle file. This should read ~/.m2/settings.xml file in your home directory.

    repositories {
        mavenCentral()
        mavenLocal()
    }
    
    0 讨论(0)
提交回复
热议问题