Thursday, October 20, 2011

Ant aint just XML

So, after having used rake a few times, I jumped head first into the Java build/deploy tool called Ant. It came out of the Apache project and was originally a custom build tool for Tomcat.

Put simply, Ant is Xml. A lot like Xslt (which it itself Xml), an Ant file embodies tasks that are meant to be executed. These tasks are executed in Java or in the language of the underlying shell. Like Xslt, and unlike Html, Ant seems to be Turing complete, meaning you theoretically can use it to perform whatever calculations you want.

Unlike Xslt though, Ant is partly a declarative language that lets the programmer specify tasks and dependencies, and figures out how to execute those tasks without violating the dependencies. In other words, with some of its features (like dependencies), it lets you specify the what, instead of specifying the how. Other features ares plainly procedural and are just code in Xml clothing: for example the tasks to create, and delete directories, and to compile and run code.

It takes a bit of getting used to the declarative programming paradigm, but it does save a bit of work to just declare stuff and let Ant figure out how to execute it. In particular, the dependency information is specified declaratively, and Ant figures out the dependency graph, makes sure there are no cycles in it, and finds a serialization to execute it.

There were no major surprises for me, except for the fact that even Ant couldn't prevent me from wasting 5-10 minutes of my time figuring out a ClassNotFound exception. I had neglected to remove the .class suffix from the main class specifier string, and Ant happily fed the wrong classname argument to java.

Some useful features I ended up using over and over were the tasks to build and delete directories, the import feature that allows the importing of other Ant files and the property task that allows the creation of immutable variables.

I was able to write a series of Ant files to compile, create javadoc for, run, and package into a zip file, a simple Java project consisting of one source file.

The cool thing about the zip file output, is that you can unzip it (or unjar it since jar files are zip files), and run ant from within the unzipped directory to create a new zip file. This is what allows people to easily and portably share, extend, and modify, Ant based projects.

The only feature I wasn't able to find the the Ant documentation is about how to create a top level directory in the zip file to include all the files you actually want to zip up. I know from many unpleasant surprises, that unzipping a zip file with no single top level directory can inadvertently flood your current directory with a bunch of files you don't want there.

No comments:

Post a Comment