'Maven. Unable to compile module with groovy-eclipse compiler

I'm trying to perform migration of my project from java 8 to java 11. What I did in root pom.xml:

  1. Replaced
<source>8</source>
<target>8</target>

With

<release>11</release>
  1. Configured maven-toolchain-plugin
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>toolchain</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <toolchains>
                        <jdk>
                            <version>[11,12)</version>
                        </jdk>
                    </toolchains>
                </configuration>
            </plugin>

After that modules which don't use groovy compiled without problems. But few modules in this project use groovy.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>3.6.0-03</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>3.0.3-01</version>
                    </dependency>
                </dependencies>
            </plugin>

I get following error:

[INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files
[DEBUG] Compiling 15 source files to D:\Projects\dxcore-java11\fix-markets\fix-server-tests\target\classes
[DEBUG] Command line options:
[DEBUG] -cp D:\Projects\dxcore-java11\fix-markets\target\classes;C:\Users\turbanov\.m2\repository\org\codehaus\groovy\groovy\3.0.3\groovy-3.0.3.jar;<a_lot_of_jars_here>; -d D:\Projects\dxcore-java11\fix-markets\target\classes -s D:\Projects\dxcore-java11\fix-markets\target\generated-sources\annotations -g -encoding UTF8 --release 11 -nowarn D:\Projects\dxcore-java11\fix-markets\src\client\TestClient.groovy
[INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files
[DEBUG] Compiling 15 source files to D:\Projects\dxcore-java11\fix-markets\fix-server-tests\target\classes
[INFO] Compiling in a forked process using C:\Users\turbanov\.m2\repository\org\codehaus\groovy\groovy-eclipse-batch\3.0.3-01\groovy-eclipse-batch-3.0.3-01.jar
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing groovy-eclipse compiler:
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project fix-server-tests: Compilation failure
Failure executing groovy-eclipse compiler:
option --release is supported only when run with JDK 9 or above


    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:190)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:186)
    at java.util.concurrent.FutureTask.run (FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511)
    at java.util.concurrent.FutureTask.run (FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
    at java.lang.Thread.run (Thread.java:748)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure

What is wrong with my configuration? Does groovy-eclipse-compiler supports such configuration?

What is strange is that if I replace <release> back to use <source>11</source>/<target>8</target> all build fine.



Solution 1:[1]

I'm trying to migration my project from java 8 to java 11.

I want to use new language features and new API.

The simplest solution for this migration would be to use Java 11 for Maven invocation. There is no need to use maven-toolchains-plugin in this case.

As for the errors reported by the groovy-eclipse-compiler - every example in the Release new version of Groovy Eclipse Maven plugin to support Java 11 issue is using maven-compiler-plugin with either explicit configuration like this:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>11</source>
        <target>11</target>
    </configuration>
</plugin>

or with implicit configuration like this:

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

I suggest to follow these examples and drop <release> configuration completely.

Another suggestion would be to use maven-enforcer-plugin to enforce the minimum version of Java used for Maven invocation.

Solution 2:[2]

As @emiles commented above:

Yes, I see now after some research that groovy-eclipse-compiler is not toolchain-aware. I have created this github issue to track the addition of this capability: github.com/groovy/groovy-eclipse/issues/1232

He put the fix in on 13 March into groovy-eclipse-compiler 3.7.0. I was having this selfsame issue on an AEM build, compiling groovy on an AEM Cloud Manager install that has both JDK8 and JDK11 installed, using the maven-toolchains-plugin to switch.

Upgrading to groovy-eclipse-compiler 3.7.0 and groovy-eclipse-batch 3.0.7-02 fixed the issue for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Illya Kysil
Solution 2 Tad