Skip to content

Instantly share code, notes, and snippets.

@1010sachin
Last active October 24, 2019 19:00
Show Gist options
  • Save 1010sachin/b4f477d7a0ab1357b39200a93186f0e8 to your computer and use it in GitHub Desktop.
Save 1010sachin/b4f477d7a0ab1357b39200a93186f0e8 to your computer and use it in GitHub Desktop.
SOLVED: Building manta-monitor with javac with error prone on JAVA 11

Problem Statement

Building the joyent/manta-monitor with JAVA 11, out of the box results in complie time errors something like:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project manta-monitor: Compilation failure
[ERROR] /Users/sachingupta/Documents/Joyent/java-manta-monitor/manta-monitor/src/main/java/com/joyent/manta/monitor/JettyServerBuilderModule.java:[18,8] cannot access java.lang.Object
[ERROR]   bad class file: /modules/java.base/java/lang/Object.class
[ERROR]     class file has wrong version 55.0, should be 53.0
[ERROR]     Please remove or make sure it appears in the correct subdirectory of the classpath.

These are the details of the maven/java env of the build system

$ java -version
openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment Zulu11.35+13-CA (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM Zulu11.35+13-CA (build 11.0.5+10-LTS, mixed mode)

$ javac -version
javac 11.0.5

$ mvn -version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T11:33:14-07:00)
Maven home: /Users/sachingupta/opt/apache-maven-3.5.4
Java version: 11.0.5, vendor: Azul Systems, Inc., runtime: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.15", arch: "x86_64", family: "mac"

Cause of the error

The error occurs due the usage of javac with error prone inorder to complie the project, which apparently is not compatible with java11, right out the box.

Some background

Error Prone is a static analysis tool for Java that catches common programming mistakes at compile-time. ErrorProne uses a patched version of JavaC, based on a post-8 pre-9 version of OpenJDK (see https://github.com/google/error-prone-javac). This is only needed for pre-9 JDKs though (and would actually fail with JDK 10+). plexus-compiler-javac-errorprone uses a custom classloader to give priority to the com.google.errorprone:javac over the system/bootstrap classpath. See the bottom of https://errorprone.info/docs/installation which still describes this.

ErrorProne (since 2.1 or so) no longer supports Java 7, support has been added for -Xplugin: so it's easier to integrate it into your builds. The approach using -Xplugin: requires you to prepend com.google.errorprone:javac to the bootstrap classpath (through -Xbootclasspath/p:) when using Java 8. This in turns requires forking a JVM (each time you need to call it when using Maven). Hence, it made sense to use plexus-compiler-javac-errorprone so that we don't ork out the JVM everytime, on Java 8.

Java 11+ requires using the -Xplugin: approach, and doesn't need anything else (you can even exclude com.google.errorprone:javac from being brought in transitively by com.google.errorprone:error_prone_core; the net.ltgt.errorprone does this automatically for instance, and requires you to configure the com.google.errorprone:javac dependency explicitly, in a specific configuration dedicated to be prepended to the bootstrap classpath)

Solution

Update the maven-compiler-plugin to 3.8.0

<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>

The following changes to the maven-complier plugin in the POM.xml solves the above issue in JAVA 11:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <forceJavacCompilerUse>true</forceJavacCompilerUse>
                    <release>11</release>
                    <showWarnings>true</showWarnings>
                    <compilerArgs>
                        <arg>-XDcompilePolicy=simple</arg>
                        <arg>-Xplugin:ErrorProne</arg>
                    </compilerArgs>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>com.google.errorprone</groupId>
                            <artifactId>error_prone_core</artifactId>
                            <version>2.3.3</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment