Ant

What Is Ant?

Ant is an XML-based build framework. It is the dominant tool for building and deploying Java applications. Of course you can use whatever tools you wish for this purpose: Shell scripts, make, etc... But since Ant is so ubiquitous, I think it is worthwhile to introduce it.

Installing Ant

When you install ant, make sure to set up the following environment variables:

Ant Build File

Ant uses build.xml in the current directory as the default build file. You can specify a different file/path using the '-f' option, e.g. ant -f C:\mybuild.xml. Below is an example ant build file for the simple Blackjack project (see blackjack.html).

<project name="Blackjack" default="dist" basedir=".">
    <description>
        Blackjack build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>
  <property name="classpath" location="."/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}">
        <manifest>
            <attribute name="Main-Class" value="BlackjackCommandLineRunner"/>
        </manifest>
    </jar>
  </target>

  <target name="run" depends="dist" description="Run the main code">
    <java jar="${dist}/lib/MyProject-${DSTAMP}.jar" fork="yes" dir="${basedir}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>

</project>