tags:

views:

73

answers:

2

As an experiment we want to build our products using the Eclipse java compiler (ecj-3.5.jar downloaded from eclipse.org) on the runtime version of Java 6 instead of the JDK, and as I understand it, it is a matter of adding this jar to the ant classpath, and setting the build.compiler property to point to the adapter.

By including

<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />

in my build.xml and invoking ant with a JRE, I get the expected error that the adapter cannot be found, and by adding ecj-3.5.jar to the classpath in the Eclipse panel I can compile my code as expected. I believe the same functionality to be available with "-lib foo.jar" from the command line with modern ants.

Now, I want to specify from within build.xml that I want ecj-3.5.jar on my classpath satisfying the same as above. We can already do this with ant tasks, so I believe it is possible.

So the question is: How can I add to the classpath used by javac to locate the compiler only from within build.xml?


It appears that the upcoming ant4eclipse 1.0 includes the Eclipse compiler (which is what I wanted to use this for), so by upgrading to that (from 0.5) should solve the problem we have.


2010-09-24: Ant4Eclipse is still at M4 without indication of when the release will happen.

+2  A: 

One way is to specify a reference to a componentdef when using javac.

<componentdef name="ecj" 
              classname="org.eclipse.jdt.core.JDTcompilerAdapter" 
              classpath="ecj-3.5.jar" />
<javac ....>
  <ecj/>
</javac>

Another option is to set build.compiler as you have or the compiler attribute for javac and then specify a compilerclasspath for javac. This is a normal path like structure to hold the classpath for loading your compiler adapter.

<javac compiler="org.eclipse.jdt.core.JDTcompilerAdapter" ....>
  <compilerclasspath>
     ...
  </compilerclasspath>
</javac>

See the javac task documentation in the Ant manual for more details. Note that both these both solutions only work from Ant 1.8 onwards.

Mark
With the ant shipped with Eclipse 3.5 (probably 1.7) I get "Problem: failed to create task or type componentdef". Another error message is classpath related with ant 1.8, but I'd really appreciate a solution working from inside Eclipse.
Thorbjørn Ravn Andersen
I added another option for Ant 1.8 but don't see how you could do it with 1.7
Mark
+1  A: 

Reading Running Ant via Java. I think you can write a simple wrapper that will properly set a classpath and add your jar file to the resulting class path.

Here I'm just cutting and pasting the sample from the above link with addition of the library that you are interested in to the classpath:

<java
        classname="org.apache.tools.ant.launch.Launcher"
        fork="true"
        failonerror="true"
        dir="${sub.builddir}"
        timeout="4000000"
        taskname="startAnt"
>
    <classpath>
        <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
        <pathelement location="/path/to/ecj-3.5.jar"/>
    </classpath>
    <arg value="-buildfile"/>
    <arg file="${sub.buildfile}"/>
    <arg value="-Dthis=this"/>
    <arg value="-Dthat=that"/>
    <arg value="-Dbasedir=${sub.builddir}"/>
    <arg value="-Dthe.other=the.other"/>
    <arg value="${sub.target}"/>
</java>

I think you can even reuse the same build file, just give a different target as an entry point.

Alexander Pogrebnyak
You are suggesting relaunching ant with a revised classpath. I'd really avoid the relaunch step.
Thorbjørn Ravn Andersen