Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to combine multiple jars? Preferably using Ant

Tags:

java

jar

ant

I have runtime dependencies on some external jars that I would like to "rejar" into a single jar. These external dependencies are stored in an external_jars directory, and I'd like to be able to not have to list them all (i.e., not to need to change my build scripts if my dependencies change). Any thoughts?

Google gave me a good answer on how to do this - if you don't mind listing out each jar as a dependency:

http://markmail.org/message/zijbwm46maxzzoo5

Roughly, I want something along the lines of the following, which would combine all jars in the lib directory into out.jar (with some sane overwrite rules).

jar -combine -out out.jar -in lib/*.jar 
like image 405
Jacob Avatar asked Feb 05 '09 11:02

Jacob


People also ask

How do I make an ant jar?

jar an executable jar file, we need to add the manifest with the Main-Class meta attribute. To execute the jar task, wrap it inside a target, most commonly, the build or package target, and execute them. Running Ant on this file creates the util. jar file for us.


1 Answers

Vladimir's answer is a correct one, but I feel that what he suggests implies repacking all jars in a one big out.jar, which is then feeded to Ant Jar task as a single <zipfileset> or something like that. This two-step approach is unnecessary. I'm not sure whether this is connected with Ant version, but I have Ant 1.7.1, and its <jar> task understands <zipgroupfileset>, which allows to feed all contents of third party jars' directly.

<jar destfile="MyApplication.jar">   <zipgroupfileset dir="lib" includes="*.jar" />    <!-- other options -->   <manifest>     <attribute name="Main-Class" value="Main.MainClass" />   </manifest> </jar> 
like image 78
nightingale Avatar answered Sep 28 '22 16:09

nightingale