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

发布于 2024-11-15 01:13:55 字数 366 浏览 2 评论 0原文

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

在 Maven 2.2.1 中(是的,我们陷入了 2.2.1,不要问我为什么),这是由 创建一个单独的ear项目并重复ear中子项目的所有依赖项

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

Using Gradle (and Java, obviously), I'd like to generate a skinny war (with no dependency jars in the war), and include all of the dependencies in the ear file. I've done some digging, but I can't find any obvious "skinny war" tutorials for Gradle.

In Maven 2.2.1 (yeah, we're stuck in 2.2.1, don't ask me why), this was accomplished by creating a separate ear project and repeating all of my dependencies from my sub-projects in the ear.

I can imagine how I would duplicate this same hack in Gradle, but I'm hoping there's a smarter way to do this. Does anyone have any ideas/examples/suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-11-22 01:13:55

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

。拉链” rel="nofollow">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。要获得没有第三方库的战争,您必须将以下代码段添加到样本/ear/earWithWar/war/build.gradle:

war{
    classpath = []
}

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

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

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

the latest 1.0-M4 snapshot of gradle contains the new ear plugin. You can either update your wrapper configuration or download the snapshot directly from

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

The complete distribution of this snapshot downloadable at http://repo.gradle.org/gradle/distributions/gradle-snapshots/gradle-1.0-milestone-4-20110610162713+0200-all.zip contains a samples folder with two example usages of the newly introduced ear plugin.

Have a look at the example in samples/ear/earWithWar This example defines a ear with a war.

To get a skinny war, you have to modify the build.gradle of the war project. To get a war without thirdparty libs, you have to add the following snippet to samples/ear/earWithWar/war/build.gradle:

war{
    classpath = []
}

To get the thirdparty libs in the lib/ subfolder of your ear you have to add the lib used by the war plugin to the earlib configuration in the root project too. The example does this for the log4j library.

Now running "gradle ear" on the example project creates a ear with a skinny war and the thirdparty libs stored in the lib/ subfolder of the ear.

The complete snapshot contains further documentation in the docs/ subfolder about the ear plugin

烟凡古楼 2024-11-22 01:13:55

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

<代码>
依赖项{
提供的Compile fileTree(dir: 'lib', include: '**/*.jar')
}

Setting war { classpath = [] } as suggested by Rene prevents everything from being included - the final archive doesn't even contain WEB-INF/classes. The only way I know of to exclude dependencies from a war file's WEB-INF/lib dir but stil include WEB-INF/classes is to use providedCompile and/or providedRuntime configurations. For example


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

屋顶上的小猫咪 2024-11-22 01:13:55

我将尝试获得快照版本,但为了完整起见,以下是我从昨天开始将所有内容组合在一起的方式。请随时改进我的 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
    }
}

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

I'm going to try to get my hands on that snapshot build, but here's how I hacked things together since yesterday, just for the sake of completeness. Please feel free to improve my Gradle/Groovy. I'm sure it's not as elegant as it could be.

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

(Please note, all jars are in the root of the ear on purpose. Don't ask me why, but some people apparently like it that way.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文