Building with Ant
Apache Ant is a Java-based build tool. It helps automate the compile process. If you need to compile just a handful of files, or many files that are all in the same directory, javac will do. However, when the package structure becomes complex, a build tool such as Ant, modeled after Make, is helpful.
Ant has two compelling features: It is extended using Java classes, and the configuration files are XML-based. Therefore, you can write a program that is essentially a wrapper for Ant and have unparalleled control over the build process. On the other end, you can control the build with only simple changes to XML files.
CAUTION
Avoid including working or temporary files in your project submission. You will likely use test classes, backup source files, and tool configuration files. Make sure none of these files find their way to your evaluator's screen. Using Ant can ensure that only the essential files are "distributed" to the certification evaluator.
You can get the binary edition of Ant from the Ant Web page at http://ant.apache.org/. You must have a Java API for an XML Processing (JAXP)compliant XML parser installed on your classpath. With the SDK 1.4, you don't have to do anything because JAXP is included (see http://java.sun.com/xml/). After you install Ant (see http://ant.apache.org/manual/install.html#installing), you can start building with it.
Many popular Java Integrated Development Environments (IDEs) have Ant functionality. For example, JBuilder automatically recognizes Ant build files named build.xml. It even has an Ant wizard. Also, the Unix launch script that comes with Ant works correctly with Cygwin, but you might have to tweak things, such as Cygwin file paths.
Follow these three steps to get started with Ant:
Download the latest version.
Install Ant.
Read the manual.
After these steps, compiling an application with Ant is as simple as issuing this command:
ant
TIP
When compiling with Ant, you might need to tweak the build.xml before issuing the simple compiling command ant. An example is provided later in this chapter.
This simple command runs Ant using the build.xml file in the current directory, on the default target specified in the same build.xml configuration file. The following line shows how you will probably use it:
ant -buildfile scjd.xml mypackage
This command runs Ant using the scjd.xml file in the current directory on the target called mypackage.
TIP
Use Ant to automate the build task. Also, you can configure Ant to generate javadoc documentation and even place the whole project into a single JAR file, just as the instructions demand.
The configuration XML file, usually build.xml, is the key to using Ant. In this configuration file are the instructions Ant follows. A task is a discrete step that Ant performs. Basically, you place a sequence of tasks in the build.xml file. The following simple example (tested with Ant 1.4.1) compiles the *.java files in the source directory (subdirectory of the current directory) and places the generated *.class files in the distribution directory. Notice that a temporary build directory is also created. You can also add a task to delete temporary files.
<project name="SCJD" default="distribution" basedir="."> <description> practice SCJD build file </description> <property name="source" location="source"/> <property name="build" location="build"/> <property name="distribution" location="distribution"/> <target name="initialization"> <!-- command that assigns DSTAMP --> <tstamp/> <mkdir dir="${build}"/> </target> <target name="compile" depends="initialization" description="compile the source " > <!-- Compile *.java in ${source} and place *.class files into ${build} --> <javac srcdir="${source}" destdir="${build}"/> </target> <target name="distribution" depends="compile" description="generate the distribution" > <mkdir dir="${distribution}/library"/> <jar jarfile="${distribution}/scjd-${DSTAMP}.jar" basedir="${build}"/> </target>
</project>
These are the primary Ant command-line options:
-helpPrints out the help file.
-versionPrints the Ant version.
-diagnosticsPrints bug report.
-quiet, -qPrints minimal information.
-verbosePrints everything.
-debugPrints debug information.
-logfile, -lSpecifies the log file.
-buildfileNames the build file.
-propertyfileSpecifies using properties from this file.
Using Ant removes one burden by automating the build process. You can also include tasks for compiling the project, generating javadoc, and packaging all your project files into one JAR file, thus meeting another requirement along the way.
Because the configuration file is XML, several tags could be useful for your project. The following primary task "types" are the ones you are most likely to use with the Ant tool:
-
DescriptionDescribes the project.
-
DirSetDefines a group of directories.
-
FileListDefines an explicitly named list of files.
-
FileSetDefines a group of files.
-
File MappersTells Ant how to find a source file.
-
SelectorsSpecifies a way to select files from a FileSet using one of the following options:
-
<contains>Contains a particular string.
-
<date>Filters file by date.
-
<depend>Modified more recently than other files it "depends" on.
-
<depth>Files are x directories down in the tree.
-
<filename>Filters by filename.
-
<size>Filesare selected by size.
These types are used throughout the build.xml configuration file. If you want to speed up the build process for your certification project, I recommend installing and using Ant. It takes about an hour to download, install, configure and test. This is a good investment; it will save you many hours because you need to build your application numerous times before submitting it.