File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ant, Maven and Other Build Tools and the fly likes How to split generated classes into different jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to split generated classes into different jar" Watch "How to split generated classes into different jar" New topic

How to split generated classes into different jar

Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 92
Hello all,

here is my problem:

I have one webapp with several packages like:

I need to build my webapp and generated for example 3 jar differents in my lib like:

1] lib --> business-1.0.jar
2] lib --> webservices-3.4.jar
3] lib --> ui-1.7.jar

any one have an idea how I can generate my java classes into 3 differents jars ? I have look how to split sources with maven-jar-plugin, maven-assembly-plugin, archiver but I haven't found ...

Thank and best regards


Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

The easiest way to do this is to have a multi module project; one module for every jar. You war can depend on the 3 modules
Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 92
Yes I have found this but my application, cannot be splitted into different modules ... (I have already think to this solution but is not possible)

my mission is only to swtich the old ant following task on maven :

<target name="makeJars" depends="buildClasses"
description="Packages appropriate jar files">

<delete dir="${deploy.jars}"/>
<mkdir dir="${deploy.jars}"/>



Peter Johnson

Joined: May 14, 2008
Posts: 5852

One of the things about Maven is that it wants to do things its way and if you try to force it to do something different, you are asking for a lot of pain. In Maven, a single POM generates a single primary artifact (yes, you can generate ancillary artifacts, but they are not the main focus of the build - ancillary artifacts are things such as javadoc and source jars). Therefore, Jayesh's advice it the best advice.

If you really want to create a WAR for three JARs in the WEB-INF/lib directory, then create the JARs as separate projects (with their own POMs) and create the WAR in a fourth project (with its own POM) referencing the three JARs as dependencies. I suspect that the classes in 'ui' and 'webservices' reference classes in 'business'; that's OK, just add the dependency to the POMs.

If you cannot do this because the classes cross-reference each other (e.g., 'ui' references 'business' and 'business' references 'ui'), then you should not be placing the classes into separate JARs anyway.

Having said all of that, when I migrated a series of projects from Ant builds to Maven last year, one of the projects generated a primary JAR with all the classes and then a second JAR with only a handful of classes. Considering where that second JAR was later used as a dependency (where it made no sense to include the primary JAR), and considering that some of the classes were generated, I decided that a major refactoring was not feasible. So what I did to add the second JAR as an ancillary artifact was to included a second run of the maven-jar-plugin and I specified and includes list of the files to place in it. You can probably use this mechanism to accomplish your goal. Here is what your plugin definition might look like (I did only two of the jars, you can do the third):

I assume that your POM creates a WAR. If so, the maven-war-plugin runs during the 'package' phase and thus I set the maven-jar-plugin to run in the prior phase.

This is only part of the solution - it should generate the JARs for you. But getting those JARS into the WAR is another problem. I'll leave you to solve that. But you will definitely want to read very carefully the documentation on war:war:

JBoss In Action
I agree. Here's the link:
subject: How to split generated classes into different jar
It's not a secret anymore!