gpt4 book ai didi

maven - 使用 gradle 构建时读取 Maven settings.xml?

转载 作者:行者123 更新时间:2023-12-02 02:26:48 25 4
gpt4 key购买 nike

我有一个 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>

在此存储库中,我部署了一个 Artifact - 实际上它是一个 gradle 插件。

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

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.
...

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

如果我在 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 文件,那么有什么问题吗?

最佳答案

有一个与此相关的开放票证有望实现:

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

但作为解决方法,您可以在 build.gradle 中使用一些常规脚本来实现此目的。就我而言,我需要来自 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"])
}


}
}

关于maven - 使用 gradle 构建时读取 Maven settings.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12522676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com