Using Ant


From here on out, I will be using an Ant script to do my compilations, now that I have more than two directories to compile. Ant is a platform-independent tool that allows you to create specifications of how your project should be built and deployed.

If you are using an IDE, you should be able to get it to build your entire codebase easily. Under Eclipse, for example, all of your source code is compiled automatically each time you save changes to Java source.

Regardless of whether or not you are using an IDE, you may want to use Ant in order to obtain IDE and platform independence. Other alternatives are to build a shell script or batch file as I demonstrated in Lesson 1. You can also use one of a number of available make tools. A make tool is a build tool very similar to Ant, but most make tools are very tightly bound to a specific operating system. Few make tools provide the ease of building Java applications that Ant does. Ant is the most effective way of doing builds in Java.

I highly recommend that you learn how to use Ant. Your IDE may suffice for your own personal needs, but it might not be sufficient in a team environment. If you work in a team environment, you will want a standardized way of building and deploying your application. Most development shops have standardized on Ant as a way of ensuring the system is built and deployed consistently and correctly.

See the sidebar "Getting Started with Ant" for a brief overview of using Ant.

Getting Started With Ant

This sidebar will help you obtain a basic understanding of how to work with the Ant build tool.

Most Java IDEs come with Ant support already built in. If you are not using an IDE, you can follow these steps to be able to use Ant for your builds.

  • Download the latest version of Ant from http://ant.apache.org.

  • Follow the instructions available with the Ant distribution to install Ant. Set the environment variable JAVA_HOME to the directory in which you installed the J2SE 5.0 SDK.

  • Update your system's path environment variable to include Ant's bin directory.

  • Create a build.xml file in the root directory of your project.

Regardless of whether you are using an IDE or not, you will put together a build.xml file that contains the instructions on how to compile, execute, and/or deploy your application.

Here is a starter build.xml for a project named agileJava.

[View full width]

<?xml version="1.0"?> <project name="agileJava" default="junitgui" basedir="."> <property name="junitJar" value="\junit3.8.1\junit.jar" /> <property name="src.dir" value="${basedir}\source" /> <property name="build.dir" value="${basedir}\classes" /> <path > <pathelement location="${junitJar}" /> <pathelement location="${build.dir}" /> </path> <target name="init"> <mkdir dir="${build.dir}" /> </target> <target name="build" depends="init" description="build all"> <javac srcdir="${src.dir}" destdir="${build.dir}" source="1.5" deprecation="on" debug="on" optimize="off" includes="**"> <classpath ref /> </javac> </target> <target name="junitgui" depends="build" description="run junit gui"> <java classname="junit.awtui.TestRunner" fork="yes"> <arg value="sis.AllTests" /> <classpath ref /> </java> </target> <target name="clean"> <delete dir="${build.dir}" /> </target> <target name="rebuildAll" depends="clean,build" description="rebuild all"/> </project>


Understanding the Sample Build File

Ant allows you to define in XML how to build various targets within a project. A target has a name and may have one or more other targets as dependencies:

 <target name="rebuildAll" depends="clean,build" /> 

The above line defines a target named rebuildAll. When you execute this target (keep reading), Ant first ensures that the targets named clean and build have been executed.

A target contains a list of commands, or tasks, to execute. The Ant software installation provides a manual describing a large number of tasks that will suffice for most of your needs. If you can't find an appropriate task, you can programmatically create your own.

The clean target contains the single task named delete. In this example, the delete task tells Ant to delete a file system directory with the name provided in quotes.

 <target name="clean">     <delete dir="${build.dir}" /> </target> 

Ant allows you to define properties that provide a construct similar to constants in Java. When the delete task executes, Ant will replace ${build.dir} with the value of the property named build.dir. The property named build.dir is declared in the agileJava Ant script as:

 <property name="build.dir" value="${basedir}\classes" /> 

This declaration sets the value of build.dir to ${basedir}\classes. The use of ${basedir} is in turn a reference to the property basedir, defined in the project element for the agileJava Ant script:

 <project name="agileJava" default="junitgui" basedir="."> 

(The . indicates that basedir is set to the current directorythe directory in which Ant is executed.)

When executing Ant, you can specify a target:

 ant rebuildAll 

The default attribute in the project element indicates the target to execute if none is specified. In this example, the junitgui target is the default and gets run if you execute ant with no arguments:

 ant 

A list of targets can be obtained by executing:

 ant -projecthelp 

This will show main targetsthose targets that specify a description attribute.

Using some built-in smarts, Ant executes tasks only when necessary. For example, if you execute the junitgui target, Ant will only run javac compiles against source that has not changed since the last time you executed junitgui. It uses the timestamps of the class files to make this determination.

To summarize the agileJava project, there are three main targets: build, junitgui, and rebuildAll. There are two subtargets, init and clean.

The build target depends on the init target, which ensures that the build output directory (./classes) exists. The build target compiles all sources in the source directory (./source) to the build output directory, using Ant's built-in javac task. The javac task specifies a number of attributes, including the attribute classpath, specified as a nested element of the javac task. The classpath attribute references a path element by the name classpath; this path element includes the JUnit jar file and the classes directory.

The junitgui target depends on the build target. If the build target succeeds, the junitgui target executes the JUnit GUI using the Java VM, passing in AllTests as the parameter.[7]

The rebuildAll target depends on execution of the clean target, which removes the build output directory, and on the build target.

Refer to the Ant manual for more detailed information. There are also several books available on Ant. One very comprehensive book is Java Development with Ant.[8]


[7] There is also an optional Ant task named junit that executes a text-based JUnit.

[8] [Hatcher2002].



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

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