< Zurück | Inhalt | Weiter >

9.4.3 A Real, Live Buildfile

Let’s take it to the next level and examine a real working buildfile. Project Organization

All but the simplest of projects will require multiple classes. Some will require libraries, multiple programs, binary deployment, Web deployment, enterprise deployment, and so on. A project will be most successful if you plan out what goes where in advance. We’re going to present a series of suggestions for how to organize the files involved in developing, building, and deploying a Java project with ant. By no means is this the only way it might be done, but it has worked well for us. The build.xml File for Payback

Example 9.4 is the actual Ant buildfile for the Payback program in our source code examples. These examples are available on the book’s Web site.11


10. http://ant.apache.org/manual/

11. http://www.javalinuxbook.com/


Example 9.4 The build.xml file for the Payback application

<?xml version="1.0"?>


$Id: 070_antIntro.sgml,v 1.51 2004/04/13 05:10:45 mschwarz Exp $ Buildfile for the Payback program. Payback will calculate

the length of time and real amount of money it takes to make a purchase using various savings or credit accounts. -->

<project name="Payback" default="all" basedir=".">

<!-- The "init" target sets up properties used throughout the buildfile. -->

<target name="init" description="Sets build properties">

<echo>Running INIT</echo>

<property name="src" value="${basedir}/src"/>

<property name="build" value="${basedir}/build"/>

<property name="doc" value="${basedir}/doc"/>


<!-- The "all" target does nothing but tie together the "jar" and "doc" targets. -->

<target name="all" depends="jar,doc"

description="Pseudo-target that builds JAR and Javadoc">

<echo>Building ALL</echo>


<!-- The "build" target compiles the code in the project. -->

<target name="build" depends="init" description="Compiles the classes">

<echo>Running BUILD</echo>

<mkdir dir="${build}"/>

<javac destdir="${build}" srcdir="${src}" debug="true" deprecation="true"/>


<!-- The "doc" target generates Javadoc documentation of the project. The "author", "version", and "use" attributes set to true cause those Javadoc tags to be used in the final document. The "private" attribute set to true causes private methods and attributes to be included in the documentation. We tend to use this for projects to provide complete reference documentation. You would probably not want to do this for an app or lib distributed as a JAR file only. -->

<target name="doc" depends="init" description="Generates Javadoc documentation">

<echo>Running DOC</echo>

<mkdir dir="${doc}/api"/>

<javadoc packagenames="net.multitool.Payback.*" sourcepath="${src}" destdir="${doc}/api" author="true" version="true"

use="true" private="true"/>


<!-- The "jar" target depends on the "build" target. It places all of the class files in the project into a JAR file, and builds a manifest using the "manifest" tag. -->

<target name="jar" depends="build" description="Builds an application JAR file">

<echo>Running JAR</echo>

<jar basedir="${build}" jarfile="${basedir}/Payback.jar">


<attribute name="Version" value="1.0"/>

<attribute name="Main-Class"





<!-- The "run" target depends on the "jar" target. It executes the class named as the "Main-Class" in the JAR's manifest. -->

<target name="run" depends="jar" description="Runs the program">

<echo>Running RUN</echo>

<java jar="${basedir}/Payback.jar" fork="true">

<arg value="${basedir}/accounts.properties"/>



<!-- The "clean" target erases all files and directories that other Ant targets might have generated. It returns a copy of the project tree to a "pristine" (some might say "clean") state. -->

<target name="clean" depends="init"

description="Erase all generated files and dirs">

<echo>Running CLEAN</echo>

<delete dir="${build}" verbose="true"/>

<delete dir="${doc}/api" verbose="true"/>

<delete file="Payback.jar" verbose="true"/>





We’ve taken a very quick tour through the most popular tool for building and deploying Java applications. We’ve shown you how to install and set up ant. We’ve given you enough description of XML so that you can read an ant buildfile. We’ve touched briefly on the basic tags used in most buildfiles.


What we’ve covered here will probably explain most of the buildfiles you en- counter in the wild. It will probably also give you what you need to know to build most of the buildfiles you will have to build. But we have left out a fair amount.

You can write your own tasks. That’s a biggie right there. There are many built-in tasks that we didn’t cover. Look those up. They might be just what you need if you find yourself saying, “Boy, ant is nice, but I wish it could do X.” X might already be a built-in task. And if not, you can write it.

Ant has datatypes that can often be used in place of the simple strings al- lowed in task attributes. Tasks and datatypes are instances of Java base classes defined in the source code for ant. If you download the source, you can write your own classes that extend these base classes and you can thus add your own tags to ant.


The Ant User’s Manual 12 at the Ant project homepage13 (which itself is part of the Apache Project14) is the definitive resource for ant.


12. http://ant.apache.org/manual/

13. http://ant.apache.org/

14. http://apache.org/

• O’Reilly has also published Ant: The Definitive Guide by Jesse Tilly and Eric M. Burke (ISBN 0596001843), which was the first book on ant that we read.

• If you are into Extreme Programming you’ll want to check out Extreme Programming with Ant: Building and Deploying Java Applications with JSP, EJB, XSLT, XDoclet, and JUnit by Glenn Niemeyer and Jeremy Poteet, published by SAMS (ISBN 0672325624).