Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a JAR containing classes and resources from webapp using Gradle

Tags:

java

gradle

jar

war

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.

like image 456
Carl Pritchett Avatar asked Jan 17 '14 00:01

Carl Pritchett


2 Answers

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.

like image 144
Carl Pritchett Avatar answered Sep 21 '22 04:09

Carl Pritchett


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"
        }
    }
}
like image 35
messnerized Avatar answered Sep 25 '22 04:09

messnerized