Configuring the compiler plugin
The compiler plugin has two properties that a pom can set without configuration, the release and the encoding, and a configuration block for everything else. The block is where a project asks javac for its warnings and, if it is strict, turns them into errors. This article adds one argument and then one switch to the compiler of the course project, and reads what the compiler says.
The project
The pom pins the compiler plugin, so the version of the goal is the one in the file and not the default of Maven, and it passes -Xlint:all to javac. The class is deliberately sloppy: a raw List, a raw ArrayList and an unchecked cast are the three warnings a beginner produces most often.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>lint</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.15.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint:all</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/example/lint/Lint.java
package com.example.lint;
import java.util.ArrayList;
import java.util.List;
public class Lint {
public static void main(String[] args) {
List list = new ArrayList();
list.add("x");
String first = (String) list.get(0);
System.out.println(first);
}
}
The warnings
mvn -B compile > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:lint >--------------------------
[INFO] Building lint 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ lint ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ lint ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[WARNING] <project>/src/main/java/com/example/lint/Lint.java:[8,9] found raw type: java.util.List
missing type arguments for generic class java.util.List<E>
[WARNING] <project>/src/main/java/com/example/lint/Lint.java:[8,25] found raw type: java.util.ArrayList
missing type arguments for generic class java.util.ArrayList<E>
[WARNING] <project>/src/main/java/com/example/lint/Lint.java:[9,17] unchecked call to add(E) as a member of the raw type java.util.List
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
Three warnings, and the build succeeds: a warning is not an error unless the project makes it one. Each line is one javac diagnostic with the file and the position, and the continuation line carries the detail, which is the compiler plugin relaying what the compiler printed. The codes behind the categories, which -Xlint:all switched on, are rawtypes for the first two and unchecked for the third.
-Xlint:all is not the default because it is noisy: it also warns about serial version ids, about a module path that could be used, and about a switch that misses a case of an enum. A project that turns it on usually does so with a list of specific categories, or with -Xlint:all,-serial minus the ones its code cannot fix.
failOnWarning
failOnWarning is a parameter of the plugin, not an argument of javac: with it at true the plugin adds -Werror to the command line. The pom, with one line added to the configuration:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>lint</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.15.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint:all</arg>
</compilerArgs>
<failOnWarning>true</failOnWarning>
</configuration>
</plugin>
</plugins>
</build>
</project>
mvn -B clean compile > strict.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" strict.log
status: 1
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:lint >--------------------------
[INFO] Building lint 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ lint ---
[INFO] Deleting <project>/target
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ lint ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ lint ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[INFO] -------------------------------------------------------------
[WARNING] COMPILATION WARNING :
[INFO] -------------------------------------------------------------
[WARNING] <project>/src/main/java/com/example/lint/Lint.java:[8,9] found raw type: java.util.List
missing type arguments for generic class java.util.List<E>
[WARNING] <project>/src/main/java/com/example/lint/Lint.java:[8,25] found raw type: java.util.ArrayList
missing type arguments for generic class java.util.ArrayList<E>
[WARNING] <project>/src/main/java/com/example/lint/Lint.java:[9,17] unchecked call to add(E) as a member of the raw type java.util.List
[INFO] 3 warnings
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] <project>/src/main/java/com/example/lint/Lint.java: warnings found and -Werror specified
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.15.0:compile (default-compile) on project lint: Compilation failure
[ERROR] <project>/src/main/java/com/example/lint/Lint.java: warnings found and -Werror specified
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
The same three warnings are printed, then the compiler reports them again as one error and the plugin turns that into a failed goal. warnings found and -Werror specified is the last useful line of the log; the rest is the usual Help 1 footer.
A project that sets this has to live with every warning of every dependency it compiles from source, and with every deprecation of a JDK it upgrades to. That is the point, and it is why most projects set it in a CI profile rather than in the pom of the build everyone runs.
The other options
Four more parameters of the plugin are worth knowing. source and target are the old pair the properties maven.compiler.source and maven.compiler.target set; release is the modern one, because it also checks that the code does not use an API the release does not have. encoding comes from project.build.sourceEncoding and decides how the bytes of a source file are read. annotationProcessorPaths keeps annotation processors off the class path of the code, which is the fix for a processor that leaks its own dependencies into the project. And compilerId names another compiler than javac, in the rare case a project uses the Eclipse compiler through a toolchain.
Every one of them appears in the debug log of a build with the expression it reads, which the switches article shows and help:describe -Dplugin=compiler -Ddetail lists for a person.
Where to go next
Resources and filtering takes the other plugin of the early phases, the one that copies files and can rewrite them on the way.
Article author: Arthur Isaev