Posted on Leave a comment

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="${}.jar" />

  <jar destfile="${}.jar"

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

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

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

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