Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a Java maven build to fail for compiler warnings?

Tags:

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?

like image 767
simbo1905 Avatar asked Feb 08 '12 11:02

simbo1905


People also ask

Does Maven require compiler plugin?

Maven Compiler Plugin might be the most important plugin in Maven. It is used to compile the sources of your project, which transform Java files ( *.

How do I change my default Maven compiler?

Show activity on this post. Create a pom-only ( <packaging>pom</packaging> ) project that has the compiler settings (and any other default settings) you want. You give treat it like any other project (release it; deploy it to your Maven repo, etc.). It doesn't help much if all you want to set is compiler settings.


2 Answers

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.
like image 112
glts Avatar answered Oct 22 '22 09:10

glts


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.

like image 42
mpavlov Avatar answered Oct 22 '22 08:10

mpavlov