My project folder has a bunch of dependent jars in:
/lib/someapi-1.1.1/main.jar
/lib/someotherapi-2.2.2/api-2.2.2.jar
/lib/...
I build a JAR file and my application requires that the dependent jars get included in the final jar in the /lib folder within the jar, so the final jar should have a structure something like:
/org/me/myclasses.class
/lib/main.jar
/lib/api-2.2.2.jar
How do I get the /lib/*.jar files flattened an included in the /lib directory of my final jar file?
CLARIFICATION
I'm essentially just trying to get a set of resource files flattened and added to a given directory in my final jar.
JAR is a group of Java classes and known as Java Archive file. In Ant, we can create Jar files by using <jar> element in build. xml file.
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.
The Jar task checks whether you specified package information according to the versioning specification. Please note that the ZIP format allows multiple files of the same fully-qualified name to exist within a single archive.
You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.
In case you wanted do skip the copy step, you could do it this way. It uses <mappedresources>
to flatten from the source lib directories to the classes/lib area.
<jar destfile="${dist}/MyOneBigJar.jar">
<fileset dir="${classes}"/>
<mappedresources>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
<chainedmapper>
<flattenmapper />
<globmapper from="*" to="classes/lib/*" />
</chainedmapper>
</mappedresources>
</jar>
The easiest way I see is to copy allyour jars to a temporary folder using the copy
task and its flatten
attribute, and to include the jars of this temporary directory into the destination jar.
ADDED DETAIL added by asker
Here's what the final ANT target looks like (for future reference):
<target name="dist">
<mkdir dir="${classes}/lib"/>
<copy flatten="true" todir="${classes}/lib" includeemptydirs="false">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</copy>
<jar destfile="${dist}/MyOneBigJar.jar">
<fileset dir="${classes}"/>
</jar>
</target>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With