rvesse / maven-compiler-warning-bug

Demonstrates an odd issue with the Maven compiler plugin and treatment of warnings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction

This is a trivial repository that shows a strange issue with the maven compiler plugin and its treatment of warnings. While obviously you should ideally avoid code that generates warnings this particular example is based on real code auto-generated by the Thrift compiler so it cannot be safely reworked to avoid the issue.

The Issue

It contains a single source file that will produce Java compiler warnings (about unchecked or unsafe operations) and has two executions of the compiler plugin. One should run with no warnings while the other with full warnings.

> mvn clean compile

However the execution without any warnings may produce a warning despite the plugin settings saying that it should not. The exact behaviour will depend on the version of the compiler plugin used.

What is wrong?

Doing some digging this appears to be done to the fact that the warnings appearing in the Maven output are actually compiler "notes" and not warnings but Maven interprets them as warnings.

For example if you run the same command that Maven invokes javac with directly:

> javac -d target/classes -classpath target/classes: -sourcepath src/main/java: -g -nowarn -target 1.5 -source 1.5 -encoding UTF-8 -Xlint:none src/main/java/example/DemonstrateMavenCompilerWarningsBug.java

Then it produces this output:

Note: src/main/java/example/DemonstrateMavenCompilerWarningsBug.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

Here we can see that these are notes. However the maven compiler plugin appears to interpret this as a WARNING which is irritating to say the least especially if we've set the <showWarnings> configuration for the plugin to false (which also is the default behaviour).

This is also discussed in MCOMPILE-224 though not as clearly as here hence why this repository exists.

Incorrect behaviour is version dependent

To complicate things the behaviour around compiler notes is inconsistent between versions. The project uses 3.1 as the default plugin version (which was the default my local Maven installation gave me if I didn't specify a version) which does show a warning.

However with 3.2 the extra warnings become informational messages instead which is much more appropriate behaviour e.g.

> mvn clean compile -Dplugin.compiler.version=3.2

If you go back further and use 2.5.1 then compiler output doesn't appear to get properly handled at all since we see no warnings even in the full warnings case:

> mvn clean compile -Dplugin.compiler.version=2.5.1

How to fix?

Simply use the latest version of the compiler plugin (3.2 at time of writing) explicitly in your POMs

About

Demonstrates an odd issue with the Maven compiler plugin and treatment of warnings


Languages

Language:Java 100.0%