ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Java Software Automation with Jakarta Ant

by Eugene Kuleshov and Dmitry Platonoff
07/24/2002

Jakarta Ant has become a widely accepted standard for Java project build automation. It is not only a tool, but also a very powerful language that allows you to describe complex build and deployment scenarios. At the same time, it is not a scripting language, but a process-oriented language. The convenient thing about Ant is that it's XML-based, so you can easily generate and edit its build files with lots of tools. And finally, Ant is also an open platform and a framework allowing you to plug in new functionality. All of these things make Ant more than suitable for the role of a general purpose automation tool.

If you would like to automate the following tasks, then there's a good chance that you should consider adding Ant support into your product:

  • Generate, compile, or package application code.
  • Create text, XML, RTF, or PDF files from templates, using XSL or FO.
  • Create zip, bzip, gzip, or tar archives.
  • Work with version control systems such as CVS, PVCS, Perforce, etc.
  • Execute commands on remote hosts via telnet or SSH.
  • Upload or download files through FTP, HTTP, HTTPS, and other Internet protocols.
  • Send email with MIME attachments.

All of these features can be used in any combination with each other. You can find the complete list of Ant tasks on the Jakarta Ant Web site. And if those are not enough, a customer can always add her own features by implementing a standard Ant task.

Execute Ant From Your Application

We assume that you are familar with Ant's Projects, Targets, and Properties, and that you know what the build file looks like. All of those things will be the same even if you're going to call Ant from your application. All you need is to complete the following steps:

  • Create and initialize an instance of the Project class.
  • Populate project and user properties.
  • Register a BuildListener to connect Ant to your GUI, or just for logging purposes.
  • Register an InputHandler to catch the input requests from Ant tasks.
  • Execute targets.

Let's take a look at each step in more detail. The key class in the Ant framework is the org.apache.tools.ant.Project class. You can create the entire Project structure by invoking the appropriate methods on this class; however, it's not the most convenient way to work with an Ant project. Instead, you can use a default ProjectHelper implementation to parse a good old build.xml file. The most attractive part of the Ant integration is the possibility of using standard and familiar build.xml files.

  import java.io.*;

  import org.apache.tools.ant.*;

  ...

  Project project = new Project();
  project.init();

  File buildFile = new File( baseDir, "build.xml");
  ProjectHelper.configureProject( project, buildFile);

You can pass property values to your Ant project. There are two types of properties. A user property cannot be overwritten or redefined within the build process. To specify it, you can use the following command:

project.setUserProperty( name, value);

A project property can be redefined within the build process. To populate these, use the following command:

project.setProperty( name, value);

For example, you can use the GUI to let the customer edit certain properties, or load a properties file from the hard drive, or obtain them from some other source.

  import java.io.*;
  import java.util.*;

  ...

  Properties prop = new Properties();
  prop.load( new FileInputStream( "build.properties"));
  for( Enumeration en = prop.keys(); en.hasMoreElements(); ) {
    String key = ( String) en.nextElement();
    project.setProperty( key, prop.getProperty( key));
  }

To be able to monitor what's happening during the build, you should register a BuildListener. There're a number of standard listeners available:

You can also provide your own implementation of the BuildListener interface to receive events when the build is started or finished, a target is started or finished, a task is started or finished, or when a message is logged.

  project.addBuildListener( new DefaultLogger()); 
  project.addBuildListener( new MailLogger()); 

In case a user interaction is required during the build process, Ant provides an InputHandler interface abstraction. With this, you can register one of the standard input handlers, such as the DefaultInputHandler, that outputs the prompt to the System.out and reads the input from System.in. Another handler is the PropertyFileInputHandler, which uses the prompt as a key to look up a value in the property file. The name of the file is read from the ant.input.properties system property.

  import org.apache.tools.ant.input.*;

  ...

  project.setInputHandler( new PropertyFileInputHandler());

Once you have a Project instance configured, you can execute any target defined in this particular build.xml file by using the target name:

project.executeTarget( targetName);

That's about it. Now we can look at some practical applications.

Pages: 1, 2, 3

Next Pagearrow