Can I specify the JDK path to compile inside Ant build.xml?

I would like to use JDK 1.6 for the project branch, while others continue to use JDK 1.5. Developers sometimes want to switch between them.

So what is the best way to tell Ant javac which JDK to use? At best, I mean solid, transparent, low maintenance, with the version along with the source (Ant and JDK, of course not, but they live in standard places).


The obvious is better than I expect it to be outside Ant: keep modifying the env JAVA_HOME variable. However, this requires that the developers manually switch (one more thing to remember: the error is affected), changing all -many-build servers (now works more for me).

Looking for some simple javac attribute like jdk-path , I noticed a few instead (thanks for reading on the net and in SO):

  • compiler is fair enough, but docs says "modern: .. javac1.5 and javac1.6 .. as aliases." For me, this suggests that it will not make any difference - right?
  • source - seems to apply only to the JLS version (although not 100% of the document linked above)
  • target - bytecode version
  • bootclasspath - some SO answers mention this, but are rather obscure and seem hacky.
  • executable - the path to javac, but not to libs .. - seems to be the closest match, implicit JDK path specification? UPDATE: verified by JB Nizet
  • fork - it seems I need true here (otherwise it will simply ignore the above without errors?). UPDATE: any performance implications or default? (I think JVM startup time is better, but still)

So, it seems that none of them helps on its own .. is there any combination of these equivalents to set JAVA_HOME before running Ant?

I have some hacks in my head (for example, the ant wrapper executable on each platform just to make sure that env var is pretty sad), but I really hope that I missed something :)

+6
java build javac ant
source share
2 answers

Using an executable attribute, you must set the fork attribute to true. This means that the javac ant task starts an external process to execute javac.

When starting javac manually, you don’t need to specify any specific JDK lib directory: it knows where to find the JDK libraries of which it is a part. I would say that it will be the same if you run it using the ant javac task (unless you redefine the bootclass path).

+2
source share

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

You can control the version of the JVM to use the target attribute:

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

All Articles