-
Notifications
You must be signed in to change notification settings - Fork 193
Groovy Eclipse Maven plugin
Groovy-Eclipse provides a compiler adapter plugin for Maven, making it is possible to co-locate and compile your Groovy and Java sources using the Groovy-Eclipse batch compiler.
Current version of the groovy-eclipse-compiler
adapter:
Current version of the groovy-eclipse-batch
compiler:
Complete POM examples can be found in the groovy-eclipse-compiler-tests project.
Many versions of the compiler plugins are available from Maven Central. To enable retrieval of the newest batch compiler and compiler adapter artifacts, add a plugin repository for Groovy's Artifactory:
<pluginRepositories>
<pluginRepository>
<id>groovy-plugins-release</id>
<url>https://groovy.jfrog.io/artifactory/plugins-release</url>
</pluginRepository>
...
</pluginRepositories>
In the build plugin section, 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 groovy-eclipse-batch
artifact):
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version><!-- 3.6.2 is the minimum -->
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<compilerArguments>
<indy/><!-- optional; supported by batch 2.4.12-04+ -->
<configScript>config.groovy</configScript><!-- optional; supported by batch 2.4.13-02+ -->
</compilerArguments>
<failOnWarning>true</failOnWarning><!-- optional; supported by batch 2.5.8-02+ -->
<fork>true</fork><!-- optional; enables Parrot Parser by default for Groovy 3+ -->
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>4.0.24-03</version><!-- "01" for JDK 8; "02" for JDK 11; "03" for JDK 17+ -->
</dependency>
</dependencies>
</plugin>
</plugins>
...
</build>
This will allow Groovy files to be compiled. The groovy-eclipse-compiler
recognizes most <configuration>
elements supported by the maven-compiler-plugin. And groovy-eclipse-batch
supports many compiler arguments in addition to indy
and configScript
.
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.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.24</version>
</dependency>
...
</dependencies>
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/main/java
and 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 standardized approach, then it is possible to put your Groovy files in src/main/groovy
and src/test/groovy
and your Java files in src/main/java
and 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 the relationship between src/test/java
and src/test/groovy
.
(You only need to do this if your project has an empty src/main/java
or src/test/java
.)
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>3.9.0</version>
<extensions>true</extensions>
</plugin>
...
</build>
The <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 to do this if your project has an empty src/main/java
or src/test/java
.)
The 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>3.3.0</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! With this approach you are not limited to using src/main/groovy
and src/test/groovy
as the locations for Groovy sources.
GMaven 2.x 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 compilation is no longer supported:
- The compiler plugin does not require the creation of Java stubs so that your Groovy files can compile against Java files. This will prevent some arcane compile errors from appearing.
- The Groovy-Eclipse compiler is the same inside Eclipse and inside Maven, and so configuration across the two platforms can be simplified.
- The compiler plugin is a standard compiler plugin for Maven. It therefore follows and allows all the same standard configuration that the
javac
compiler plugin uses. This makes it simpler to introduce Groovy into an existing Maven project. All you need to do is change the compiler plugin that the pom references.
There are still some reasons to use GMaven or GMavenPlus:
- GroovyDoc tool is not supported because the compiler plugin does not produce stubs.
- Groovy Mojos are not supported.
- Groovy scripts cannot be executed in your POMs.
-
InvokeDynamic is not supported.As of 2.4.12-04 indy is supported. - Groovy Android is not supported.
-
Configuration scripts are not supported.As of 2.4.13-02 config scripts are supported. - Interactive shell/console is not supported.
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>1.18.24</version>
</dependency>
Also, the following configuration needs to be added to the maven-compiler-plugin
configuration:
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<compilerArguments>
<!-- consult Lombok docs to confirm agent class name for your version -->
<javaAgentClass>lombok.launch.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 m2e. This feature is available on all Groovy-Eclipse update sites. 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 feature.
The Groovy-Eclipse configurator for m2eclipse is not compatible with AspectJ or Scala (citation needed). So you cannot use a joint AspectJ/Groovy/Scala project in Eclipse. These languages must be isolated into separate projects.