简体   繁体   中英

Create a JAR containing classes and resources from webapp using Gradle

I would like to create a jar from the contents of a WAR using gradle. The result I want is quite like what the archiveClasses = true setting of maven-war-plugin does.

I understand that the gradle war plugin doesn't seem to do this (as per this stackoverflow question ).

Is there a way to do this manually? Say by manipulating the gradle jar task to collect the required parts from the WEB-INF folder?

When I just use the default jar task it doesn't get the resources from the WEB-INF directory.

The reason I want to do this is because I have modularised a WAR and the modules depend on common resources in the WAR (FreeMarker files in my case). I want to be able to test each module by depending on JAR created from the WAR.

I understand I could also create a "common" jar that held all the resources and have both the WAR and the module depend on this, but it would be more convenient to create a JAR form the WAR as per the maven-war-plugin.

So here is how I solved it (not sure if this is the best way):

jar {
    from ("${projectDir}/src/main/webapp/WEB-INF") {
        include('freemarker/**')
    }
}

I also found this alternative in the Gradle forums but I couldn't get the line resources.srcDir("src/main/webapp").exclude('images/') to compile, but I really like the idea of somehow just adding to the jar task's default resources directories. Edit : I think my first example above is as good as it gets.

You can add this to include both src/main/resources and scr/main/webapp in the root of the created jar file:

sourceSets {
    main {
        resources {
            srcDirs "src/main/resources", "src/main/webapp"
        }
    }
}

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