Using Error-Prone

Error Prone is static anaylsis tool for Java. It hooks into the compilation process. To use in your maven build you need do some configuration for the maven compiler plugin

An example of error prone being setup for maven compiler plugin

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.2</version>
                <configuration>
                    <compilerId>javac-with-errorprone</compilerId>
                    <forceJavacCompilerUse>true</forceJavacCompilerUse>
                    <source>8</source>
                    <target>8</target>
                    <compilerArgs>
                        <!-- Promote this check to Error level for bug patters see http://errorprone.info/bugpatterns -->
                        <arg>-Xep:MissingOverride:ERROR</arg>
                    </compilerArgs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-compiler-javac-errorprone</artifactId>
                        <version>2.8.2</version>
                    </dependency>
                    <!-- override plexus-compiler-javac-errorprone's dependency on
                        Error Prone with the latest version -->
                    <dependency>
                        <groupId>com.google.errorprone</groupId>
                        <artifactId>error_prone_core</artifactId>
                        <version>2.0.21</version>
                    </dependency>
                </dependencies>
            </plugin>

In the above we increased the severity error-prone’s MissingOverride bugpattern to Error (error-prone default was WARNING)

This how the output looks like if now in maven’s compilation goal a class that has a missing @Override annotation

Another feature of error-prone is that it has Intellij Integration
You need to install the https://plugins.jetbrains.com/plugin/7349-error-prone-compiler-integration once that’s done you can change the compiler that Intellij uses

Now within Intellij you will get the same compilation errors as you get from maven

For more details about the installation, see the error-prone documentation http://errorprone.info/docs/installation

Leave a Reply

Your email address will not be published. Required fields are marked *