Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a maven settings.xml when building with gradle?

Tags:

maven

gradle

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?

like image 781
u123 Avatar asked Sep 21 '12 00:09

u123


1 Answers

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"])     }     } } 
like image 85
CroesusKall Avatar answered Oct 01 '22 08:10

CroesusKall