Error-prone: question: How to use error-prone in maven without changing the compiler?

Created on 5 Apr 2018  Â·  6Comments  Â·  Source: google/error-prone

I want to use error-prone on my Java project.

However, I am reluctant to to change the compiler configuration and want to keep the normal javac configuration from maven-compiler-plugin.

Basically, it means using use error-prone in the verify phase, as for instance with the findbugs or checkstyle plugin.

How to use error-prone in maven without changing the compiler?

Most helpful comment

@monperrus: you'd basically want to recompile the code in the verify phase, targeting a different output directory. Something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <executions>
        <execution>
            <id>compile-with-error-prone</id>
            <phase>verify</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <compilerArgs>
                    <arg>-d</arg>
                    <arg>${project.build.directory}/classes-ep</arg>
                </compilerArgs>
                <compilerId>javac-with-errorprone</compilerId>
                <forceJavacCompilerUse>true</forceJavacCompilerUse>
                <!-- Erroneously inverted logic... for details, see
                https://jira.codehaus.org/browse/MCOMPILER-209 -->
                <useIncrementalCompilation>true</useIncrementalCompilation>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.google.errorprone</groupId>
            <artifactId>error_prone_core</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac-errorprone</artifactId>
            <version>2.8.2</version>
        </dependency>
    </dependencies>
</plugin>

(I tested the above config by modifying an existing project, so I might have missed a detail. But it should help you on your way.)

All 6 comments

@monperrus: you'd basically want to recompile the code in the verify phase, targeting a different output directory. Something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <executions>
        <execution>
            <id>compile-with-error-prone</id>
            <phase>verify</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <compilerArgs>
                    <arg>-d</arg>
                    <arg>${project.build.directory}/classes-ep</arg>
                </compilerArgs>
                <compilerId>javac-with-errorprone</compilerId>
                <forceJavacCompilerUse>true</forceJavacCompilerUse>
                <!-- Erroneously inverted logic... for details, see
                https://jira.codehaus.org/browse/MCOMPILER-209 -->
                <useIncrementalCompilation>true</useIncrementalCompilation>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.google.errorprone</groupId>
            <artifactId>error_prone_core</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac-errorprone</artifactId>
            <version>2.8.2</version>
        </dependency>
    </dependencies>
</plugin>

(I tested the above config by modifying an existing project, so I might have missed a detail. But it should help you on your way.)

Thanks a lot!

@Stephan202 and where does one exactly configure the <compilerId>javac-with-errorprone</compilerId>?

@namannigam that ID can be any value; it doesn't need to be referenced anywhere else. (Well it's a bit more subtle than that, but don't worry about it.)

If you're asking where this whole blob of XML should best live, I'd recommend to put it inside your parent pom.xml's pluginManagement section:

<project ...>
    ...
    <build>
        <pluginManagement>
            <plugins>
                <!-- here --
            </plugins>
        </pluginManagement>
    </build>
    ...
</project>

But if you have a single pom.xml without a pluginManagement second, you can also configure it directly inside build:

<project ...>
    ...
    <build>
        <plugins>
            <!-- here -->
        </plugins>
    </build>
    ...
</project>

If that doesn't answer your question, I need some more details :)

@Stephan202 Did you misread <id>compile-with-error-prone</id> for <compilerId>javac-with-errorprone</compilerId>‽ Because indeed the <id> of an <execution> can be anything (unless you really want a specific known value), but the <compilerId> of the maven-compiler-plugin's <configuration> needs to be that specific value.

Please note however that this way of configure the maven-compiler-plugin to use ErrorProne won't work with Java 11+; you'd better use the new -Xplugin:ErrorProne way, as explained in https://errorprone.info/docs/installation#maven (that doesn't change how/where you put the <execution>s, only what you put into the <configuration> (and <dependencies>).

Doh, @tbroyer I did misread indeed! Thanks for noticing :)

Was this page helpful?
0 / 5 - 0 ratings