Wednesday, September 28, 2011

Building Systems with Ant

Introduction

Ever wish there was an easier way to build your Java projects?  Tired of using the command line to manually run all of your fancy tools?  Well wait no longer as Ant can automate your build processes with the help of a few user-defined XML files.  These XML files are created using a special subset of the XML language and to learn this we shall once again use the concept of code katas.

Tasks
  1. Ant Hello World - Print Hello World.  Learn the <echo> element
  2. Ant Immutable Properties - Show that Ant properties are immutable once they are set.  Learn the <property> element.
  3. Ant Dependencies - Learn how the depends attribute of the <target> tag works.
  4. Hello Ant Compilation - Use the <javac> element to compile a Java program.
  5. Hello Ant Execution - Use the <java> element to run a Java program.
  6. Hello Ant Documentation - Use the <javadoc> element to generate the documentation for a Java program.
  7. Cleaning Hello Ant - Create a target that deletes the build directory to clean the system.
  8. Packaging Hello Ant - Clean the system and pack it into a zip for distribution.

Experiences

Most of the katas were simple and easy to complete using a few targets and lines of code.  The only kata that gave me problems was the Packaging Hello Ant kata.  While it was simple to package all of the contents of the working directory into a zip archive, it was slightly more tricky to pack all of those contents within a folder inside of the zip archive.  To do this, I had to use the <copy> element to copy the contents of the working directory to a new temporary directory.  Then I could specify that I wanted the new directory and all its contents to be included by the <zip> element to create a zip archive with the desired structure and delete that temporary folder.  Hence, this exercise took a little more thinking as to how I would get the appropriate structure for the zip archive.

From these exercises, I learned how convenient build systems can be.  With a single command, I could compile a program, run it, generate documentation for it, clean the project, and pack it for distribution.  This would take many command-line operations and being able to do it just by typing "ant -f dist.build.xml" saves a lot of effort and time.

Overall, these exercises have given me a better appreciation for build systems, especially Ant.  Not only are they extremely convenient, they are also quite simple to use as the basic XML language used to direct Ant is rather simple and well documented.  Consequently, I am definitely looking forward to seeing Ant's true power as I start to work on bigger and bigger systems and see just how much simpler it makes the building process.

0 comments:

Post a Comment