简体   繁体   English

如何使用Gradle将Jar中的所有文件包含到战争中

[英]How to include all files from jar into war using Gradle

I was trying plugins: 我正在尝试插件:

  • io.freefair.war-overlay - but it just overlay one war to another, io.freefair.war-overlay-但是它只是将一场战争叠加到另一场战争,
  • waroverlay - it has appropriate option "includeWarJars true" but it does't works for me. waroverlay-它具有适当的选项“ includeWarJars true”,但不适用于我。

Currently I'm trying write script: 目前,我正在尝试编写脚本:

dependencies {
    /* jar, his files I would like to include in war */
    compile project(':my_jar_1')
}

war {

    /* Step 1. Copy all from my_jar_1 into WEB-INF/classes */

    into('WEB-INF/classes') {
        from configurations.compile
                .filter { it.name.startsWith("my_jar_1") }
                .collect { zipTree(it).matching { exclude 'META-INF/**/*.*' }}
    }

    /* Step 2. Deleting jar from war WEB-INF/lib. I got stuck here. It println, but doesn't delete jar */

    doLast {
        zipTree('build/libs/war_1-0.0.0.war').files.each {
            if (it.path.endsWith('.jar')) {
                delete it
                println 'DELETED ' + it.path
            }
        }
    }
}

Could somebody tell me how to make it work? 有人可以告诉我如何使其工作吗? Or maybe smb know more elegant solution? 或者也许smb知道更优雅的解决方案?

Also I was trying to declare my own configuration 我也在尝试声明自己的配置

configurations { overlay }
dependencies { 
    overlay project(':my_jar_1') 
}
war {
        into('WEB-INF/classes') {
            from configurations.overlay
            ...

But it shows error 但它显示错误

FAILURE: Build failed with an exception. 失败:构建失败,发生异常。

  • What went wrong: Failed to capture snapshot of input files for task 'war' property 'rootSpec$1$1' during up-to-date check. 出了什么问题:在最新检查期间无法捕获任务'war'属性'rootSpec $ 1 $ 1'的输入文件的快照。

    Failed to create MD5 hash for file '/home/user/projects/OveralJarToWar/my_jar_1/build/libs/my_jar_1-1.0-SNAPSHOT.jar'. 无法为文件“ /home/user/projects/OveralJarToWar/my_jar_1/build/libs/my_jar_1-1.0-SNAPSHOT.jar”创建MD5哈希。

The content of WEB-INF/lib and WEB-INF/classes is configured by single property classpath of war task. WEB-INF/libWEB-INF/classes内容由war任务的单个属性classpath配置。 According to documentation: 根据文档:

Any JAR or ZIP files in this classpath are included in the WEB-INF/lib directory. 该类路径中的所有JAR或ZIP文件都包含在WEB-INF / lib目录中。 Any directories in this classpath are included in the WEB-INF/classes directory 该类路径中的所有目录都包含在WEB-INF / classes目录中

So, in your case, the classpath should be modified as follow 因此,在您的情况下,应将classpath修改如下

war {
    def myJar = project(':my_jar_1').jar.outputs
    def myClassesAndResources = project(':my_jar_1').sourceSets.main.output
    classpath = classpath - myJar + myClassesAndResources
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gradle:如何从 WAR 中排除 JAR? - Gradle: How to exclude JAR from a WAR? 如何仅包含来自 main/java 的特定文件作为 jar 的一部分发布到使用 gradle? - How to include only specific files from the main/java to be part of jar to be published to using gradle? 如何使用Gradle版本1.12将本地(.war .jar)文件发布(部署)到Artifactory? - How to Publish (deploy) local (.war .jar) files to Artifactory using Gradle version 1.12? Gradle 包含战争中另一个项目生成的 jar - Gradle include jar produced by another project in war Gradle Multiproject Build:如何在不创建伪项目的情况下将WAR项目的JAR包含到另一个WAR中 - Gradle Multiproject Build: How to include JAR of a WAR project into another WAR without creating pseudo projects 如何在Gradle中将文件从JAR复制到WAR? - How to copy files from JARs to WAR in Gradle? 如何使gradle包含war文件中的* .ftl文件 - How can I make gradle include *.ftl files in war file 如何在gradle jar任务生成的jar中包括运行时依赖项 - How to include runtime dependency in jar generated from gradle jar task 使用Gradle War Plugin时,如何使bootRepackage依赖jar而不是战争 - How to make bootRepackage depends on jar not war when using Gradle War Plugin Gradle从同一个项目创建war和jar - Gradle create war and jar from same project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM