Who’s On Phirst

Official blog of Phurnace Software.

Tag >> Embedded Ant

Posted by: Alexander Bibighaus on

Ant, the de-facto standard build tool for Java, could be defined as a platform independent scripting tool similar to Make but with an XML syntax. Ant is mature, extensible, and relatively easy to use. These qualities paved the path for Ant to be applied to other problems besides "building software". For instance, Ant is commonly used as a scripting tool to move files around, and moreover as a test tool to launch tests and generate reports. Most Java developers already know everything I just said.

I would like to share with you another way Ant can add value to your organization, embedded Ant. It is often the case where your software needs to do tasks similar to those available in Ant. For a long while, this was a cool thought, but not a good solution in practice. It required either launching Ant in another process or studying the Ant source code so you can work through some significant issues.

However, improvements in 1.6 and 1.7 now allow Ant to be easily embedded. I first successfully embedded Ant about six months ago with version 1.6. I considered the following points before making the decision to give it a try.

  • Ant is very mature.
  • The wide usage of Ant implies a well tested infrastructure
  • Ant tasks are cross platform
  • High probability anyone who inherits this code will know Ant

My product requirements for this feature were that it:

  • Must be able to fork a Java JRE on multiple OS's and run a process to completion (Windows, AIX, Linux).
  • Must be able to kill or stop the process on-demand.
  • Must be able to capture the standard output and standard error in real time.

Sure, I could use the Java IO and Runtime; however, when weighing the cost to write, debug, and test this code versus taking the time to figure out how to embed Ant, the decision was easy.

Here is a quick tutorial that demonstrates the concept. In practice, I use more code to make this easier. However, this shows the concept.

  • Create your build.xml and package it in jar.
 

<project name="embedded-ant-example" default="_init">
 
     <target name="sayHello">
 
           <echo>Hello ${to}</echo>
 
     </target>
   </project>
       
     

  • Create an Ant Wrapper

Project project = new Project();

project.init();

project.setBasedir(workingDir);

DefaultLogger antLogger = new DefaultLogger();
antLogger.setErrorPrintStream(System.err);
antLogger.setOutputPrintStream(System.out);
antLogger.setMessageOutputLevel(Project.MSG_INFO);

project.addBuildListener(antLogger);

// Load the build file
InputStream is = MyClass.getResourceAsStream(“build.xml”);
File tempFile = File.createTempFile();
copy(is, tempFile);

ProjectHelper.getProjectHelper().
parse(project, buildFile);

// set properties
project.setProperty("to", "World");

project.executeTarget("sayHello");


A few caveats still remain:

  • The project.init() is expensive. If possible, you only want to do this once per build file.
  • The ProjectHelper.getProjectHelper() method does not yet take an InputStream as an argument. The source code appears to allow you to write a plugin that would support this, but I took a simple approach. I read the resource from the jar and copy it to a temp file.

In summary, Ant has proved useful as an embedded library. However, what I noticed most was that Ant not only provided a robust solution, but also enabled other features that would have otherwise not been considered.

In Embedded AntAnt
Comment (1) Read More...