Groovy-Eclipse provides a compiler plugin for Maven. Using the compiler plugin, it is possible to compile your maven projects using the Groovy-Eclipse compiler.
The most recent version of the Groovy-Eclipse-Compiler plugin for maven is 2.9.1-01.
The most recent version of the groovy-eclipse-batch artifact is 2.3.7-01.
They are both available from maven central.
In your plugin section, you must change the compiler used by the maven-compiler-plugin. Like the javac ant task, the maven-compiler-plugin does not actually compile, but rather delegates the compilation to a different artifact (in our case, the
<build> ... <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher --> <version>3.1</version> <configuration> <compilerId>groovy-eclipse-compiler</compilerId> <!-- set verbose to be true if you want lots of uninteresting messages --> <!-- <verbose>true</verbose> --> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.9.1-01</version> </dependency> <!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch --> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> <version>2.3.7-01</version> <!-- or choose a different compiler version --> <!-- <version>2.1.8-01</version> --> </dependency> </dependencies> </plugin> ... </plugins> </build>
This will allow Groovy files to be compiled. The groovy-eclipse-compiler recognizes all settings supported by the maven-compiler-plugin.
Remember that you still need to specify a groovy artifact as a build dependency in addition to the
maven-compiler-plugin dependency. The groovy dependency version should match the compiler version. Something like this:
<dependencies> ... <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.3.7</version> </dependency> ... </dependencies>
Note that the groovy-eclipse-compiler and groovy-eclipse-batch artifacts are available in Maven-central, so there is no need to explicitly declare any extra repositories.
There are several ways to set up your maven project to recognize Groovy source files
The simplest way to set up your source folders is to do nothing at all: add all of your Groovy files to
src/test/java. This requires absolutely no extra configuration and is easy to implement. However, this is not a standard maven approach to setting up your project. If you require a more standard maven approach, then it is possible to put your Groovy files in
src/test/groovy and you Java files in
src/test/java. There are several ways of doing this.
If there is at least one file (Java or not) in
src/main/java, then all files in
src/main/groovy will be found. If, however,
src/main/java is empty, then
src/main/groovy will be ignored. You can get around this by placing an empty file in
src/main/java just so that
src/main/groovy will be recognized. The same is true for
src/test/groovy. This is actually a workaround for GRECLIPSE-1221.
groovy-eclipse-compilermojo for configuring source folders
(You only need this approach if your project has an empty
If your project has no Java files and you don't want to add an empty file in
src/main/java, then you can configure source files by referencing the groovy-eclipse-compiler mojo. Just add this to the
plugins section of your pom:
<build> ... <plugin> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.9.1-01</version> <extensions>true</extensions> </plugin> ... </build>
<extensions>true</extensions> section is important because this redefines the default lifecycle of your project so that an extra phase is added. This phase has an extra goal attached to it that adds the two Groovy source folders.
(You only need this approach if your project has an empty
build-helper-maven-plugin allows you to do things like adding extra source folders to your project without needing to redefine the default lifecycle. You need to add this configuration to your build plugin section:
<build> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/main/groovy</source> </sources> </configuration> </execution> <execution> <id>add-test-source</id> <phase>generate-test-sources</phase> <goals> <goal>add-test-source</goal> </goals> <configuration> <sources> <source>src/test/groovy</source> </sources> </configuration> </execution> </executions> </plugin> ... </build>
The benefit of using this approach is that you do not need to make any changes to the default lifecycle. The downside is, of course, that you need 31 lines of configuration to do this!
There is an archetype available for this project. You can use it through a command like this:
mvn archetype:generate \ -DarchetypeGroupId=org.codehaus.groovy \ -DarchetypeArtifactId=groovy-eclipse-quickstart \ -DarchetypeVersion=2.5.2-01 \ -DgroupId=foo \ -DartifactId=bar \ -Dversion=1 \ -DinteractiveMode=false \ -DarchetypeRepository=https://nexus.codehaus.org/content/repositories/snapshots/
(This archetype is not actively maintained.)
A sample project using the compiler plugin as well as the source code for the plugin itself are available from the github repository:
The full URL to the maven projects is:
There are several projects in the repository:
The sample project and archetype is not maintained as well as we would like. Some community help with this would be greatly appreciated. Please see GRECLIPSE-1285.
GMaven 2.0 and later no longer supports compilation. You can read more about this on the GMaven 2 project page. GMaven 1.x is now deprecated.
GMaven 1.x had limitations over the groovy-eclipse-compiler and for the following reasons GMaven 2.0 compilation is no longer supported:
There are still some reasons to use GMaven:
Whether or not the Groovy-Eclipse compiler plugin for Maven is appropriate for your project will depend on your requirements.
Project Lombok is compatible with the groovy-eclipse-compiler. There is some extra configuration that you need to do. The lombok jar needs to be added to both the build and compile dependencies sections:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>0.10.4</version> </dependency>
Also, the following configuration needs to be added to the maven-compiler-plugin configuration:
<configuration> <compilerId>groovy-eclipse-compiler</compilerId> <verbose>true</verbose> <compilerArguments> <javaAgentClass>lombok.core.Agent</javaAgentClass> </compilerArguments> <fork>true</fork> </configuration>
If you are going to be working with your maven project inside of Eclipse, it is strongly recommended that you use m2eclipse. And to use your Groovy projects with m2eclipse, you will need to install the Groovy-Eclipse configurator for m2eclipse. This feature is available any of the Groovy-Eclipse update sites (e.g., nightly, milestone, or release). Just go to your Eclipse update manager and add the Groovy-Eclipse update sites (if you haven't done so already). Select the Groovy-Eclipse M2E integration.
|Eclipse level||Release update site|
|Eclipse level||Development update site|
The Groovy-Eclipse configurator for m2eclipse is not compatible with AspectJ or Scala. So you cannot use a joint AspectJ/Scala/Groovy project in Eclipse. These languages must be separated into separate sub-projects.
The compiler plugin was originally described here and here, but these posts are no longer updated and this page will always contain the more recent information.