JaVa
   

Basics of Using Ant

This section is a quick tutorial covering the basics of Ant. You will learn about projects, targets, properties, tasks, filesets, pathelements, and other key concepts. Upon completion of this chapter, you should understand Ant well enough to write your own Ant buildfiles.

Projects, Targets, and Tasks

Ant's build scripts, called buildfiles, are written in XML. Every buildfile contains one project element. A project element contains target elements. Each target consists of a set of task elements. A task performs a function such as copying a file, compiling a project, or creating a JAR file. A target is a collection of tasks and properties. A target can depend on other targets, meaning that a target does not execute until the targets it depends on are executed (for example, you would normally want to compile classes before you put them in a JAR). To indicate that a target depends on another target, you use the depends attribute. Thus, you may have something like the set of targets in the following listing (we left out the tasks associated with the targets; we'll cover them later). The project is a group of related targets. Although you can define any target you like, a set of standard naming conventions exists, as we discuss in the section “Standard Targets.”

<project default="all" basedir=".">
 <target depends="clean,fetch,build,test,docs,deploy">
 ...
 </target>
 <target >
 ...
 </target>
 <target >
 ...
 </target>
 <target depends="clean" >
 ...
 </target>
 <target depends="build" >
 ...
 </target>
 <target depends="clean" >
 ...
 </target>
 <target depends="build, test" >
 ...
 </target>
 <target depends="deploy" >
 ...
 </target>
</project>


This listing contains a build target that could, for example, compile the source and create JAR and WAR files. Notice that the build target depends on the execution of the tasks associated with the clean target (the tasks are not shown). The test target in turn depends on the build target. The order of the dependencies expressed with the target's “depend” attribute in this example is quite logical. For example, you can't test the code if it does not build—after the code is built, it's tested.

You can give targets any name that you like. However, people generally use common Ant names to create buildfiles, as discussed in the next section.


JaVa
   
Comments