从ant + ivy迁移到gradle [英] Migrating from ant + ivy to gradle

查看:230
本文介绍了从ant + ivy迁移到gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于gradle的好东西,所以我印象深刻。很多人都很热衷于用gradle开发简单。他们都强调了gradle的多项目功能。



我已经有了一些包含一些子项目的主项目。外部依赖关系在所有项目自己的ivy.xml中定义。并且模块相互依赖性在eclipse的.classpath文件和ant build.xml中并行定义。这样我可以为IDE构建,并可以用ant创建运行时。是否有一种简单的方法将相同的构建结构从eclipse / ant和ivy移植到gradle(或gradle + ivy)?



构建过程很简单:


  1. 从ivy.xml解析子项目的依赖性
  2. 生成子项目
  3. (对所有子项目重复1和2)
  4. 解决主项目的依赖关系(包括ivy和eclipse / ant配置)
  5. 构建主项目

  6. 将所有内容打包成一个jar文件

不是火箭科学......但它花了很多天在蚂蚁实施它。
将它迁移到gradle是否真的很简单?如果是的话,什么是出发点?



我看到有一个eclipse插件,但我只能看到生成.classpath的可能性,而不是解析/读取它。我也看到一个 Ivy XML 插件。我不清楚如何处理依赖项目/模块?



欢迎任何帮助。



此致:
本周

解决方案

如果你没有很多自定义ant任务,只需要转换常春藤依赖关系,这会从你的ivy.xml创建gradle依赖关系。粘贴到ivy.xml旁边的空build.gradle中,并运行 gradle ivyToGradle 。然后将输出复制到您的build.gradle文件中。由于使用了compileOnly配置,因此需要gradle 2.12+。
$ b 这不会处理所有可能的ivy.xml设置,您可能需要针对您对ivy的特定使用进行调整。 XML。它处理诸如运行时配置,测试配置,仅编译配置,全局排除,传递真​​/假和版本强制等基础知识。

 任务ivyToGradle {
description将ivy.xml中的依赖关系转换为gradle依赖关系。
doLast {
def ivyModule = new XmlParser()。parse(new File($ projectDir / ivy.xml))
if(!ivyModule.dependencies.exclude.isEmpty()| |!ivyModule.dependencies.override.isEmpty()){
printlnconfigurations.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(,)
printlnexclude $ {excl}
}
}
def覆盖= ivyModule.dependencies.override.findResults {
if (确切!=它。@ matcher){
ret urn null
}
if(!it。@ org || !it。@ module || !它。@ rev){
返回null
}
返回'$ {it。@ org}:$ {it。@ module}:$ {it。@ rev}'
}
if(overrides){
printlnresolutionStrategy.force(
println overrides.join(,\\\

println)
}
println}
println
}
println(dependencies {)
ivyModule.dependencies.dependency.each {
def transitive =(false!= it。@ transitive)
def force =(true== it。@ force)
def scope =compileOnly//需要gradle 2.12或更高版本
if(it。@ conf?.contains(test)){$ b $ scope =testCompile
} else if(it。@ conf?.contains(runtime)) {
scope =compile
}
def hasBlock =!it.exclude.isEmpty()| | !传递||强制
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(,)
printlnexclude $ {excl}
}
}
if(!transitive){
printlntransitive = false
}
if(force){
printlnforce = true
} $ b $基点rintln}
} else {
println$ scope'$ {it。@ org}:$ {it。@ name}:$ {it。@ rev}'
}
}
println(})
println
}
}


I read many good things about gradle so I get impressed. Many people are enthusiastic how easy is to develop with gradle. They all emphasize the multi-project capabilies of gradle.

I already have a main project with some subprojects. The outside dependencies are defined in all the project's own ivy.xml. And the module interdependencies are defined in both eclipse's .classpath file and ant build.xml parallelly. This way i can build for IDE and can create a runtime with ant. Is there a simple way to migrate the same build structure from eclipse/ant and ivy to gradle (or gradle + ivy)?

The build process is simple:

  1. Resolve the subproject's dependencies from ivy.xml
  2. Build the subproject
  3. (repeat 1 and 2 for all the subprojects)
  4. Resolve the dependencies for main project (both from ivy and eclipse/ant config)
  5. Build main project
  6. Package everything into one jar file

Not rocket science... but it took many days to implement it in ant. Is that really a simple to migrate that to gradle? If yes, what are the starting points?

I see that there is an eclipse plugin, but I see only the possibility to generate .classpath, not to parse/read it. I see an Ivy XML plugin too. It is not clear for me how to handle dependent projects/modules?

Any help is welcome.

Regards: Bence

解决方案

If you don't have a lot of custom ant tasks and just need to convert your ivy dependencies, this will create gradle dependencies from your ivy.xml. Paste into an empty build.gradle alongside your ivy.xml and run gradle ivyToGradle. Then copy the output into your build.gradle file. Requires gradle 2.12+ due to use of the compileOnly configuration.

This will not handle all possible ivy.xml settings, you may need to tweak to your particular use of ivy.xml. It handles basics such as runtime configs, test configs, compile-only configs, global exclusions, transitive true/false, and version forcing.

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 ""
    }
}

这篇关于从ant + ivy迁移到gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆