Apache Ant: How To Include Multiple Jar Files In A Single Jar File

By Angsuman Chakraborty, Gaea News Network
Tuesday, August 19, 2008

Apache Ant is an excellent and popular Java based build system. It has several built-in commands (tasks in Ant lingo) one of which allows you to create Jar file from your existing class file and resources. What if you wanted to include not just class files but selective contents of other jar files too and make a single big jar file?

Ant provides an undocumented way to include the contents of multiple jar files within a single jar file.
Ant Jar task actually allows you to include multiple zipfileset as sub-elements. You can use zipfileset to include contents of other jar (and zip files) within your (big) jar file.

While zipfileset is documented as a sub-element of zip task, nowhere it is mentioned that it also works in a jar task.

An example of using zipfileset:

[jar destfile=”${build-abc}/abc.jar” duplicate=”fail” index=”true”>
[zipfileset src=”${compile-lib}/demo.jar” includes=”**/*.class”/>
[/jar>

Note: Replace [ with < as WordPress User Interface is buggy and doesn’t allow you to use < without immediately following it with a space!

Discussion
September 17, 2008: 10:34 am

Why be anonymous? Are you from Maven team?
I am told Maven2 still sucks and makes people less productive.

I would rather stick with simple and light-weight Ant, thank you very much. I don’t see any need for heavyweight (some would say bloated) software like Maven.


anonymous
September 16, 2008: 5:15 pm

are you people still living in the dark ages of ant… ant is good I have used it for quite a while…but give maven2 a go( i know maven1 sucked) and of course the tip is obviously clever and good…

August 20, 2008: 12:52 am

[...] url and accompanying text fits into their 140 character limit. For example I posted this article on how you can include multiple jar files in a single jar file in Ant build tool. Twitter automatically shortened the url to this meaningless jumble - https://tinyurl.com/5uclnr. So [...]

August 19, 2008: 1:43 pm

Thanks. Why do you think it is better to have multiple jar files when you need all of them? I much prefer to have all of them in a single jar with index.

A single jar file appeals to my sense of aesthetics. It is also more convenient to distribute, no more classpath issues, requirement to keep all jars in proper locations and so on. All you need to do is upload the jar and do a java -jar … :)

August 19, 2008: 12:46 pm

Good tip. Note that you should hardly ever have to copy classes from one jar into another. It is better practice to simply place the second jar onto the classpath.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :