This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes merge multiple jars into one jar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "merge multiple jars into one jar?" Watch "merge multiple jars into one jar?" New topic
Author

merge multiple jars into one jar?

Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
I need to merge a couple jar files into one jar using Ant.

It is easy to do this for a fixed list of jarfiles, using <zipfileset>. The problem is, I want to do this in a generic way, and avoid hard-coding the names of the jar files.

Here is a section of my build file:



I have to specify the dependencies twice: Once for my build classpath, and once in the jar task. It would be nice to use the ${dependencies} property for both. Unfortunately I don't see how to make the <zipfileset> do that.

Thanks
Geoffrey


Sun Certified Programmer for the Java 2 Platform
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
I wouldn't recommend doing this. However to do this, you will have to either hard code the jar names in the mainfest file OR find a neat way to read the jar file names (using java.io) and build up the manifest file.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Why don't you recommend doing this? The obvious purpose is just to make a single application file (executable jar) that can run with "java -jar". My program has dependencies on some other jars (all open source). Merging everything into one jar seems to be the simplest way. As long as I obey the open source license, there aren't any legal issues.

I did not put the classpath in the Manifest file. I am not doing anything fancy with classloaders. The only thing I specified in the Manifest is the Main-Class.

Anyways, since I couldn't think of a way to do this using <zipfileset>, now I am expanding all the jars into a temp directory and then jarring it up again. This is a bit slower but it works.

Thanks
Geoffrey
Glenn Seg
Greenhorn

Joined: Sep 20, 2005
Posts: 2
I see that in your orignal approach (naming each file) and in the new approach, you are omitting the data in the META-INF directory. Actually, in the second way, the contents of the META-INF directory gets overwritten for each jar file that is expanded.

Are you planning to merge the manifest files in a future revision of the build file? In addition to having possible legal problems (dependent on the jar files you are using), you are losing data that may or may not be important. Do you think that not having the manifest information will cause problems?

The reason I ask is that I am doing the same thing and I can't afford for this to cause some unforseen problem.
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
http://one-jar.sourceforge.net/

This achieves what you specified as your aim. As to how you can do it in ant generically...

Well, you could wrap the ant build in a shell script and do.


And then do something with the foreach task from ant-contrib. Or keeping it all anty you just write your own task to do it. For instance :-



Compile errors are yours to fix
[ September 21, 2005: Message edited by: Lewin Chan ]

I have no java certifications. This makes me a bad programmer. Ignore my post.
Glenn Seg
Greenhorn

Joined: Sep 20, 2005
Posts: 2
Thanks...I will take a look at the link you posted. Also, there is a 'Manifest' class in the Ant jar file that has a merge method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: merge multiple jars into one jar?
 
Similar Threads
what is TStamp
ant exception
ANT Script Example for Websphere Application Server
Simple & Basic EJB application -- Help is needed
Emma is not producing coverage data