How to pack my classes in .jar and still pack like a war?

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

When I ran a:

mvn clean install 

It packs everything in a .war file as well as in a parsed war.

My project layout uses the arch type of the simplest maven web application.

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

My classes are currently in:

 /target/myapp/WEB-INF/classes/ 

I want it to compile my code in .jar, for example:

 myapp-1.0.jar 

And this is in my /target/myapp/WEB-INF/lib folder, like all my other banks.

Update

I want to do this via maven, not intellij specific.

+7
source share
6 answers

You can easily create two artifacts - JAR and WAR.

I'm not sure if the advantage is that your .class files are in JAR as opposed to WEB-INF / classes. Both are compressed; JAR will not provide additional compression. You might want to reuse these .class files in other applications. if this happens, you might consider porting these Java classes to a separate module and adding the JAR file that you are creating, just like any other third-party JAR in your WAR file. Other applications can easily check the JAR file from your repository and simply reuse it. This layout is best done by moving the generic Java classes to a separate repository in your version control system.

+2
source

It’s not at all clear how good it can be that the contents of a WAR will be packaged as a JAR. Code that implements webapp will not work without a WAR deployment context.

However, it’s very practical to think about how to extract the functional core of your code into a separate Maven module, which has JAR packaging and a good Java API. Then your WAR module may be dependent on this and should only contain code to turn this Java API into a web interface. The advantage of this is that it will also significantly improve the ability of your code to be tested (testing for JARs will certainly be easier) and will allow you to think in terms of having other clients of the same base code. This may interest you. Do you need any Java code directly in the WAR? Maybe, maybe not. There are several ways to do this in the end ...

0
source

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

 <attachClasses>true</attachClasses> 

which will complete the task and create a separate jar file. This jar has a classifier "classes", which can be changed by entering into the configuration. This can be useful if you split up integration tests that rely on classified ones from a military project to publish them and make them available as dependencies. This has nothing to do with segregation of duties.

0
source

Runninng

 mvn jar:jar install 

will make the jar as if you installed the type of packaging in the jar. Without any changes to your pom.

So, all you need to do is run this line separately, add it to the target script or add jar:jar to your existing maven assembly.

0
source

The type of packaging is indicated by the tag "packaging" in pom.xml (usually placed on top of the file along with the identifier of the artifact, version, etc. of the project.

Try to find

 <packaging>war</packaging> 

in pom.xml and change it to

 <packaging>jar</packaging> 
-2
source

ok write java command in console

jar cvf myapp-1.0.jar * .class

-2
source

All Articles