This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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
Author

How to split generated classes into different jar

Adrien Ruffie
Ranch Hand

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

here is my problem:

I have one webapp with several packages like:

com.myapp.business
com.myapp.webservices
com.myapp.ui

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

Adrien


SCJP 5, SCDJWS 5
http://adrien-ruffie.blogspot.com
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2413
    
  28

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: 90
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}"/>

<jar
jarfile="${deploy.jars}core.jar"
basedir="${build.java}"
includes="com/myapp/*.class,
com/myapp/business/*.class
/>

<jar
jarfile="${deploy.jars}/controller.jar"
basedir="${build.java}"
includes="com/myapp/ui/**,
com/myapp/tags/**"
/>

<jar
jarfile="${deploy.jars}/webservices.jar"
basedir="${build.java}"
includes="com/myapp/webservices/*.class"
/>
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5838
    
    7

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: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html


JBoss In Action
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to split generated classes into different jar