Recipe 1.7 Automating Compilation with Ant


Problem

You get tired of typing javac and java commands.

Solution

Use the Ant program to direct your compilations.

Discussion

The intricacies of Makefiles 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 to 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 argument every time you run Ant. Example 1-1 shows the build script used to build the files in the starting directory. See Recipe 21.0 for a 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="../darwinsys.jar"/>   </target> </project>

When you run Ant, it produces a reasonable amount of notification as it goes :

$ 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

make is another build tool used in Unix and C/C++ development. make and Ant each have advantages; I'll try to stay neutral, although I admit I have been using make far longer than I have Ant.

Makefiles 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. However, Ant has the ability to run many Java tasks at once such as the built-in Java compiler, jar/war/tar/zip files, and many more to the extent that it may be more efficient to run several Java compilations in one Ant process than to run the same compilations using make. That is, once the JVM that is running Ant itself is up and running, it doesn't take long at all to run the Java compiler and run the compiled class. This is Java as it was meant to be!

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 Makefiles than Ant files. Non-Java developers have typically not heard of Ant; they almost all use make. Almost all 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. For example, make is used for telephone switch source code, which consists of hundreds of thousands of source files containing tens or hundreds of millions of lines of source code. By contrast, Tomcat 4 is about 340,000 lines of code, and the JBoss J2EE server about 560,000 lines. The use of Ant is growing steadily, particularly now that most of the widely used Java IDEs (JBuilder, Eclipse, NetBeans, and others) have interfaces to Ant. Effectively all 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 system but is included with many open source Java packages.

To sum up, although make and Ant are both good tools, new Java projects should use Ant.


See Also

The sidebar make Versus Ant;Ant: The Definitive Guide by Jesse E. Tilly and Eric M. Burke (O'Reilly).



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

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