Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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):

  • compiler- fair enough, but docs says "modern: .. javac1.5 and javac1.6 .. as aliases".. To me this suggests it won't make in any difference - will it?
  • source- seems only related to JLS version (althought not %100 clear from the docs linked above)
  • target - bytecode version
  • bootclasspath - some SO answers mention this, but pretty unclear and seems hackish
  • executable - path to javac, but not to libs.. -- seems the closest match, implicitly specifying JDK path? UPDATE: confirmed by JB Nizet
  • fork - it seems I'll need true here (otherwise it'll just ignore the above without error?). UPDATE: Any performance implications vs. default? (I guess JVM startup times are better these days, but still)

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 :)

like image 955
inger Avatar asked Feb 13 '11 18:02

inger


People also ask

Does Ant use Java_home?

The ant. bat script makes use of three environment variables— ANT_HOME , CLASSPATH and JAVA_HOME . Ensure that ANT_HOME and JAVA_HOME variables are set, and that they do not have quotes (either ' or ") and they do not end with \ or with /. CLASSPATH should be unset or empty.

How do I run an Ant file in xml?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

Is Ant compatible with Java 11?

Ant Migration Tool version 51.0 and later requires Java version 11 or later. If working with Ant Migration Tool version 36.0 to 50.0, for enhanced security, we recommend Java 7 or later and a recent version of the Ant Migration Tool (version 36.0 or later).

What is build xml in Ant?

The build. xml file contains information that the wsgen Java Ant task uses to assemble Web services into Enterprise Application archive (*. ear) files.


2 Answers

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).

like image 161
JB Nizet Avatar answered Sep 29 '22 16:09

JB Nizet


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" />  
like image 25
Mads Hansen Avatar answered Sep 29 '22 18:09

Mads Hansen