Ant Target for Creating JAR Files

Sometimes it comes in handy to have an ANT target for creating JAR files of your project, e.g. to be able to export it to some other location or project. The following code serves as reference to achieve this behavior. It consists of 6 basic steps:

  1. call compile target
  2. delete previous JAR version if it exists
  3. create JAR file
  4. create destination folder if it does not yet exist
  5. copy JAR to destination
  6. delete local version of JAR

Let’s see some XML

<target name="build-and-copy-to-tomcat-lib">
  <antcall target="compile" />

  <delete file="${plugin.name}.jar" />

  <jar destfile="${plugin.name}.jar"
          basedir="docroot/WEB-INF/classes"/>

  <mkdir dir="${app.server.deploy.dir}/../lib/ext/" />

  <copy file="${plugin.name}.jar" todir="${app.server.deploy.dir}/../lib/ext/"/>

  <delete file="${plugin.name}.jar" />
</target>

This example copies the JAR created to your Tomcat’s lib/ext folder so that it is accessible by other applications.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *