Section 1.3. Ant at Work

1.3. Ant at Work

Rather than go on and on about what Ant can do for you, an example can illustrate how easy Ant makes the build process. Assume that you have a Java file called, as shown in Example 1-1.

Example 1-1. A simple Java class
public class Project  {     public static void main(String args[])      {         System.out.println("No worries.");         } }

Assume you want to compile this code and store the results, Project.class, in a JAR file, Project.jar. With Ant and a build file, this is a piece of cake. By default, Ant looks for a build file named build.xml. That file is a valid XML document; Example 1-2 shows the build file for this example.

Example 1-2. A simple Ant build file
<?xml version="1.0" ?> <project default="main">     <target name="main" depends="compile, compress">         <echo>             Building the .jar file.         </echo>     </target>        <target name="compile">         <javac srcdir="."/>     </target>      <target name="compress">         <jar jarfile="Project.jar" basedir="." includes="*.class" />   </target> </project>

To run this Ant build file, make sure it's in the same directory as, and enter ant at the command-line prompt. Ant has been tested on many platforms, including Linux; Unix versions from Solaris to HP-UX; Windows 9x, NT, 2000, and XP; OS/2 Warp, Novell Netware 6, and MacOS X.

When you run Ant on this first build file, here's what you'd see in Unix (using the bash shell):

-bash-2.05b$ ant Buildfile: build.xml compile:     [javac] Compiling 1 source file compress:       [jar] Building jar: /home/httpd/vhosts/builder/Project.jar main:      [echo]       [echo]             Building the .jar file.      [echo]          BUILD SUCCESSFUL Total time: 2 seconds

You'll get the same results in any supported operating system. For example, here's what you'd see in Windowsverything except the build time is identical:

C:\ant\ch01>ant Buildfile: build.xml compile:     [javac] Compiling 1 source file compress:       [jar] Building jar: C:\ant\ch01\Project.jar main:      [echo]      [echo]             Building the .jar file.      [echo] BUILD SUCCESSFUL Total time: 4 seconds

For the most part, Ant builds are independent of operating system, and for that reason, % is used as a generic command prompt in this book. If anything is operating-system-dependent, it will be listed explicitly.

When Ant finishes executing the build file, you'll have build.xml,, the compiled Project.class, and Project.jar, all in the same directory. Project.jar will contain a manifest file and Project.class. Fortunately, Ant handles 10, 20, or 100 source files in this same way, making your life easy at build time.

    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: