2012-09-21 65 views
11

我有位于一个maven的settings.xml:使用gradle构建时读取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-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这个考虑设置。 XML文件,所以什么是错的?

回答

15

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

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

但作为一个变通方法,您可以使用一些Groovy脚本中的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"]) 
    } 


    } 
} 
+0

这是一个伟大的回答,是一般信息有用,以及(在脚本建立摇篮文件)。我还建议任何人阅读下面的@ PeterKahn的答案,这不需要额外的脚本,但需要额外的插件。 – mnd

4

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

2

参见:https://github.com/home1-oss/maven-settings-decoder

我用它在gradle这个构建脚本来避免的build.gradle或环境变量中暴露的Nexus密码。

buildscript { 
    repositories { 
    ... 
    maven { url 'https://raw.github.com/home1-oss/maven-settings-decoder/mvn-repo/' } 
    } 
    dependencies { 
    ... 
    classpath 'cn.home1.tools:maven-settings-decoder:1.0.0.OSS-SNAPSHOT' 
    } 
} 
... 
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()") 
...