使用gradle构建时读取maven settings.xml? [英] Reading a maven settings.xml when building with gradle?
问题描述
我有一个maven settings.xml位于:
/home/u123/.m2/settings.xml
其中我指定了一个远程Maven仓库:
<?xml version =1.0encoding =UTF-8?>
< settings>
<个人资料>
<个人资料>
< id>预设< / id>
< repositories>
< repository>
< id> my.repo< / id>
< url> http:// myrepo /< / url>
< / repository>
< / repositories>
< / profile>
< / profiles>
< activeProfiles>
< activeProfile>预设< / activeProfile>
< / activeProfiles>
< / settings>
在这个回购中,我部署了一个工件 - 实际上它是一个gradle插件。
$ b
现在我尝试使用下面的build.gradle文件构建另一个需要使用此插件/工件的项目:
apply plugin:'java'
apply plugin:'maven'
buildscript {
dependencies {
classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
}
}
但构建失败:
...
>找不到组:com.test,模块:my-gradle-plugin,版本:1.0.0-SNAPSHOT。
...
Gradle无法找到我的gradle插件,即使我有上述settings.xml文件指向远程maven仓库。
如果我在我的build.gradle文件中指定存储库,它可以工作: p>
buildscript {
repositories {
maven {
urlhttp :// myrepo /
}
}
依赖关系{
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
但是作为一种解决方法,您可以在构建中使用一些groovy脚本.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输出。servers。server
}
def getCredentials = {
def entries = getMavenSettingsCredentials()
(条目中的条目){
)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])
}
}
}
I have a maven settings.xml located in:
/home/u123/.m2/settings.xml
where I specify a remote maven repository:
<?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>
In this repo I have deployed an artifact - actually its a gradle plugin.
Now I try to build another project that needs to use this plugin/artifact using the below build.gradle file:
apply plugin: 'java'
apply plugin: 'maven'
buildscript {
dependencies {
classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
}
}
But the build fails:
...
> Could not find group:com.test, module:my-gradle-plugin, version:1.0.0-SNAPSHOT.
...
Gradle cannot find my-gradle-plugin even though I have the above settings.xml file pointing to the remote maven repository.
If I instead specify the repository inside my build.gradle file it works:
buildscript {
repositories {
maven {
url "http://myrepo/"
}
}
dependencies {
classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
}
}
Based on this post:
http://forums.gradle.org/gradle/topics/have_mavenlocal_check_m2_home_conf_settings_xml
it seems that gradle considers the settings.xml file so what is wrong?
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"])
}
}
}
这篇关于使用gradle构建时读取maven settings.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!