【问题标题】:Reading a maven settings.xml when building with gradle?使用 gradle 构建时读取 maven settings.xml?
【发布时间】:2012-09-13 09:54:15
【问题描述】:

我有一个 maven settings.xml 位于:

 /home/u123/.m2/settings.xml

我在哪里指定一个远程 maven 存储库:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <profiles>
    <profile>
      <id>default</id>
      <repositories>
          <repository>
              <id>my.repo</id>
               <url>http://myrepo/ </url>
          </repository>
      </repositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>default</activeProfile>
  </activeProfiles>
</settings>

在这个 repo 中,我部署了一个工件 - 实际上它是一个 gradle 插件。

现在我尝试使用以下 build.gradle 文件构建另一个需要使用此插件/工件的项目:

apply plugin: 'java'
apply plugin: 'maven'

buildscript {
    dependencies {
        classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
    }
}

但是构建失败:

...
> Could not find group:com.test, module:my-gradle-plugin, version:1.0.0-SNAPSHOT.
...

即使上面的 settings.xml 文件指向远程 maven 存储库,Gradle 也找不到 my-gradle-plugin。

如果我改为指定存储库 inside 我的 build.gradle 文件,它可以工作:

buildscript {
    repositories {
        maven {
            url "http://myrepo/"
        }
    }
    dependencies {
        classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
    }
}

根据这篇文章:

http://forums.gradle.org/gradle/topics/have_mavenlocal_check_m2_home_conf_settings_xml

看来 gradle 考虑了 settings.xml 文件有什么问题?

【问题讨论】:

    标签: maven gradle


    【解决方案1】:

    有一张与此相关的公开票有望被实施:

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

    但作为一种解决方法,您可以在 build.gradle 中使用一些 groovy 脚本来实现这一点。在我的情况下,我需要来自 settings.xml 的身份验证信息。但这可以很容易地用于获取存储库信息。

    例子:

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

    【讨论】:

    • 这是一个很好的答案,对一般信息也很有帮助(Gradle 构建文件中的脚本)。我还建议任何阅读本文的人在下面查看@PeterKahn 的答案,它不需要额外的脚本,但确实需要额外的插件。
    • 延迟 4 年,票未关闭。我喜欢你的解决方案,不幸的是它似乎没有使用设置安全
    【解决方案2】:

    gradle-maven-settings-plugin 对我有用(至少在我的 Windows 环境中)

    应该添加

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

    build.gradle 然后可以像这样添加存储库:

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

    它将使用来自 Maven 的 settings.xmlmyRepo 凭据来访问 https://intranet.foo.org/repo 存储库

    【讨论】:

    • 如果您的 maven settings.xml 有身份验证详细信息,那么只需添加“net.linguica.maven-settings”即可解决问题
    【解决方案3】:

    您必须在 Gradle 构建脚本中声明所有存储库。 settings.xml仅用于查找本地Maven仓库的位置,例如解析repositories { mavenLocal() }时。

    【讨论】:

      【解决方案4】:

      我使用 maven-publish、maven-publish-auth 插件来完成此操作,而无需手动解析设置

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

      希望有用。

      彼得

      【讨论】:

      • 对我没用(可能是因为我在 Windows 下运行它,它希望设置文件的默认位置是~/.m2/settings.xml
      • 我认为此链接已损坏,它会将我带到discuss.gradle.org 没有关于setting.xml 的讨论
      【解决方案5】:

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

      我在 gradle 构建脚本中使用它以避免在 build.gradle 或环境变量中暴露 nexus 密码。

      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()")
      ...
      

      【讨论】:

        【解决方案6】:

        请在 build.gradle 文件的存储库部分中使用 mavenLocal()。这应该读取您主目录中的 ~/.m2/settings.xml 文件。

        repositories {
            mavenCentral()
            mavenLocal()
        }
        

        【讨论】:

        • 这对我不起作用,我必须将 maven url 显式复制到 gradle build 文件中。
        • 我不知道谁以及为什么赞成这个答案,因为它根本不起作用。 mavenLocal() 仅指 maven repo,而不是上面一个文件夹的 settings.xml 文件。
        • 这对我不起作用。我得到了同样的错误:Could not find &lt;dependency&gt;.
        猜你喜欢
        • 2014-05-29
        • 1970-01-01
        • 2014-05-16
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        • 2020-06-05
        • 1970-01-01
        • 2013-04-01
        相关资源
        最近更新 更多