Steve Loughran wrote an Ant guide called Ant in Anger that is part of the Ant download. This guide explains many pitfalls and recommends ways to use Ant. Two very useful suggestions are creating a list of names for targets and learning how to divide build files.
The following are some of Steve's recommended names for Ant top-level targets:
test--Runs the junit tests
clean--Cleans the output directories
deploy--Ships the JARs, WARs, and so on to the execution system
publish--Outputs the source and binaries to any distribution site
fetch--Gets the latest source from the Concurrent Versions System (CVS) tree
docs/javadocs--Outputs the documentation
all--Performs clean, fetch, build, test, docs, and deploy
main--Performs the default build process (usually build or build and test)
init --Initializes properties and performs other initialization tasks; read in per-user property files init-debug--Initializes debug properties
init-release--Initializes release properties
compile--Performs the actual compilation
link/jar--Makes the JARs or equivalent files
We'll discuss some of the concepts from Ant in Anger in this chapter. We strongly suggest that you read Ant in Anger, because it contains excellent guidelines for using Ant. The guide is included with the Ant binary distribution under the docs directory.
We'll now begin examining the techniques involved in using Ant to build and deploy Java applications. During the rest of this chapter, we build a "Hello World" example. Because the emphasis is on how to build components with Ant--and not the mechanics of implementing these various components--the example is meant to be as simple as possible. We package the application and the common JAR, construct a build file that creates an applet, and construct a master build file that coordinates the entire build.
The source code for the Hello World example is divided into several directories. Each directory has its own Ant build file, which contains instructions for compiling the components and packaging the binaries. Each directory also includes the requisite configuration files (such as manifest files). The master Ant build file, located in the root directory, calls the other build files and coordinates the entire build.
This divide-and-conquer technique for organizing components into separate directories is quite common with Ant. It may seem like overkill on a simple project like this one, but suppose you were building a system with 50 components. Each component has its own set of deployment descriptors and configuration files, and each component is deployed in different containers. The divide-and-conquer technique becomes necessary to manage the complexity--it also makes it easier to reuse components.
Here is the directory structure for the Hello World project:Model 2 Hello World root
+---Model
+---Application
+---Applet
+---WebApplication
The Model directory holds the common code (in this simple project, only the application will access the common code). The Application directory holds the Java application code, including the manifest file that marks the deployment JAR as executable. The Applet directory holds the applet code. The WebApplication directory holds the Web application code.