eclipse - 从 ant + ivy 迁移到 gradle

标签 eclipse ant gradle ivy gradle-eclipse

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

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

构建过程很简单:

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

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

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

    欢迎任何帮助。

    问候:
    本斯

    最佳答案

    如果你没有很多自定义的 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 ""
        }
    }
    

    关于eclipse - 从 ant + ivy 迁移到 gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17323938/

    相关文章:

    java - 使用 GWT 和 Protege 在 Eclipse 上创建搜索应用程序

    php - 配置 Eclipse 以在工作区外部添加 buildpath 文件夹

    ant - 使用 Ant 提取嵌套的 zip 存档

    java - 为什么 .bat 文件会跳行并跳到末尾?

    java - 安装 Google App Engine 插件 Eclipse Indigo 时出错

    java - Eclipse中如何使用java.exe和javaw.exe?

    java - Apache tomcat 还是 apache ant?

    postgresql - Gradle Docker任务

    android - Android Gradle Build Variant Deploy

    android - 升级到Android Studio 3.0 : Unable to resolve dependency for ':app@debug/compileClasspath' : Could not resolve project :appLib