25

I am trying:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Werror</compilerArgument>
                <fork>true</fork>
            </configuration>
        </plugin>

but with no joy. Any ideas now to get medieval on such errors as suggested at this blog post?

peterh
  • 11,875
  • 18
  • 85
  • 108
simbo1905
  • 6,321
  • 5
  • 58
  • 86
  • There was a bug as is mentioned below that is no longer a problem. This form as specified with the compilerArgument works great. – Ben Mathews Mar 26 '15 at 18:48

7 Answers7

34

Update for the year 2015, using Maven 3.3 and Java 8.

Here's a minimal compiler configuration that enables all warnings and makes the build fail whenever warnings occur.

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <showWarnings>true</showWarnings>
            <compilerArgs>
                <arg>-Xlint:all</arg>
                <arg>-Werror</arg>
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>

Bits of note:

  • <showWarnings>true</showWarnings> is required. For reasons unknown, Maven by default actively suppresses warnings with the -nowarn flag, so the -Xlint and -Werror flags would be ignored.
  • showDeprecation doesn't need to be enabled because -Xlint:all already emits deprecation warnings.
  • Experimentation shows that fork doesn't need to be enabled, even though the documentation says otherwise.
glts
  • 21,808
  • 12
  • 73
  • 94
  • I like to leave out the `groupId` for built-in Maven plugins to emphasise their special standing as built-ins. The built-in Maven plugins all have the default group ID `org.apache.maven.plugins`, so it isn’t necessary to specify it in the POM. (Try `mvn help:effective-pom` to verify this.) – glts Feb 28 '16 at 14:40
9

New in maven-compiler-plugin 3.6.0: the failOnWarning flag. This worked for me:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
      <execution>
        <id>compile</id>
        <phase>process-sources</phase>
        <goals>
          <goal>compile</goal>
        </goals>
        <configuration>
          <compilerArgument>-Xlint:-processing</compilerArgument>
          <failOnWarning>true</failOnWarning>
        </configuration>
      </execution>
    </executions>
  </plugin>

Note that I had to exclude the processing lint or otherwise auto-matter's annotations would break the build with cryptic "symbol not found" errors.

mpavlov
  • 866
  • 10
  • 13
  • 2
    The above answer works, though I tried making it work without the `-Xlint` setting and that was not successful. I found `true` is the same as ` -Werror`. By itself `-Werror` doesn't do anything as it needs to be paired with `-Xlint:all`. So by itself `true` doesn't do anything either. – Brad Cupit Aug 24 '17 at 15:37
3

An update on 2020, I am using Spring Boot 2.2.5. The following config can stop the mvn install when warning, error happen.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <failOnError>true</failOnError>
        <failOnWarning>true</failOnWarning>
    </configuration>
 </plugin>
Eric Tan
  • 1,377
  • 15
  • 14
2

EDIT: This answer is outdated however I can't delete it as it was an accepted answer at the time.

This a bug with Maven see: https://issues.apache.org/jira/browse/MCOMPILER-120 it's been fixed in 2.4 of the Maven-compiler-plugin but I don't believe that's been released yet. tag won't work either unfortunately.

turbanoff
  • 2,439
  • 6
  • 42
  • 99
alex.p
  • 2,627
  • 17
  • 28
0

I arrived at this post when I was looking for a working solution to fail my maven builds for compiler warnings

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <fork>true</fork>
        <compilerArgs>
            <arg>-verbose</arg>
            <arg>-Werror</arg> 
            <arg>-Xlint:all</arg>
        </compilerArgs>
    </configuration>
</plugin>

For more explanation in detail please refer the link below

Treat warnings As Errors

Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
0

There is an alternate form perhaps give it a try? Note the s on the end of <compilerArguments>

<configuration>
    <compilerArguments>
        <Werror />
    </compilerArguments>
</configuration>
JohnKlehm
  • 2,368
  • 15
  • 9
0

By using the workaround in this comment in the open jira issue for maven compiler plugin, the build can be failed for compiler warning.

This works for me:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <compilerId>javac</compilerId>
            <source>1.6</source>
            <target>1.6</target>
            <compilerArgument>-Werror</compilerArgument>
            <showDeprecation>true</showDeprecation>
        </configuration>

        <dependencies>
           <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-api</artifactId>
                <version>1.8.2</version>
                <exclusions>
                  <exclusion>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-component-api</artifactId>
                  </exclusion>
                </exclusions>
           </dependency>
           <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-manager</artifactId>
                <version>1.8.2</version>
                <exclusions>
                  <exclusion>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-component-api</artifactId>
                  </exclusion>
                </exclusions>
           </dependency>
           <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-javac</artifactId>
                <version>1.8.2</version>
                <scope>runtime</scope>
                <exclusions>
                  <exclusion>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-component-api</artifactId>
                   </exclusion>
                </exclusions>
          </dependency>
       </dependencies>
    </plugin>
Raghuram
  • 51,854
  • 11
  • 110
  • 122