inger
inger

Reputation: 20184

Can I specify the JDK path to compile against within an Ant build.xml?

I would like to use JDK 1.6 for a branch of a project while others keep using JDK 1.5. Developers want to occasionally switch between those.

So what is the best way to tell Ant's javac which JDK to use? By best, I mean a robust, transparent, low maintenance, versioned together with the source (Ant itself and JDK are certainly not, but they live at standard places).


The obvious -rather than best- way I guess would be outside of Ant: keep changing the JAVA_HOME env variable. However this would require developers to manually switch (another thing to remember: error prone), an changing all the -many- build servers (more work for me now).

Looking for some simple javac attribute e.g jdk-path, I noticed several instead (thanks to reading up on the net and in SO):

So, it seems none of these help in itself.. is any combination of these equivalent to setting JAVA_HOME prior to running Ant?

I have some hacks in mind (eg wrapping ant executable on each platform just to set that env var - quite sad), but I really hope I missed something :)

Upvotes: 7

Views: 9423

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66723

Which version of the JDK is used to compile the classes should not necessarily matter. There may be differences in how a particular JDK compiles resources, but if the difference is just between v1.5 and v1.6, you can compile code to be compatible with Java 1.5 (or even 1.1) using a 1.6 JDK.

You can control what JVM version to compile for using the target attribute:

<javac srcdir="${src}" 
       destdir="${build}" 
       fork="true" 
       source="1.5" 
       target="1.6" />  

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691735

Using the executable attribute needs to set the fork attribute to true. This means that the javac ant task will launch an external process to execute javac.

When you launch javac manually, you don't have to specify any specific JDK lib directory: it knows where to find the libraries of the JDK it's part of. I'd say it will be the same if you launch it through ant's javac task (unless you override the bootclasspath).

Upvotes: 3

Related Questions