Wednesday, September 28, 2011

Can you pratice with ants?

Ants. They can get everywhere and rudely invite themselves to picnics, trash cans, and dropped food. But there's something good about them; they often clean up after other animals and are small. Unless you've been watching too many B-movies about them growing to monstrous sizes and shooting lasers from their antennas.

Sorry about that, went off on a tangent there. Ants, small and useful... maybe. But there's one ant that you should take a look at. Apache's Ant builder, a very useful tool that can compile, build, manage dependent files, and create zipped files, just to name a few of the many uses for this versatile tool. You can get more information straight form the source here which will take you to Apache's Ant home page that describes the project in great detail. Just don't forget to come back here after looking at all the nice documents.

Apache's Ant project home page

Now that you're back, I talked about some katas in my last blog here. Katas are what martial artists use to practice different moves and technuqies in a set routine in order for them to get used to performing the move or technique more fluidly. After getting interested in what Apache's Ant has to offer, I was offered some Ant katas that are similar in practice as the Robokatas I had talked about in my previous blog.
  1. Of course, there is the obligatory Hello World for Ant.
  2. Finding about immutables in Ant.
  3. Ant target dependencies.
  4. Making Ant compile a Hello Ant Java program.
  5. Running the Hello Ant Java program from within Ant.
  6. Making Javadocs for the Hello Ant program with Ant.
  7. Cleaning up the clutter from the compilation of Hello Ant.
  8. Making a distributable of Hello Ant (so everyone can see how Hello Ant can take over the world).
This might not look as arduous as the Robokatas, but they are nevertheless important to practice and maintain programming skills. On to the joy (and pain) of the Ant katas.

The first one is a quick intro to Ant, just make Ant output "Hello World" through the use of an echo statement. Now, you might see something familiar about echo statements and old DOS commands if you remember DOS (or can program in DOS!), but if you checked out the Apache's Ant homepage Ant was created as a command line tool. The setup was simple enough that only a quick look though some old DOS "Hello World" files I had was enough to complete this task. (Yes, I had some programming in DOS.) The next one was learning about how static variables, called a <property>, are immutable. Create a <property> and give it a value, create another <property> with the same name and set the value to something else then print the vale of the <property>. As stated before, the <property> variables are static and can't be reset once they are set.

The next kata is about dependencies, which can alter the way that targets are executed in Ant. Targets are generally the methods, or the actions, that are executed in Ant. Often, dependencies are needed in order to insure that critical parts are completed first before other parts. You wouldn't want to put on a jacket before putting a shirt on, just like if a computer program tries to read a file before it is created; it will say something like "Uh... no..." and stop or whatever the program does when it encounters an error. So dependencies are a way to restrict targets from running and putting them on the side as a stack until all their dependencies are fulfilled, if at all. Create a bunch of targets with dependencies and see how the ordering of the dependencies can change the order in which they execute. As a joke, someone told me about revolving dependencies, where target 1 depends on target 2 and so forth until the last target is dependent on target 1. When I tried to run the circular dependent targets, I got an error explicitly saying that Ant does not like revolving dependencies. Oh well, so much for that fun...

The fourth item in the kata is to make an actual Hello Ant Java program and then use Ant to compile the Java file. Normally this would take several lines of input that is a pain to type in the command line. First, Java has to create source files for the compiled code, then create a separate run file that needs to be evoked with another line of input just to run the completed code. Even through all that, you have to deal with any errors that might come up. All that command line compiling is a pain and just having something along the lines of "push the button" would be way better. With Ant, you have that. Just create another build.xml file that has all the lines of code that need to be run as targets, direct Ant to run that file and you should be good to go! If not, Ant can easily direct your attention to where the problem is and fix it easily.

So, we're done with the fourth item and onto the fifth. If the fourth item was compiled, then the fifth shouldn't be harder. All that needs to be done is to run the "Hello Ant" Java program after it has compiled. Remember, that's after and when usually something needs to be run in a specific order, dependencies need to be used. Just make sure that the target that calls the Java run code for "Hello Ant" is called after, not before, the "Hello Ant" Java code is compiled. Another thing that someone pointed out was the way to import other xmls into another xml file. Not by cut and paste, but using the <import> method to make Ant get the targets and dependencies from the imported file.

For the sixth item, I had to make a Javadoc for "Hello Ant". I bet you're thinking, "Really? A Javadoc for something this lame?" I know that it might seem lame, but if I'm struggling with this, then I have a serious problem that I need to work on some Javadoc katas. Once "Hello Ant" can compile and run without any errors, it was time to get the Javadocs started. The Ant <import> method came in handy again, as I wanted to get the compiled program first before creating Javadocs for the "Hello Ant" Java program. The seventh item was just a way to clean up after compiling the "Hello Ant" program. By doing this, there will be no error if I change something inside the "Hello Ant" program and have to compile the "Hello Ant" program again, since the original compiled files are deleted and only the executable remained.

The last item is to make a distributable of "Hello Ant" so that everyone can see how great "Hello Ant" is. In order to create the distributable, "Hello Ant" must compile, run, have a Javadoc, and clutter removed before copying all the source files and then compressing them into a zip file. More dependencies, imports, and headaches, but at least this has been a growing experience. So I had to make sure that "Hello Ant" compiles and runs, if not, what the heck was I working with for the last two items? "Hello Ant" compiles and runs, and I also have a Javadoc for the "Hello Ant" program, meaning that I cleaned up all the clutter and now I can copy the source files into a temp directory without the added clutter which I will create into a zip file.

Once I completed the kata, I felt more confident about using Ant. In the beginning it was difficult, even for such simple tasks, since mostly all of the syntax was not easily explained on the Ant website. I had downloaded the manual and looked through the material, but I still had some trouble with the syntax even though there was some syntax I recognized. When the manual wasn't enough, I went looking on Google for some explanations for some of the more troubling aspects which I should have known. The manual should be a bit more fluid in order to get some information about a method much easier. Ant makes compiling and running programs much easier by using xmls to generate a build file, rather than using something like makefile, which uses tab spaces. But to each their own and I like to use Ant for now, until something else that's better to me comes along. Oh, and keep up with the programming katas.

No comments:

Post a Comment