Simple Apache Ant script example

In one of my post I discussed about connecting to Postgres with Java. On that time in order to compile and run the app I used Bash script. Now in this post I would like to deprecate the script and use Ant (build.xml) instead of it.

Basically Ant is a software tool for automating software build processes. Ant runs the file called build.xml and it has structure similar to following,

<?xml version=1.0?>

<span class="pl-ent">xml</span><span class="pl-e"> version</span>=<span class="pl-s1"><span class="pl-pds">"</span>1.0<span class="pl-pds">"</span></span>?>"
<span class="pl-ent">xml</span><span class="pl-e"> version</span>=<span class="pl-s1"><span class="pl-pds">"</span>1.0<span class="pl-pds">"</span></span>?>
<span class="pl-ent">xml</span><span class="pl-e"> version</span>=<span class="pl-s1"><span class="pl-pds">"</span>1.0<span class="pl-pds">"</span></span>?>
<project name="MYPROJECT" default="main" basedir=".">
<!-- set global properties for this build -->
<property name="jar.name" value="MYPROJECT.jar"/>
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="build.deploy.dir" value="${build.dir}/deploy"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="lib.dir" value="lib"/>
<!-- define <span class="hiddenSpellError" pre="define " data-mce-bogus="1">classpath</span> -->
<path id="base.path">
<pathelement location="${build.classes.dir}" />
<pathelement location="${lib.dir}/postgresql-9.2-1002.jdbc4.jar"/>
<pathelement location="${lib.dir}/jsch-0.1.51.jar"/>
</path>
<target name="init">
<mkdir dir="${build.classes.dir}"/>
<mkdir dir="${build.deploy.dir}"/>
</target>
<!-- Compile the java code from ${src.dir} into ${build.classes.dir} -->
<target name="compile" depends="init">
<javac
srcdir="${src.dir}"
destdir="${build.classes.dir}"
debug="on"
deprecation="off"
optimize="on"
includes="*.java"
classpathref="base.path"
>
</javac>
</target>
<!-- Put everything in ${build.classes.dir} into the ${build.deploy.dir}/${jar}.jar file -->
<target name="jar" depends="compile">
<echo message="Compiling jar files"/>
<delete file="${build.deploy.dir}/${jar.name}"/>
<jar destfile="${build.deploy.dir}/${jar.name}" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="MYMAINCLASS"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
</jar>
</target>
<target name="main" depends="jar">
</target>
<!-- Cleans up the current ${build.dir} -->
<target name="clean">
<delete dir="${build.dir}}"/>
</target>
</project>

The following is a conventional Apache Ant structure. Those sections which I wrote with capital letters are most important. One is your project name, another one is the name of your jar file and the last one which is the most crucial one is your main class name. If you do not identify your main class name then the ant throws exception on the case that you have more than one class to compile.

You should also bear in mind of the library you want to include inside of your Jar file. I personally highly recommend this practice because after running Ant script, you would have a single and unify Jar file with all your project dependency which is very clean and you can deploy it without being worry of libs or anything else because all of your dependency libs are added in Jar file. You can introduce your libraries in pathelement section.

Send your idea and information to kasra@madadipouya.com

Leave a Reply