【问题标题】:Is there a "right way" to create skinny wars and an ear file in Gradle有没有“正确的方法”在 Gradle 中创建瘦战争和耳朵文件
【发布时间】:2011-06-13 22:26:53
【问题描述】:

使用 Gradle(显然还有 Java),我想生成一个精简的战争(战争中没有依赖项 jar),并将所有依赖项包含在 ear 文件中。我已经进行了一些挖掘,但我找不到任何明显的 Gradle 的“瘦身战争”教程。

在 Maven 2.2.1 中(是的,我们被困在 2.2.1 中,不要问我为什么),这是由 creating a separate ear project and repeating all of my dependencies from my sub-projects in the ear 完成的。

我可以想象如何在 Gradle 中复制同样的 hack,但我希望有更聪明的方法来做到这一点。有没有人有任何想法/示例/建议?

【问题讨论】:

    标签: war ear gradle


    【解决方案1】:

    gradle 的最新 1.0-M4 快照包含新的 ear 插件。您可以更新包装器配置或直接从

    下载快照

    http://repo.gradle.org/gradle/distributions/gradle-snapshots/gradle-1.0-milestone-4-20110610162713+0200-bin.zip

    此快照的完整分发版可在 http://repo.gradle.org/gradle/distributions/gradle-snapshots/gradle-1.0-milestone-4-20110610162713+0200-all.zip 下载,其中包含一个示例文件夹,其中包含新引入的 ear 插件的两个示例用法。

    查看 samples/ear/earWithWar 中的示例 此示例定义了带有战争的耳朵。

    要获得一个瘦战争,你必须修改战争项目的 build.gradle。要在没有第三方库的情况下进行战争,您必须将以下 sn-p 添加到 samples/ear/earWithWar/war/build.gradle:

    war{
        classpath = []
    }
    

    要获取您 ear 的 lib/ 子文件夹中的第三方库,您还必须将 war 插件使用的库添加到根项目中的 earlib 配置中。该示例为 log4j 库执行此操作。

    现在在示例项目上运行“gradle ear”会创建一个带有瘦战争的耳朵,并且第三方库存储在耳朵的 lib/ 子文件夹中。

    完整的快照在 docs/ 子文件夹中包含有关 ear 插件的更多文档

    【讨论】:

    • 酷!我在某处读到一个耳朵插件可能正在开发中。我会尽快获得新版本(不幸的是,这里有一个工作流程),然后检查一下。
    • 我已经设法试用了 EAR 插件,但它对我不起作用,因为 Maven 遗留了一个奇怪的项目结构(项目位于“空”子文件夹中)。一旦我有机会修复我们的文件夹结构,我会回到这一点。我想我还应该在 Gradle 论坛的某个地方留言,说明该插件不喜欢项目结构中的额外文件夹...
    • 好的,我又用了EAR插件,它确实有效,但它似乎仍然缺乏对“瘦身大战”的支持。希望它将作为里程碑 4 最终版本的一部分添加...
    【解决方案2】:

    按照 Rene 的建议设置 war { classpath = [] } 可以防止包含所有内容 - 最终存档甚至不包含 WEB-INF/classes。我知道从 war 文件的 WEB-INF/lib 目录中排除依赖项但仍包含 WEB-INF/classes 的唯一方法是使用 providedCompile 和/或 providedRuntime 配置。例如

    dependencies { providedCompile fileTree(dir: 'lib', include: '**/*.jar') }

    【讨论】:

    • 我通过添加这一行解决了这个问题:war.classpath = war.classpath.filter { !it.name.endsWith('.jar') }
    【解决方案3】:

    我将尝试着手构建该快照构建,但为了完整起见,这是我从昨天开始将所有东西组合在一起的方式。请随时改进我的 Gradle/Groovy。我敢肯定它没有想象中的那么优雅。

    //Make sure the war and jars get built first
    task ('ear', type:Jar, dependsOn: ":myWarProject:assemble" ){
        //This needs to be in the config block, or Gradle skips the task, assuming the file list for the jar/ear is empty...
        from{ "ear/src/main/application" }
    }
    
    ear.doFirst{
        //Set up the ear file name
        baseName = "myapp-" + rootVersion
        extension = "ear"
    
        //Gather up the jars required by all of the subprojects
        def allSubprojectDependencies = getAllProjectDependencies([
            "subproject1",
            "subproject2",
            "subproject3",
            "subproject4",
            "subproject5"
        ])
        from { allSubprojectDependencies }
    
        //grab the assembled war file
        from {
            subprojects.find{ it.name=="myWarFile" }.war.archivePath
        }
    
        //Other stuff required for our ear, such as security or eventing EJBs
        //Make sure you apply your "repositories" block to your root/ear project or "allProjects" if you do this...
        from { configurations.earConfig.files }
    
        //Create the classpath manifest
        manifestClassPath = allSubprojectDependencies.collect { it.name }.sort().join(' ')
        manifest { attributes( "Class-Path": manifestClassPath ) }
    }
    
    def getAllProjectDependencies (def projectNames){
        def allDependencies = []as Set
        projectNames.each{ projectName ->
            def subProject = subprojects.find{ subProject ->
                subProject.name.equals(projectName)
            }
            def subProjectDependencies = subProject.configurations.compile.files
            allDependencies.addAll subProjectDependencies
        }
        return allDependencies.unique{ a,b->
            if (a.equals(b)){
                return 0
            }
            return -1
        }
    }
    

    (请注意,所有的罐子都是故意放在耳根的。不要问我为什么,但有些人显然喜欢这样。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多