The intricacies of makefiles and their importabilities have led to the development of a pure-Java solution for automating the build process. Ant is free software; it is available in source form or ready-to-run from the Apache Foundation’s Jakarta project web site, at http://jakarta.apache.org/ant/. Like make, Ant uses a file or files -- written in XML -- listing what to do and, if necessary, how to do it. These rules are intended to be platform-independent, though you can of course write platform-specific recipes if necessary.
To use Ant you must create a 15-30 line file specifying various
options. This file should be called
build.xml
; if you call it anything else,
you’ll have to give a special command-line arguments every time
you run Ant. Example 1-1 shows the build script used
to build the files in the starting
directory.
See Section 21.1 for discussion of the XML syntax.
For now, note that the <!- - tag begins an
XML comment, which extends to the - -> tag.
Example 1-1. Ant example file (build.xml)
<project name="Java Cookbook Examples" default="compile" basedir="."> <!-- set global properties for this build --> <property name="src" value="."/> <property name="build" value="build"/> <!-- Specify the compiler to use. Using jikes is supported but requires rt.jar in classpath. --> <property name="build.compiler" value="modern"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <!-- specify what to compile. This builds everything --> <target name="compile" depends="init"> <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}" classpath="../com-darwinsys-util.jar"/> </target> </project>
When you run Ant, it produces a reasonable amount of notification as it goes, similar to make :
$ ant compile Buildfile: build.xml Project base dir set to: /home/ian/javasrc/starting Executing Target: init Executing Target: compile Compiling 19 source files to /home/ian/javasrc/starting/build Performing a Modern Compile Copying 22 support files to /home/ian/javasrc/starting/build Completed in 8 seconds $
Make Versus Ant
Both make and Ant have advantages and disadvantages, detractors and advocates. I’ll try to stay neutral, though I admit I have been using make for 15 years longer than I’ve been using Ant.
make files are shorter. No contest. make has its own language instead of using XML, so it can be a lot more terse. make runs faster; it’s written in C.
Ant files can do more. The javac task in Ant,
for example, automatically finds all the *.java
files in subdirectories. With make, a
sub-make is normally required. And the
include
directive for subdirectories differs
between GNU make and BSD
make.
Ant has special knowledge of CLASSPATH, making it easy to set a CLASSPATH in various ways for compile time. See the CLASSPATH setting in Example 1-1. You may have to duplicate this in other ways -- shell scripts or batch files -- for manually running or testing your application.
make is simpler to extend, but harder to do so portably. You can write a one-line make rule for getting a CVS archive from a remote site, but you may run into incompatibilities between GNU make, BSD make, etc. There is a built-in Ant task for getting an archive from CVS using Ant; it was written as a Java source file instead of just a series of command-line commands.
make has been around much longer. There are millions (literally) more make files than Ant files. Developers outside of Java have by and large not heard of Ant; they almost all use make. Most non-Java open source projects use make.
make is easier to start with. Ant’s advantages make more sense on larger projects. Yet of the two, only make has been used on the really large projects. Telephone switch source code consists of hundreds of thousands of source files containing tens or hundreds of millions of lines of source code. make is used here. The use of Ant is growing steadily, particularly now that most of the widely used Java IDEs (JBuilder, Visual Age for Java, NetBeans Forte, and others), have interfaces to Ant. Most Java open source projects use Ant.
make is included with most Unix and Unix-like systems and shipped with many Windows IDEs. Ant is not included with any operating systems but is included with many open source Java packages.
make has remained mostly compatible over its 20-year history. The Ant developers are planning to break backward compatibility after only a couple of years (in Version 2.0, due out later in 2001), though there is another tool, Amber, that will provide compatibility with Ant in addition to adding new features.
To sum up, make and Ant are both good tools. Use whichever one you choose in your own projects, but be prepared to use both in code you receive.
18.189.171.125