Tools

Ant

Apache Ant is a build tool that can be used to produce standalone build environments for (among other things) Java applications. Ant is preinstalled on many platforms (including the CS lab machines) and also very easy to install (just unzip and include tha ant bin directory in the PATH) using the binary distribution packages.

How Ant works

Ant is a platform-independent command-line tool that similarly to make uses a build file (called build.xml) to specify how to compile Java classes and assemble JAR files. The build format used by Ant is XML-based and specifies a number of parameters and actions for the build process. Below you will find a sample build file that allows you to set up an example Java project (zip) that can be compiled and built from command line. The example build file assumes the following file structure:

build.xml                          <-- Ant build file
lib/                               <-- lib directory (for JAR files)
src/                               <-- source code directory
    examples/
             ant/
                 HelloWorld.java   <-- example program

Using ant from the command-line is very straight-forward and follows the format ant target where target specifies the build process task to execute. Build process parameters such as default target, work and source directories, etc. can be specified in the build file, as well as dependency chains between targets that allow you to run several tasks using a single command (see example build file below). For example:

Using Ant to compile and build the example project in a JAR file (executing the default dist target):

ant

Using Ant to clean the example project / remove build files (executing the clean target):

ant clean

The example project also includes two shell scripts that demonstrates how to compile and build a JAR file for the project from the command line (using the default class in a JAR file, and (using a specific class path)

This simple combination of Ant and shell scripts often give you the majority of the tools you will need for building and executing Java programs from the command line. Note that Ant works seamlessly with Eclipse projects, just ensure that all Java classes are stored under the search path implied by the package structure of the classes and all classes will be located automatically.

More information on using Ant can be found in the Apache Ant manual as well on Wikipedia.

Sample Ant build file

Here is an annotated sample build file for Ant. Annotations in red explain parameters and build targets.

<project name="AntExample" default="dist" basedir=".">
  <description>
    Example of a simple Ant build script                        <-- project description (optional)
  </description>

  <!-- global properties -->
  <property name="lib" location="lib"/>                         <-- lib directory, place JARs here
  <property name="src" location="src"/>                         <-- source code directory, note package-wise structure
  <property name="build" location="build"/>                     <-- temporary build directory (automatically removed)
  <property name="jarfile" location="antexample.jar"/>          <-- JAR file produced
  <property name="mainclass" value="examples.ant.HelloWorld"/>  <-- default mainclass (for use with java -jar)

  <!-- classpath -->
  <path id="classpath.lib">                                     <-- build process class path
    <fileset dir="${lib}">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <target name="init">
    <!-- create time stamp -->
    <tstamp/>

    <!-- create build directory structure -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init" description="compile source">  <-- compilation target
    <!-- compile from ${src} into ${build} -->
    <javac includeantruntime="false" debug="true"
           srcdir="${src}" destdir="${build}">
      <compilerarg value="-Xlint:unchecked"/>
      <classpath refid="classpath.lib"/>
    </javac>

    <!-- delete test classes -->
<!--
    <delete>                                                           <-- deletion of static inner classes named Test
      <fileset dir="${build}" includes="**/*$Test.class"/>             <-- uncomment for use in, e.g., unit testing
    </delete>                                                          <-- (deleting unit test classes in deployment JARs)
-->
  </target>

  <target name="dist" depends="compile" description="generate distribution">   <-- JAR creation target (default)
    <jar jarfile="${jarfile}" basedir="${build}">
      <manifest>
        <attribute name="Main-Class" value="${mainclass}"/>
      </manifest>
    </jar>

    <delete dir="${build}"/>
  </target>

  <target name="clean" description="clean up">                                 <-- clean-up target
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete file="${jarfile}"/>
    <delete>
      <fileset dir="." includes="**/*.tmp"/>
    </delete>
  </target>
</project>