2012-09-21 41 views
11

Tôi có một settings.xml maven nằm ở:Đọc tệp settings.xml của maven khi xây dựng bằng gradle?

/home/u123/.m2/settings.xml 

nơi tôi chỉ định một kho maven từ xa:

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

Trong repo này tôi đã triển khai một artifact - thực sự là một Plugin gradle của nó.

Bây giờ tôi cố gắng xây dựng một dự án mà cần phải sử dụng plugin này/vật bằng cách sử dụng tập tin build.gradle dưới đây:

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

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

Nhưng xây dựng thất bại:

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

Gradle không thể tìm thấy tôi -gradle-plugin mặc dù tôi có tệp settings.xml ở trên trỏ đến kho lưu trữ maven từ xa.

Nếu tôi thay vì chỉ định kho bên nộp build.gradle tôi nó hoạt động:

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

Dựa trên bài đăng này:

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

có vẻ như gradle mà xem xét cài đặt. xml tập tin như vậy những gì là sai?

Trả lời

15

Có một vé mở liên quan đến này sẽ được thực hiện hy vọng:

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

Nhưng như một cách giải quyết bạn có thể sử dụng một số kịch bản hấp dẫn trong build.gradle để đạt được điều này. Trong trường hợp của tôi, tôi cần thông tin xác thực từ settings.xml. Nhưng điều này có thể dễ dàng được điều chỉnh để có được thông tin kho lưu trữ.

Ví dụ:

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

Đây là câu trả lời tuyệt vời và cũng hữu ích cho thông tin chung (kịch bản trong tệp xây dựng Gradle).Tôi cũng khuyên mọi người nên đọc câu hỏi này để xem câu trả lời của @ PeterKahn dưới đây, điều này không đòi hỏi thêm kịch bản, nhưng yêu cầu bổ sung thêm. – mnd

4

Bạn phải khai báo tất cả kho lưu trữ trong tập lệnh tạo Gradle của mình. settings.xml chỉ được sử dụng để tìm vị trí của kho lưu trữ Maven cục bộ, ví dụ: khi giải quyết repositories { mavenLocal() }.

2

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

tôi sử dụng nó trong gradle xây dựng kịch bản để tránh lộ mật khẩu mối quan hệ trong build.gradle hoặc biến môi trường.

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()") 
... 
Các vấn đề liên quan