Thursday, September 29, 2011

Ant 型 (Kata)


In this exciting week, I've had the opportunity to learn and work with the Ant Build System-- a software tool for automating software build processes. Although I've "used" build systems before, they were only IDE-integrated ones (such as MSBuild in Microsoft Visual Studio), and as such, I did not have to even know that such pieces of code existed. However, with all of its capabilities, it is crucial to actually write code using build system tools, so that when you want to release code to others, everything is neat and complete.

Since my primary focus for now is Java, the build system of choice is Apache Ant, which despite its somewhat not-so-pleasant name, stands for Another Neat Tool. And it is! Ant is quite nice to programmers, although it can get rather complicated at times. It comes packed with feature after feature, so it did seem overwhelming at first. In fact, it is still a little overwhelming to me, but just because of the sheer amount of capabilities that it has. But just like Robocode, I carried out the learning process of Ant through a series of programming martial arts, called Katas.


The 8 Ant Katas that I practiced were:
  1. Ant Hello World- Start from ground zero-- write a script that uses the <echo> task to print out Hello World.
  2. Ant Immutable Properties- Understand the immutability of properties-- create a script that sets a property to one value, and then attempts to change it by assigning it another value. Printing out the value of the property returns the initial value, demonstrating immutability.
  3. Ant Dependencies- Understand the use of the "depends" attribute by creating several targets that have different dependencies, and see in what order the targets end up executing. Also, understand what happens when the dependencies are cyclic (they depend on each other in an infinite loop).
  4. Hello Ant Compilation- Learn how to compile Java code using Ant-- create a script that uses <javac> to compile a simple HelloAnt.java file.
  5. Hello Ant Execution- Run Java code using Ant-- create a script that first compiles Java code (using dependencies and the compiler script from Kata 4), and executes the HelloAnt program, which prints "Hello Ant" to the console.
  6. Hello Ant Documentation- Generate JavaDoc documentation-- create a script that first compiles the HelloAnt program and then generates the JavaDocs for it using the <javadoc> task.
  7. Cleaning Hello Ant- Implement a cleaning task-- add a "clean" target to the compile script that deletes the "build/" directory that stores the .class files and JavaDocs.
  8. Packaging Hello Ant- Zip up the project so it can be shipped out-- create a script that uses the <zip> task to package the project into a zip file.
It was difficult to get started with writing the first couple of scripts because of the "unfamiliar territory" that I was in. However, as I got used to the different attributes and properties and just the overall structure of the scripts, things became much easier. To anyone wanting to start using Ant, I would recommend getting familiar the Ant Manual when implementing tasks. At first, I used this just to copy the examples, but many of them contain more complex features which are not necessary to learn at this moment, so definitely learn how the manual works, especially the Parameters table which lists and explains all the attributes, and even tells you whether it is required or not. I wish I had realized this earlier!


The most difficult part of these Katas was managing the directories to and from which files were to be compiled, run, generated, and packaged (for Katas 4-6 and 8). I frequently got an error message reporting that "<ProjectDirectory>/src could not be found", even though I had set up the properties correctly in the compile script. I resolved these errors by explicitly specifying the "basedir" property at the top of the script with the project declaration. Prior to this, I had thought that since it was specified in another dependency script, this would not have to be set, just like how properties need not be set again. I am not sure if this is the correct way to solve this issue, but it averted the crises that I had, so perhaps this is indeed the solution. I shall look into this more, and in the meanwhile hopefully I can learn more of the Ant features!

No comments:

Post a Comment