我有一个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>
在这个回购中,我已经部署了一个工件 - 实际上它是一个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.
...
Gradle无法找到my-gradle-plugin,即使我有上面的settings.xml文件指向远程maven存储库。
如果我在我的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文件,所以出了什么问题?
答案 0 :(得分:20)
有一个与此相关的开放票据有望实施:
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"])
}
}
}
答案 1 :(得分:3)
我使用maven-publish,maven-publish-auth插件来完成此操作而无需手动解析设置
How can Gradle Use Repository Settings From Maven's Settings.xml to publish artifacts
希望它有用。
彼得
答案 2 :(得分:3)
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 myRepo
的{{1}}个凭据访问https://intranet.foo.org/repo存储库
答案 3 :(得分:2)
请参阅: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()")
...
答案 4 :(得分:2)
请在build.gradle文件的存储库部分中使用mavenLocal()。这应该在您的主目录中读取〜/ .m2 / settings.xml文件。
repositories {
mavenCentral()
mavenLocal()
}