Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
monperrus opened this issue Apr 5, 2018 · 6 comments
Closed

Comments

@monperrus
Copy link

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?

@Stephan202
Copy link
Contributor

Stephan202 commented Apr 5, 2018

@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.)

@monperrus
Copy link
Author

Thanks a lot!

@namannigam-zz
Copy link

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

@Stephan202
Copy link
Contributor

@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 :)

@tbroyer
Copy link
Contributor

tbroyer commented Oct 25, 2019

@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>).

@Stephan202
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants