Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need an ANT task to include a set of jar files in the final jar's /lib dir

Tags:

java

jar

ant

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.

like image 589
David Parks Avatar asked May 05 '13 08:05

David Parks


People also ask

Which is used to create jar with all required classes in Apache Ant?

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.

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.

What is a jar task?

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.

How do I export a jar from an external library?

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'.


2 Answers

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>
like image 86
martin clayton Avatar answered Sep 25 '22 15:09

martin clayton


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>
like image 33
JB Nizet Avatar answered Sep 25 '22 15:09

JB Nizet