Automate your build process using Java and Ant

www.spiroprojects.com



A defined process is one of the most necessary but often least-used tools in software development. It is by nature an overhead task that accompanies a development effort. A defined build process ensures that the software in your development project is built in the exact same manner each time a build is executed. As the build process becomes more complex -- for example, with EJB builds or additional tasks -- it becomes more necessary to achieve such standardization. You should establish, document, and automate the exact series of steps as much as possible.

Why do I need a defined build process?
A defined build process is an essential part of any development cycle because it helps close the gap between the development, integration, test, and production environments. A build process alone will speed the migration of software from one environment to another. It also removes many issues related to compilation, classpath, or properties that cost many projects time and money.

What is Ant?
Ant is a platform-independent scripting tool that lets you construct your build scripts in much the same fashion as the "make" tool in C or C++. You can use a large number of built-in tasks in Ant without any customization. Some of the most important tasks are shown in the following table but explained in more detail in the example that follows.
Here are some useful commands that are built in the Ant distribution.

Command
Description
Ant
Used to execute another ant process from within the current one.
Copydir
Used to copy an entire directory.
Copyfile
Used to copy a single file.
Cvs
Handles packages/modules retrieved from a CVS repository.
Delete
Deletes either a single file or all files in a specified directory and its sub-directories.
Deltree
Deletes a directory with all its files and subdirectories.
Exec
Executes a system command. When the os attribute is specified, then the command is only executed when Ant is run on one of the specified operating systems.
Get
Gets a file from an URL.
Jar
Jars a set of files.
Java
Executes a Java class within the running (Ant) VM or forks another VM if specified.
Javac
Compiles a source tree within the running (Ant) VM.
Javadoc/Javadoc2
Generates code documentation using the javadoc tool.
Mkdir
Makes a directory.
Property
Sets a property (by name and value), or set of properties (from file or resource) in the project.
Rmic
Runs the rmic compiler for a certain class.
Tstamp
Sets the DSTAMP, TSTAMP, and TODAY properties in the current project.
Style
Processes a set of documents via XSLT.

While other tools are available for doing software builds, Ant is easy to use and can be mastered within minutes. In addition, Ant lets you create expanded functionality by extending some of its classes. I will show this expansion in a following example.

What do I need to use Ant?

You must install three components on your machine to run Ant: JDK, XML parser, and Ant (see Resources for links).
In many cases, the XML parser is part of the lib files distributed with the Servlet runner or the Web Server. If not, the free XML parser from java.sun.com is sufficient.
Ant installation consists of downloading the files, adding the class libraries to the classpath, and adding the Ant binaries to the path.

Example scenario

This example scenario should help show you the value of Ant and provide insight into its benefits and how you can use it.
Because a large amount of the current Java development is focused on server-side Java, I have chosen a server-side application for the example. Developers working on server-side Java applications are typically interested in the compilation of servlets, deployment of JSP files, and deployment of HTML files, configuration files, or images.
A common scheme for doing this build would involve the development of small scripts in platform-specific languages based on the server's operating system. For example, a developer working on an NT machine could create a batch file that performs the compilation tasks and then runs the deployment. However, if the production environment had Unix or Linux, the developer would have to rewrite the script, ensuring that the scripts were in sync.

OK, show me how this works

So, I've hopefully convinced you of the need to use Ant and shown how simple it is to install. Now I'll show you how simple Ant is to use by stepping through an example that performs simple compilation and deployment.


Can Ant do XYZ?

At this point, you have seen some of the possible tasks in your build process that Ant can automate. Those tasks are included out of the box in Ant. You might want to customize Ant to help you perform some more difficult tasks such as building EJBs and performing remote configuration management. Some of you may want to increase Ant's reporting capabilities or construct a user interface that can run the Ant process.
The simple answer to the question "Can Ant do XYZ?" is "Yes, but you may have to customize it."

Extending Ant

Two Ant extensions are interesting to discuss at this point. They are increased reporting and the ability to distribute code remotely using Ant.


 
Previous
Next Post »