Permalink
Browse files

- Use the eclipse compiler for standalone java compilation too.

- Wired up some more compile options (source compatibility, target compatibility, debug, compilerArgs).
  • Loading branch information...
1 parent 26973cb commit 6aeeac642b5a9af036d30bf248c8d98e77504606 @adammurdoch committed Oct 18, 2010
View
@@ -1,19 +1,26 @@
What is it?
-----------
-A Gradle plugin that configures the Groovy plugin to use the Groovy Eclipse compiler.
+A Gradle plugin that configures the Java and Groovy plugins to use the Groovy Eclipse compiler instead of the javac and
+groovyc compilers.
Building
--------
-You'll need to use Gradle 0.9-rc-1 or later.
+You'll need to use Gradle 0.9-rc-1. It does not work with earlier or later Gradle releases.
Run `gradle install` to build the plugin into the local maven repository.
Using the plugin
----------------
-The plugin works with Gradle 0.9-rc-1 or later. To use it, add the following to your build script:
+The plugin works with Gradle 0.9-rc-1. To use it, you need to
+1. load the plugin from the local maven repository using a `buildscript { }` section
+2. apply the `groovy-eclipse` plugin.
+3. apply any of the `java', `java-base`, `groovy` or `groovy-base` plugins. The plugin does not do anything on its own
+ without one of these plugins applied.
+
+Here's an example:
buildscript {
repositories {
@@ -24,5 +31,5 @@ The plugin works with Gradle 0.9-rc-1 or later. To use it, add the following to
}
}
- apply plugin: 'groovy'
+ apply plugin: 'groovy' // or apply plugin: 'java'
apply plugin: 'groovy-eclipse'
@@ -21,9 +21,14 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
+import org.gradle.api.internal.project.AntBuilderFactory;
+import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.internal.tasks.compile.GroovyJavaJointCompiler;
import org.gradle.api.internal.tasks.compile.IncrementalGroovyCompiler;
+import org.gradle.api.internal.tasks.compile.IncrementalJavaCompiler;
+import org.gradle.api.internal.tasks.compile.JavaCompiler;
import org.gradle.api.tasks.WorkResult;
+import org.gradle.api.tasks.compile.Compile;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.compile.GroovyCompileOptions;
@@ -43,22 +48,38 @@
import java.util.List;
/**
- * {@link org.gradle.api.Plugin} that configures each instance of {@link org.gradle.api.tasks.compile.GroovyCompile}
- * to use the groovy eclipse compiler instead of the groovyc compiler.
+ * {@link org.gradle.api.Plugin} that configures each instance of {@link org.gradle.api.tasks.compile.Compile} and
+ * {@link org.gradle.api.tasks.compile.GroovyCompile} to use the groovy eclipse compiler instead of the javac or groovyc
+ * compiler.
*/
public class EclipseGroovyCompilerPlugin implements Plugin<Project> {
private static URLClassLoader adapterClassLoader;
private static final Logger LOGGER = LoggerFactory.getLogger(EclipseGroovyCompilerPlugin.class);
- public void apply(Project project) {
+ public void apply(final Project project) {
project.getTasks().withType(GroovyCompile.class).allTasks(new Action<GroovyCompile>() {
public void execute(GroovyCompile groovyCompile) {
- groovyCompile.setCompiler(new IncrementalGroovyCompiler(createCompiler(), groovyCompile.getOutputs()));
+ groovyCompile.setCompiler(new IncrementalGroovyCompiler(createGroovyCompiler(), groovyCompile.getOutputs()));
}
});
+ project.getTasks().withType(Compile.class).allTasks(new Action<Compile>() {
+ public void execute(Compile compile) {
+ ProjectInternal projectInternal = (ProjectInternal) project;
+ compile.setJavaCompiler(new IncrementalJavaCompiler(createJavaCompiler(), projectInternal.getServiceRegistryFactory().get(AntBuilderFactory.class), compile.getOutputs()));
+ }
+ });
+ }
+
+ private JavaCompiler createJavaCompiler() {
+ // Classloader hackery to work around Gradle's poor classloader model. This will get fixed after Gradle 0.9 is out.
+ try {
+ return (JavaCompiler) createClassLoader().loadClass(EclipseBackedGroovyJavaJointCompiler.class.getName()).newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException("Could not load compiler adapter.", e);
+ }
}
- private GroovyJavaJointCompiler createCompiler() {
+ private GroovyJavaJointCompiler createGroovyCompiler() {
// Classloader hackery to work around Gradle's poor classloader model. This will get fixed after Gradle 0.9 is out.
try {
return (GroovyJavaJointCompiler) createClassLoader().loadClass(EclipseBackedGroovyJavaJointCompiler.class.getName()).newInstance();
@@ -81,6 +102,7 @@ private static URLClassLoader createClassLoader() {
FilteringClassLoader parentClassLoader = new FilteringClassLoader(GroovyJavaJointCompiler.class.getClassLoader());
parentClassLoader.allowPackage("org.gradle.api");
parentClassLoader.allowPackage("org.gradle.util");
+ parentClassLoader.allowPackage("org.slf4j");
// check class is not visible from parent class loader
try {
@@ -95,7 +117,7 @@ private static URLClassLoader createClassLoader() {
return adapterClassLoader;
}
- public static class EclipseBackedGroovyJavaJointCompiler implements GroovyJavaJointCompiler {
+ public static class EclipseBackedGroovyJavaJointCompiler implements GroovyJavaJointCompiler, JavaCompiler {
private final GroovyCompileOptions groovyCompileOptions = new GroovyCompileOptions();
private final CompileOptions compileOptions = new CompileOptions();
private String sourceCompatibility;
@@ -112,6 +134,10 @@ public void setGroovyClasspath(Iterable<File> classpath) {
// Don't care
}
+ public void setDependencyCacheDir(File file) {
+ // Don't care
+ }
+
public CompileOptions getCompileOptions() {
return compileOptions;
}
@@ -143,13 +169,28 @@ public WorkResult execute() {
Main compiler = new Main(stdout, stderr, false, Collections.<Object, Object>emptyMap(), new CompilationProgressImpl());
List<String> args = new ArrayList<String>();
+ classpath.add(destinationDir);
+
args.add("-cp");
args.add(GUtil.join(classpath, File.pathSeparator));
args.add("-d");
args.add(destinationDir.getAbsolutePath());
- args.add("-1.5");
+ if (GUtil.isTrue(sourceCompatibility)) {
+ args.add("-source");
+ args.add(sourceCompatibility);
+ }
+ if (GUtil.isTrue(targetCompatibility)) {
+ args.add("-target");
+ args.add(targetCompatibility);
+ }
+ if (compileOptions.isDebug()) {
+ args.add("-g");
+ }
+ args.addAll(compileOptions.getCompilerArgs());
+
+ // TODO - add the remaining options
- // TODO - add the options
+ LOGGER.info("Using compile args: {}", args);
for (File file : source) {
args.add(file.getAbsolutePath());
@@ -1,7 +1,7 @@
package org.gradle;
-public class Person {
- String getName() {
+public class Person implements Nameable {
+ public String getName() {
return "java person";
}
}
@@ -0,0 +1,5 @@
+package org.gradle;
+
+interface Nameable {
+ String getName();
+}

0 comments on commit 6aeeac6

Please sign in to comment.