What's a clean way to one-time export dependencies from a Maven2 pom.xml?


(Merlyn Albery-Speyer) #1

I have a single pom.xml file. It has a project.properties element defined with some dependency versions and otherwise just many project.dependencies.dependency elements, also some exclusions defined via project.dependencies.dependency.exclusions elements. I’m happy to ignore the rest of the pom.xml content.

I don’t need my Gradle build to stay in sync with the pom.xml definitions, a one-time export works for me. I’ve done this before for myself and written a quick Groovy script with an XmlSlurper to output Gradle configuration.

This time I’d like to do a demonstration for others, and would like to begin by quickly moving from Maven to Gradle for my example project. I’d like to do this in an automated way without doing anything exotic. What’s the most elegant way in your opinion?

PS Maven3 will accept the pom.xml I want to use, so I tried maven2gradle on it. It fails with the exact stacktrace listed in it’s FAQ - https://github.com/jbaruch/maven2gradle/wiki/FAQ


(Szczepan Faber) #2

I’m not aware of anything else but maven2gradle that you’ve already tried. Perhaps the https://github.com/jbaruch/Gradle-M2Metadata-Plugin plugin might help in some way.


(Merlyn Albery-Speyer) #3

I settled on this Groovy script. It’ll work fine for simple pom.xml files:

def project = new XmlSlurper().parse('pom.xml' as File)
def dependencies = project.dependencies.dependency
def compile = dependencies.findAll { it.scope?.text() != 'test' }
def testCompile = dependencies.findAll { it.scope?.text() == 'test' }
  def printDependency = { type, dep ->
 def identifier = "'${dep.groupId.text()}:${dep.artifactId.text()}:${dep.version.text()}'"
 if (!dep.exclusions.size()) {
  println "
$type $identifier"
 } else {
  println "
$type($identifier) {"
  dep.exclusions.each {
   println "
  exclude module:'${dep.groupId.text()}', artifact: '${dep.artifactId.text()}'"
  }
  println "
}"
 }
}
  println "dependencies {"
compile.each( printDependency.curry('compile') )
println()
testCompile.each( printDependency.curry('testCompile') )
println "}"