Notes on Apache Ant

A basic checklist before diving into Ant

1. Make sure that files inside /AntInstallDirectory/bin are on the PATH environment variable

2. Make sure that files inside /AntInstallDirectory/lib are part of the CLASSPATH environment variable. If you need instruction on how to set environment variables in Windows, go to this page, if you need instruction on how to do it in OSX, go to this page.

The build process and the build file

The java build cycle is write the code, compile the code, run the code, debug the code, write some more codes, compile the code, run the code so on and so forth–and you will do this many many times over while you are still a programmer, you will do this countless times a day.

If you just got started as a Java programmer, haven’t joined a development team just yet, you might even encounter this question in a interview; you can get quizzed on what is your experience on java build processes–using Apache ant seems to be a common practice across many Java programming teams, it appears to be an expectation too that, you as a Java programmer, are familiar with this tool–so there are a lot of values in being familiar with Apache ant.

The case for Ant

The value of Ant goes way beyond knowing the buzz word in build tools, it’s actual value goes beyond being buzz word compliant. The Java compilation process and run time environment can be pretty complex. For example, if you write a code which uses the MySQL database then you will need to pass the classpath information to javac so that it will know where to find the java libraries of the MySQL connector. Not only that, after your have successfully compiled your code, you will still need to pass the classpath information again of the MySQL java library to java before it can run without problems. It may not be that big a deal if you are just referencing a single .jar or .class file as part of your compilation process, but it can get real hairy and cumbersome when your project will include a couple of .jar files or a couple of .class files as part of the compilation process–plus of course, a non trivial application will be comprised of more than just one java source file.

Imagine that you compile your code like this

javac File1.java, File2.java, File3.java, File4.java -classpath ../first.jar
java File -classpath ../first.jar

I actually can’t just recall an actual command line javac that I’ve used in the past, but I think you get the idea. Do you really want to type this line forty times a day? Ant makes a good case, even if just to save your from Carpal Tunnel Syndrome.

If you are the tech lead of a team, ant makes you sleep better at night knowing that everyone in the team is using the same .jar files in their libraries.

Some Ant basics

The Ant build file is an XML file, it’s name is build.xml–now you can go on ahead and snatch some example files somewhere in the Internet and retrofit it to suit your own needs, but it’s really easy to understand if you take the time.

Because the build file is XML, you can use whatever editor you are already using to edit it, a simple text editor such as Programmer’s notepad, TextMate, TextEdit or even Notepad will do the job. The Ant intallation comes with a JAXP compliant XML parser so you don’t need to install any external parser.

Build the folders first

Before we go any further with the build file, I suggest that you create some folders first in your working directory, so that we can practice with them later.

mkdir practiceproject
cd practice project
mkdir src lib build

The 3 folders we just created actually are very typical folders inside the project file.

The basic build file

‘?xml version=”1.0″?’ – Build files are XML files so the document begins with a declaration that tells us which version of XML is in use to allow the possibility of automatic version recognition–if it becomes necessary. Notice that the XML declaration is enclosed in question marks, and not in slash (/).

‘project name=” ” default=”compile” basedir=”.”‘

ant-directory

You can place the build.xml anywhere you want, but that will change the values of some of the directory locations in your build.xml–in our example, my build.xml is actually not inside build, src or lib, it’s actually on the same folder level as src, lib and build.

property name=”src”, “lib” and “build” – Property names are just like user variables, you get to name anything you want. In this example, I actually would like to declare src=”src”, build=”build” and lib=”lib” because I’d like them to hold the folder names so I can refer to them later using the syntax ${src}; this means I’d like to resolve the value assigned to the src property. The rule is simple, if you want to resolve the value of a property, just enclose it inside ${NameOfProperty}–it’s a bit unix-y.

path id=”classpath.base” – this is where you declare all the jars and other libraries that you will need for the project. If you need to reference mysql-java connector jar, this will be the place to put it.

target name=”init” and target name=”compile” – targets is probably the single most important feature of Ant, it is used a wrapper for sequences of actions. Think of it as a function inside a build file. You can put a sequence of actions inside a target. In our case, I’ve defined 2 targets, init and compile. The init target just contains a single action, and that is to make sure that there a build folder, if it doesn’t find one, it will make one.

The compile target will be the workhorse of this build script. Every time I make a change on my java source file(s), I just invoke ant and it will perform the java compilation process for all .java files, not only that, it will store all the resultant .class files inside my build folder–not only that, all the necessary libraries and jars that my app needs will be included in the classpath during every compile.

Now you are ready to take on a small project. If you need to compile your code 40x a day, you just need to type ant–instead of a 200 character command using raw javac.

Ant isn’t limited to this example, it can do much more–a lot of techies have already written very good guides on how to use Ant. Let me give you 2 of them.

The official Ant page and;
This guide from Ashley J.S MIlls

If you want to show appreciation for my efforts dear reader, you could buy me a tall hazel nut Americano ($2) via PayPal. Thanks
Post navigation
(previous post)
(next post)
| | | | .

{1 Comment below .. you can add one }


BUILD FAILED Premature end of file — A list of errors 12.23.2009at 09:46

[...] If you need to refresh on Apache ant, or just starting out with Ant, take a look at some Notes on Apache Ant [...]

Leave a comment