What is ant and what can it do for you?

First, it's another neat tool. This tool, along with JUnit, is widely used within the Java development community, and is something you should at least look at if you plan to do any Java development in the future. It may save you a lot of time, even on small projects.

The ant utility is easily installed, and, once installed it should be available to you from the command line or from within whatever IDE you may be using, such as NetBeans or Eclipse. From the command line, you simply enter the antcommand at the command-line prompt. This assumes you have an XML file called build.xml (the default name) in the current directory (the default location), and this file contains properly formatted XML instructions to tell ant what you want it to do.

Here is a sample build file that you might place in a file called build.xml and use if you ...

Finally, here is a sample build file that you might use if you were in the early stages of developing a project to simulate the board game Mastermind:

<?xml version="1.0"?>

<!-- build.xml - An Ant buildfile for a Mastermind Project -->
<project name="Mastermind Buildfile"
         default="compile"
         basedir=".">

  <!-- The directory containing source code -->
  <property name="src.dir"
            value="src"/>

  <!-- Temporary build directories -->
  <property name="build.dir"
            value="build"/>
  <property name="build.classes"
            value="${build.dir}/classes"/>
  <property name="build.lib"
            value="${build.dir}/lib"/>

  <!-- Target to create the build directories prior to the -->
  <!-- compile target. -->
  <target name="prepare">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${build.classes}"/>
    <mkdir dir="${build.lib}"/>
  </target>

  <target name="clean"
          description="Remove all generated files.">
    <delete dir="${build.dir}"/>
  </target>

  <target name="compile"
          depends="prepare"
          description="Compiles all source code.">
    <javac srcdir="${src.dir}"
           destdir="${build.classes}"/>
  </target>

  <target name="jar"
          depends="compile"
          description="Generates mmconapp5.jar in the 'lib' directory.">
    <copy todir="${build.classes}">
         <fileset dir="src">
           <include name="*.txt"/>
         </fileset>
    </copy>
    <jar jarfile="${build.lib}/mmconapp5.jar"
         basedir="${build.classes}"
         manifest="${src.dir}/META-INF/MANIFEST.MF"
         excludes="**/*Test*.class"/>
        <!-- Exclude unit tests from the final JAR file -->
  </target>

  <target name="all"
          depends="clean,jar"
          description="Cleans, compiles, then builds the JAR file."/>

</project>

A sample buildfile called build.xml that you can study (and perhaps modify) and use to help save time when you are developing your Mastermind project has been placed on-line. It is available via the sample code page under Misc | Ant.