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

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

问题描述

我读了很多关于 gradle 的好东西,所以我印象深刻.很多人都热衷于用 gradle 开发是多么容易.他们都强调gradle的多项目能力.

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.

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

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)?

构建过程很简单:

  1. 从ivy.xml解析子项目的依赖
  2. 构建子项目
  3. (对所有子项目重复 1 和 2)
  4. 解决主项目的依赖关系(来自ivy和eclipse/ant配置)
  5. 构建主项目
  6. 将所有内容打包成一个 jar 文件

不是火箭科学……但是在 ant 中实现它需要很多天.将其迁移到 gradle 真的很简单吗?如果是,起点是什么?

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?

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

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?

欢迎任何帮助.

问候:本斯

推荐答案

如果你没有很多自定义的 ant 任务并且只需要转换你的 ivy 依赖项,这将从你的 ivy.xml 创建 gradle 依赖项.粘贴到 ivy.xml 旁边的空 build.gradle 中,然后运行 ​​gradle ivyToGradle.然后将输出复制到您的 build.gradle 文件中.由于使用了 compileOnly 配置,因此需要 gradle 2.12+.

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.

这不会处理所有可能的 ivy.xml 设置,您可能需要调整您对 ivy.xml 的特定使用.它处理诸如运行时配置、测试配置、仅编译配置、全局排除、可传递真/假和版本强制等基础知识.

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天全站免登陆