Site Meter Amund Tveit's Minimal Guide to Java-development using Jakarta Ant
 

Home
Publications
Resume
Blog
InternetPercent
Amund@Twitter
Google
Web amundtveit.info

A Minimal Guide to Java-development using Jakarta Ant

Author: Amund Tveit, ae@amundtveit.info

This is a guide to using the Jakarta Ant (Build Tool) for efficient java development. Ant replaces Make as the preferred tool for (large-scale) java development. The syntax of the configuration files is xml (i.e. build.xml) which makes it reasonably simple to understand and change.

My personal development environment is usually Cygwin on Windows, Emacs, CVS/SSH, and JDK (note: as of year 2001, at grad.school)

Prerequisites

Client(s)

  1. Linux, Windows* or another operating system that supports Java 2
(* Cygwin is recommended on the Windows platform)

Commands have the syntax devbox$ <command>.

Guide

Install Ant

Download ant from the Jakarta Project

Unpack it and install it, and remember to set some environment variables ($ANT_HOME and $JAVA_HOME), as well as $PATH and $CLASSPATH

On Unix:

  devbox$ export ANT_HOME="/usr/local/ant"
  devbox$ export JAVA_HOME="/usr/local/jdk"
  devbox$ export PATH="/usr/local/ant/bin:${PATH}"
  devbox$ for i in /usr/local/ant/lib/*; do export CLASSPATH="$i:${CLASSPATH}"; done

On Windows:

  devbox$ set ANT_HOME="c:/programs/ant"
  devbox$ set JAVA_HOME="c:/programs/jdk"
  devbox$ set PATH="c:/programs/ant/bin:%PATH%"
  devbox$ [add .jar-files found in c:/programs/ant/lib to CLASSPATH]

Create Initial build.xml file

Start your favorite XML-supporting editor (e.g. Emacs or Merlin) Let's say you want to create a project named YourProject Add the following to your build.xml file:
  <project name="YourProject" default="compile" basedir="." >

Adding aliases

Aliases or properties are names describing directories or other parts of your project. Let's say you want to put your javadoc in the directory release/YourProject/documentation/api you may want the property javadoc.home to refer to it, as shown below, the latter property classes.home specifies where class-files should be generated during compilation (see target compile below).
  <property name="javadoc.home" value="release/YourProject/documentation/api" />
  <property name="classes.home" value="release/YourProject/classes" />

Setting the class path

One of the first things that annoyed me with java was all the mess with classpaths. This can fortunately easily be avoided with ant, as shown below. (This example assumes that your .jar and .class files are stored in lib and classes, respectively)
  <path id="yourproject.classpath" >
    <fileset dir="lib">
      <include name="**/*.jar" />
    </fileset>  
    <pathelement location="classes" />
  </path>
The defined id yourproject.classpath can then be used in the classpath for javac, rmic or other programs requiring a classpath. Shown below for javac.
  <javac srcdir="src" destdir="classes">
    <classpath refid="yourproject.classpath"/>
  </javac>

Let's get to action

The most important tag in the build.xml file is <target .. >, this is similar to compile targets that you might be familiar for make and the corresponding Makefile. Examples of targets:
  1. Target prepare to set up directory/file structure for compilation
      <target name="prepare" >
        <mkdir dir="${classes.home}" />
        <mkdir dir="${javadoc.home}" />
      </target>
    
  2. Target compile to compile the source code, dependent on prepare for the needed directory structure
      <target name="compile" depends="prepare" >
        <javac srcdir="src" destdir="${classes.home}" debug="off" deprecation="on" />
      </target>
    
  3. Target javadoc to compile the javadoc code, dependent on prepare for the needed directory structure
      <target name="javadoc" depends="prepare" >
        <javadoc sourcepath="src" destdir="${javadoc.home}" />
      </target>
    
  4. Target clean to wipe out compiled class/jar-files
      <target name="clean">
        <delete dir="${classes.home}" />
        <delete dir="${javadoc.home}" />
      </target>
    
(In smaller projects, the five targets shown above may be sufficient) Now you should be able to compile files easily with ant, e.g.
 devbox$ ant compile

Academic/Educational Use of this Guide

  1. Mihhail Matskin and Hamid Reza Mizani. 2G1523 Programming Web Services (Home Work), IT University, Royal Institute of Technology, Sweden

Other Guides by Amund Tveit

Feedback

Your feedback is greatly appreciated, please take your time filling out the form below so I can continue to improve this minimal guide.
This guide was:
    Just what I was looking for!
    Ok..
    Crappy!
Your email address:

Your suggested improvements:

Originally written by Amund Tveit, 2001-09-25.

Follow @atbrox