In the midst of my transition to Linux at work, I’m also working on updating the Ant build script templates that I’ve used for years to deploy every project I/we develop. When I built my Linux box, I installed Eclipse using Pulse. More often than not, I run my Ant scripts from within Eclipse (I love that integration), but from time to time it’s expedient, for one reason or another, to execute a build from the command line.
In previous lives, I’ve installed a standalone copy of Ant and used that for command line execution, but this time I was feeling thrifty. Clearly there’s an Ant executable baked right into Eclipse, which I already have installed, so why can’t I just use that one? Well, no reason, except that I didn’t know the path to said executable. So I dug around.
As you might expect, this is not a difficult problem to solve. I found my Ant root directory by looking in Eclipse under Window > Preferences > Ant > Runtime. On the Classpath tab, the first expandable group is named Ant Home Entries. Expand that to see all of the libraries that exist in the native classpath or, more importantly for this purpose, to see where the Ant home directly exists. To find the Ant home directory from these library paths just drop the /lib/[…].jar part of the library path. On my Pulse-installed instance, doing that leaves me an Ant home directory of /home/[user]/Applications/Pulse/Common/plugins/org.apache.ant_1.7.0.v200706080842.
Once you know the home directory, all that’s needed is to set a couple of environment variables and you’ll be able to run the Eclipse-installed instance of Ant from the command line or Eclipse.
For Linux and Mac users, edit your shell’s login script. In my .bash_profile, I added the following lines (syntax will vary for other shells):
Once you’ve saved and quit the editor, ensure that you “source” your modified script (or exit the terminal and return or even just open a new tab) to activate your changes for the current session:
$ source ~/.bash_profile
That’s it. To test, just type “ant” at your shell prompt. Ironically, Ant is alive and well if you get a failure message:
Buildfile: build.xml does not exist!
That’s it. Linux and Mac users can go on about their day.
Windows users will have to set/update the ANT_HOME and PATH environment variables in Control Panel > System > Advanced > Environment Variables. Windows users may also have to restart in order for their changes to stick unless they also set those values at their own command prompt for use during the current session. For the values to persist beyond the next reboot, though, the first method is required.