Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the Eclipse compiler completely from _within_ build.xml

Tags:

java

javac

ant

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.


2011-12-01: We have now migrated from ant to maven. The build.xml scripts hit the complexity wall and a fresh approach was needed. Anyone needing to choose what to do - do not go the ant4eclipse path except for trivial projects.


2012-11-30: A year later, the maven experience is still mostly good. There is a lot of quirks and changes in mindset but most make sense in the context. Maven can specify the compiler level on individual projects easily. We are looking into using ecj instead of javac (for several reasons) but for most purposes javac works nicely.

like image 529
Thorbjørn Ravn Andersen Avatar asked Mar 02 '10 14:03

Thorbjørn Ravn Andersen


2 Answers

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

like image 136
Mark Avatar answered Oct 12 '22 01:10

Mark


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.

like image 22
Alexander Pogrebnyak Avatar answered Oct 11 '22 23:10

Alexander Pogrebnyak