How to make Ant prefer classes from a linked JAR over JDK classes?

ant, java

Solution

You could modify the boot classpath and there is support for that with a specific attribute in ANT, but I think it should be the `java.endorsed.dirs` property (in raw javac):

javac -Djava.endorsed.dirs=/some/path/lib/endorsed ...

Or with a `compilerarg` ANT sub-element:

<target name="compile" depends="prepare-src">
    <javac srcdir="${build.src}"
       destdir="${build.dest}"
       debug="${debug}"
       optimize="${optimize}"
       target="1.3"
       source="1.3"
       deprecation="${deprecation}"
       classpathref="compile.classpath">
    <compilerarg value="-Djava.endorsed.dirs=/some/path/lib/endorsed"/>
</javac>

You shouldn't add the `endorsed` directory to the classpath as the boot classpath and any endorsed directories are checked before the classpath to resolve required types. This would mean the JDK's newer DOM implementation would be found first.

Problem

I am trying to build the dom4j JAR, which includes an xml-apis JAR which contains a DOM API that is older than that shipped with more recent JDKs. However, even though in the build file the source and target compiler attributes are set to 1.3, and even though the xml-apis JAR is included in the build path, Ant still tries to compile dom4j against another, newer, w3c API (I guess one from a JDK installation). Here's the relevant Ant code: ``` <path id="compile.classpath"> <fileset dir="./lib/endorsed"> <include name="*.jar" /> </fileset> <fileset dir="./lib"> <include name="*.jar" /> </fileset> </path> <target name="compile" depends="prepare-src"> <javac srcdir="${build.src}" destdir="${build.dest}" debug="${debug}" optimize="${optimize}" target="1.3" source="1.3" deprecation="${deprecation}" classpathref="compile.classpath"> </javac> </target> ``` the JAR that should be used is in lib/endorsed, but it's not used during compilation. How come?

Original source