Skip to content

Instantly share code, notes, and snippets.

@p4535992
Created August 4, 2016 12:37
Show Gist options
  • Save p4535992/72141b88c2f4ce9f89909835176f6b0b to your computer and use it in GitHub Desktop.
Save p4535992/72141b88c2f4ce9f89909835176f6b0b to your computer and use it in GitHub Desktop.
Convert ivy.xml to build.gradle
/*Method 1*/
/* Original reference http://gradle.1045684.n5.nabble.com/converting-from-ant-ivy-to-gradle-td4408318.html */
task generateDependencies() << {
ivyFile = new File('ivy.xml')
gather = { dependencies, conf ->
dependencies.findAll( {
it['@conf'].toString().contains(conf.ivyConf) }).each {
String line = "\t${conf.gradleConf} group:
'${it['@org']}', name: '${it['@name']}', version: '${it['@rev']}'"
if(it['@org'].toString().contains('carfax'))
line += ", configuration: '${conf.ivyConf}'"
delegate << line
}
delegate << ''
}
confs = [ jar: [ivyConf: 'jar', gradleConf: 'compile' ],
test: [ivyConf: 'test', gradleConf: 'testCompile' ],
]
output = []
dependencies = new XmlSlurper().parseText(ivyFile.text).dependencies.dependency
output.metaClass.gather = gather.curry(dependencies)
output.gather confs.jar
output.gather confs.test
outputFile = new File('build.gradle')
outputFile.delete()
outputFile << 'dependencies {\n'
output.each { outputFile << "$it\n" }
outputFile << '}\n'
}
/*Method 2*/
/* Original reference https://gist.github.com/ethankhall/7370807 */
println("dependencies {")
def ivyModule = new XmlParser().parse(new File('ivy.xml'))
ivyModule.dependencies.dependency.each {
def scope = it.@conf?.contains("test") ? "testCompile" : "compile"
println("\t$scope '${it.@org}:${it.@name}:${it.@rev}'")
}
println("}")
/*Method 3*/
/* Original reference http://konstructcomputers.blogspot.it/2014/06/migrating-from-ant-and-ivy-to-gradle.html */
task convertIvyDeps << {
def ivyXml = new XmlParser().parse(new File("ivy.xml"))
println "dependencies {"
ivyXml.dependencies.dependency.each {
def scope = it.@conf?.contains("test") ? "testCompile" : "compile"
println("\t$scope \"${it.@org}:${it.@name}:${it.@rev}\"")
}
println "}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment