Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package my classes in a .jar, and still package as a war?

Tags:

java

spring

maven

So I have a spring mvc web application that I built in IntelliJ 9, using maven.

When I run a:

mvn clean install

It packages everything up in both a .war file and an exploded war.

My project layout is using a maven simple web app arch type.

How can I have my classes compiled into a .jar file also?

Currently my classes are in:

/target/myapp/WEB-INF/classes/

What I want is for it to compile my code in .jar like:

myapp-1.0.jar

And that have this in my /target/myapp/WEB-INF/lib folder like all my other jars.

Update

I want this done via maven, not intellij specific.

like image 843
Blankman Avatar asked Jan 22 '12 16:01

Blankman


2 Answers

You can easily create two artifacts - a JAR and a WAR.

I'm not sure what the advantage is of having your .class files in a JAR as opposed to WEB-INF/classes. Both are compressed; the JAR won't provide an extra compression. You might want to be re-using those .class files in other applications. if that's the case, you might consider moving those Java classes into a separate module and adding the JAR file you create just as you would any other 3rd party JAR in your WAR file. Other applications would easily be able to check out the JAR file from your repository and simply reuse it. This arrangement is best done by moving the shared Java classes into a separate repository in your version control system.

like image 196
duffymo Avatar answered Oct 19 '22 02:10

duffymo


The simple solution is in the configuration for the war-plugin:

<attachClasses>true</attachClasses>

which will do the job and create a separate jar file. This jar has a classifier "classes" which can be changed by giving into the configuration. This can be useful if you have separated integration-tests which rely on classed from the war project to publish them and make them available as dependencies. This has nothing to do with separation of responsibilities.

like image 21
khmarbaise Avatar answered Oct 19 '22 03:10

khmarbaise