如果你没有很多自定义的 ant 任务并且只需要转换你的 ivy 依赖项,这将从你的 ivy.xml 创建 gradle 依赖项。粘贴到 ivy.xml 旁边的空 build.gradle 并运行gradle ivyToGradle
。然后将输出复制到您的 build.gradle 文件中。由于使用 compileOnly 配置,需要 gradle 2.12+。
这不会处理所有可能的 ivy.xml 设置,您可能需要调整您对 ivy.xml 的特定使用。它处理诸如运行时配置、测试配置、仅编译配置、全局排除、传递真/假和版本强制等基础知识。
task ivyToGradle {
description "Converts dependencies in ivy.xml to gradle dependencies."
doLast {
def ivyModule = new XmlParser().parse(new File("$projectDir/ivy.xml"))
if (!ivyModule.dependencies.exclude.isEmpty() || !ivyModule.dependencies.override.isEmpty()) {
println "configurations.runtime {"
ivyModule.dependencies.exclude.each {
def excludeStrs = []
if (it.@org != null) {
excludeStrs.add("group: '${it.@org}'")
}
if (it.@module != null) {
excludeStrs.add("module: '${it.@module}'")
}
if (!excludeStrs.isEmpty()) {
def excl = excludeStrs.join(", ")
println " exclude ${excl}"
}
}
def overrides = ivyModule.dependencies.override.findResults {
if ("exact" != it.@matcher) {
return null
}
if (!it.@org || !it.@module || !it.@rev) {
return null
}
return " '${it.@org}:${it.@module}:${it.@rev}'"
}
if (overrides) {
println " resolutionStrategy.force("
println overrides.join(",\n")
println " )"
}
println "}"
println ""
}
println("dependencies {")
ivyModule.dependencies.dependency.each {
def transitive = ("false" != it.@transitive)
def force = ("true" == it.@force)
def scope = "compileOnly" // Requires gradle 2.12 or later
if (it.@conf?.contains("test")) {
scope = "testCompile"
} else if (it.@conf?.contains("runtime")) {
scope = "compile"
}
def hasBlock = !it.exclude.isEmpty() || !transitive || force
if (hasBlock) {
println " $scope('${it.@org}:${it.@name}:${it.@rev}') {"
it.exclude.each {
def excludeStrs = []
if (it.@org != null) {
excludeStrs.add("group: '${it.@org}'")
}
if (it.@module != null) {
excludeStrs.add("module: '${it.@module}'")
}
if (!excludeStrs.isEmpty()) {
def excl = excludeStrs.join(", ")
println " exclude ${excl}"
}
}
if (!transitive) {
println " transitive = false"
}
if (force) {
println " force = true"
}
println " }"
} else {
println " $scope '${it.@org}:${it.@name}:${it.@rev}'"
}
}
println("}")
println ""
}
}