Saturday, March 21, 2009

Ant for Mac OS

For those who are avid users of Ant (Another Neat Tool), an Open Source tool developed by Apache (http://ant.apache.org), you will be pleased to know that Mac OS X v10.5.x (Leopard) now comes with it!

You may find the Ant files in the directory: /usr/share/ant. Since the environment was already prepared to recognize Ant, you may run it anywhere on the Mac Terminal without additional configuration.

Mac Books with Leopard bought in 2008 still have Ant version 1.7.0 installed on them, whereas the latest Ant version to date is 1.7.1. Those who are using Ant ftp, ssh, javadoc tasks might want to upgrade to this new version to avail of some bug fixes. There are also some additional attributes on tasks such as javac and fileset which they may find useful. For a complete list of changes, see the release notes on Ant 1.7.1.

Ant compilation problem using javac task on Mac

Here's something I encountered a few days back:
Scenario:
* Some files containing the Java 1.6 annotation @Override was causing the Ant build.xml to report an error after compilation using the javac task.
* Upon checking the javac task in build.xml, the Java version used correctly pointed to 1.6:

<javac srcdir="${srcdir}" destdir="${destdir}"
classpathref="${classpathref}"
compiler="${compile.compiler}"
source="1.6"
target="1.6"
debug="${compile.debug}"
failonerror="${failonerror}" />


* When the Ant javac's attribute: verbose was added and set to "yes", Ant still appeared to be using Java 1.5. It seemed that Ant was ignoring whatever was set on the source and target parameters inside the Ant build file!

* I tried looking for JAVA_HOME where I could set the correct Java version, but did not find any.

Cause:
After much googling, Mac OS keeps its JDK versions in the directory: /System/Library/Frameworks/JavaVM.framework/Versions/. After performing ls -al, the following appeared:

mac:Versions user$ ls -al
<snip>
lrwxr-xr-x 1 root wheel 5 Jan 8 12:02 1.5 -> 1.5.0
drwxr-xr-x 8 root wheel 272 Sep 20 2008 1.5.0
lrwxr-xr-x 1 root wheel 5 Jan 8 12:02 1.6 -> 1.6.0
drwxr-xr-x 8 root wheel 272 Jan 8 12:02 1.6.0
drwxr-xr-x 8 root wheel 272 Jan 8 12:02 A
lrwxr-xr-x 1 root wheel 1 Jan 8 12:02 Current -> A
lrwxr-xr-x 1 root wheel 3 Jan 8 12:02 CurrentJDK -> 1.5
mac:Versions user$


Apparently, Apache Ant looks at the CurrentJDK, which in my case, points to JDK 1.5! This is why Ant seems to be disregarding the source and target attributes of the javac task!

Solution:
Currently, one of the easiest solutions is to force your Mac's CurrentJDK to refer to JDK 1.6.
NOTE: Make sure that you remember changing the CurrentJDK, as this might break your builds in other projects not using 1.6. Extra precaution must be made, especially if you are sharing your Mac with other developers/testers.

To change the soft link:
1. On the Mac Terminal, go to the Java versions directory:
cd /System/Library/Frameworks/JavaVM.framework/Versions/

2. Delete the existing CurrentJDK link:
sudo rm CurrentJDK

3. Create a new CurrentJDK link pointing to the 1.6 JDK:
(Recall: There exists a Java directory in /System/Library/Frameworks/JavaVM.framework/Versions/ named 1.6 which points to JDK 1.6:
lrwxr-xr-x 1 root wheel 5 Jan 8 12:02 1.6 -> 1.6.0)

sudo ln -s 1.6 CurrentJDK

Generalization:
This problem will occur for any Ant javac references to the Java version used for compilation. The current (i.e. 1.7.1.) Ant Manual for the javac task was checked at http://ant.apache.org/manual, but no documentation is currently found for this problem for the Mac.