Tuesday, September 27, 2011

I'm terrified of bugs but Ant just might change my mind

I am. I truly am terrified of bugs. As a person, I am terrified of just about anything with more than four legs and as a software developer, I am terrified of my programs not doing what I want them to do. O bugs how I loathe thee! I recently discovered however, albeit ten years too late, that there actually is a bug I am actually quite fond of: Ant. Granted, if one were to truly overanalyze this one exception to my entomophobia, an ant is still an insect and the Ant I am so fond of is a Java application builder.

As any software developer should know, there exists a massive divide in the developer world regarding operating systems. Furthermore, not every developer is going to agree on which development environment they prefer to write code in and everyone has their own thought process about how their software projects should be organized. Naturally, these exponential differences between developer systems are going to cause trouble when two or more developers deem it necessary and wise to collaborate on a project. Thus the birth of software building utilities arrived and developers could carry on knowing they were right all along about which development system and environment is the best.

Ant is a tool which is ideally used for building Java projects. Invoking Ant on different operating systems should, theoretically, always yield the same project build and ultimately make it possible for developers to continue to work on the project in their preferred manner but have a quick solution for sending the project off to be worked on by another person as well. Additionally, Ant can be instructed to run various quality assurance tools each time it tries to build a project thus ensuring that the projects being shared adheres to the same standards as others using the same Ant build. It's easy to see now why a software developer can, upon learning about the existence of it, be so quick to embrace the technology.

Learning to write an Ant script does require a basic understanding of XML. However, suffice it to say that each Ant task is a different XML tag that is understood by Ant to perform some action whether it be print something to the command line or to simply set a value to a property. In order to get a crash course on Ant, I walked myself through several "Ant Katas" to get familiarized with some basic Ant scripting concepts. Said Ant Katas follow:

Preface
I do not intend to explain how I accomplished these katas in detail, but rather share with you my learning experience through them. On a side note, there have been several external links thus far in this article for your edification and education. You are welcome.

Ant Kata #1 - Ant Hello World
The idea behind this Kata is to learn how to write a simple Ant script. With a bit of quick internet researching, I was able to learn that an echo tag with a "helloworld" message value was all I needed. However, this being my first foray into Ant scripting, I hit a road block almost immediately. An echo tag by itself was no good because Ant requires actions to be wrapped in target tags to be performed. Targets, as it turns out, are the little blocks of instructions that tell Ant what to do. But since you can have several targets inside an Ant script, how would it know which target needs to be carried out, if at all? Hence my final lesson from a deceivingly simple kata was that one can specify a default target for Ant to use when the script is executed if no target is specified by the user of said script.

Ant Kata #2 - Ant Immutable Properties
This Kata involved associating a value to a property element in Ant and then associating a different value with it shortly after. As one might expect with a Kata referring to immutability, a property's value can not be altered once it has been set. What struck me as interesting here is that essentially properties can be viewed as constants in Java since they can only be set and referred to but never changed.

Ant Kata #3 - Ant Dependencies
If you have ever used Make before, you might be familiar with the concept of dependencies. Briefly speaking, associating a dependency to one target tells Ant that the execution of this target depends on the successful execution of its dependencies. For any person reasonably logical, the concept of dependencies shouldn't be very difficult to grasp. If a target Keyholder depends on Car (to get to work) and a Store depends on Keyholder (so it can open), it follows that Car is needed for Keyholder which is needed for Store. Likewise, if Car depends on Store (apparently the car needs to be purchased from the store first in this case) then we have a circle of dependencies with no logical entry or exit point. In the latter case, Ant would complain and fail to execute the build.

Ant Kata #4 - Hello Ant Compilation
The point of Ant is to build projects - ideally Java - and any decent Kata routine certainly ought to have something practical as part of its choreography. Ant Katas are no different and to practice Ant in see it perform its core functions in all its glory, I wrote a simple Java program to simply print "Hello Ant" to the console. By applying everything I learned in the previous Katas, I was able to execute this Kata without much trouble. Compiling the Java file was a simple matter of calling on Ant's javac task and telling it where to find the Java file and where to put the compiled file.

Ant Kata #5 - Hello Ant Execution
Of all the Katas, the one which gave me the most problems was figuring out how to execute the compiled class file from the previous Kata. Rather than an issue with understanding how to use the java task in Ant properly, it was a dependence on my development tools to keep track of my class path which led to my headaches. However, once I was able to surmise that it was my use of a lengthy package name which was causing my Ant build failures, I was able to quickly diagnose the problem and fix my script accordingly. Lo and behold, my compile was successful and my execution printed out "Hello Ant" to my delight.

Ant Kata #6 - 8
For you, the reader, I will summarize that the goals for Katas 6 through 8 were to have JavaDocs be generated by an Ant script, a standalone "clean" target to exist and be readily invokable should the user choose to do so, and to put everything we've learned from the previous Katas together and package the project in one neat zip file for distribution. However, as I forewarned previously, this is a discussion of my learning experience through these Katas and, quite frankly, there is little left to discuss here.

Ironically, it turns out, the simpler Katas taught me the fundamentals of Ant scripting which allowed following Katas to be much easier to execute, and the skills for deciphering how Ant tasks work were all it took to execute the latter Katas. In retrospect - which is always an easy perspective to speak from - it was with a very low level knowledge of Ant and XML that I was able to manage completing my 8 Ant Katas and take my first step towards writing more robust and functional Ant scripts. That fact, coupled with the power of Ant and its supreme usefulness in software development, made me a believer that perhaps not all bugs demand from me the response of utter terror.

No comments:

Post a Comment