是否有“正确的方法”在Gradle中创建瘦小的战争和耳朵文件

时间:2011-06-13 22:26:53

标签: war ear gradle

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

在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,但我希望有更聪明的方法来做到这一点。有没有人有任何想法/例子/建议?

3 个答案:

答案 0 :(得分:4)

最新的1.0-M4 gradle快照包含新的耳塞。您可以直接从

更新包装器配置或下载快照

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下载的此快照的完整发布包含一个示例文件夹,其中包含新引入的耳塞的两个示例用法。

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

要获得一场瘦小的战争,你必须修改war项目的build.gradle。要获得没有第三方库的战争,您必须将以下代码段添加到samples / ear / earWithWar / war / build.gradle:

war{
    classpath = []
}

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

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

我将尝试抓住这个快照构建,但是这是我从昨天以来为了完整而一起破解的方式。请随意改进我的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
    }
}

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