What is the recommended way to compile with eclipse compiler?


(frank.harnack) #1

I want to use compileJava with the eclipse compiler. First I try this:

buildscript {
 dependencies {
  classpath files('foolib/jdt.jar')
 }
}
  apply plugin: 'java'
  compileJava {
 options.compiler = 'org.eclipse.jdt.core.JDTCompilerAdapter'
}

But the result is a class “JDTCompilerAdapter” not found error. It works only with this additional code (from http://docs.codehaus.org/display/GRADLE/Cookbook ):

compileJava.doFirst {
 ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
 buildscript.configurations.classpath.each { File f ->
  antClassLoader.addURL(f.toURI().toURL())
 }
}

Is there a nicer way to do this? What I did wrong with the buildscript script block?

thanks

Frank


(Peter Niederwieser) #2

The basic problem is that compileJava uses the Ant javac task, and Ant is loaded by a different class loader than the build script. Hence you have to add the Eclipse compiler to Ant’s class loader. I don’t currently know a fundamentally different solution than the one shown, but it should be possible to replace your second code snippet with this one-liner:

org.apache.tools.ant.Project.class.classLoader.addURL(file(“foolib/jdt.jar”).toURI().toURL())

Then you can also remove the buildScript {} section in your first code snippet.


(frank.harnack) #3

Hello Peter,

many thanks for your answer.

Finally my code looks like this:

apply plugin: 'java'
  compileJava {
 options.compiler = 'org.eclipse.jdt.core.JDTCompilerAdapter'
 doFirst {
  files('org.eclipse.jdt.core_xyz.jar','jdtCompilerAdapter.jar').each{org.apache.tools.ant.Project.class.classLoader.addURL(it.toURI().toURL())}
 }
}

Regards Frank


(frank.harnack) #4

additional info: If you use the gradle daemon (1.0-milestone-3), the jars remain added to the class loader during the daemon lifespan.


(Luke Daley) #5

Peter, I’d say we need an issue to provide a proper way to do this. Do you agree?


(Peter Niederwieser) #6

Yes, I think we need a proper way to add stuff to our parent class loader. This is also needed in other situations like when dealing with JDBC driver Jars.


(Davide Cavestro) #7

Any idea on the status of https://github.com/adammurdoch/gradle-groovy-eclipse-compiler ? Does it suffer the same classloader’s headaches?