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 name="myproject" default="all" basedir=".">       <target name="all" depends="clean,fetch,build,test,docs,deploy">              ...       </target>       <target name="clean" >              ...       </target>       <target name="fetch" >              ...       </target>       <target name="build" depends="clean" >              ...       </target>       <target name="test" depends="build" >              ...       </target>       <target name="docs" depends="clean" >              ...       </target>       <target name="deploy" depends="build, test" >              ...       </target>       <target name="publish" 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 buildafter 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.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net