简体   繁体   中英

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,
  • waroverlay - it has appropriate option "includeWarJars true" but it does't works for me.

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?

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.

    Failed to create MD5 hash for file '/home/user/projects/OveralJarToWar/my_jar_1/build/libs/my_jar_1-1.0-SNAPSHOT.jar'.

The content of WEB-INF/lib and WEB-INF/classes is configured by single property classpath of war task. According to documentation:

Any JAR or ZIP files in this classpath are included in the WEB-INF/lib directory. Any directories in this classpath are included in the WEB-INF/classes directory

So, in your case, the classpath should be modified as follow

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM