Compiler options
Every option of javac has a place in the JavaCompile tasks of a build. Two of them are worth setting in every project: the release the code targets, and the warnings the compiler prints. This lesson turns both on and then makes the same build fail on a warning.
The block
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java'
}
version = '1.0.0'
tasks.withType(JavaCompile).configureEach {
options.release = 17
options.compilerArgs += ['-Xlint:all']
}
tasks.withType(JavaCompile) reaches compileJava and compileTestJava at once, and every compile task a plugin adds later. The test sources are compiled with the same release, so a test cannot use an API the main code could not.
src/main/java/com/example/Names.java
package com.example;
import java.util.ArrayList;
import java.util.List;
public final class Names {
public static List<String> of(String... names) {
List list = new ArrayList();
for (String name : names) {
list.add(name);
}
return list;
}
}
The warnings
gradle compileJava --console=plain --offline --no-daemon > build.log 2>&1; sed "s#$PWD#<project>#g" build.log
<project>/src/main/java/com/example/Names.java:8: warning: [rawtypes] found raw type: List
List list = new ArrayList();
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
<project>/src/main/java/com/example/Names.java:8: warning: [rawtypes] found raw type: ArrayList
List list = new ArrayList();
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
<project>/src/main/java/com/example/Names.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
list.add(name);
^
where E is a type-variable:
E extends Object declared in interface List
<project>/src/main/java/com/example/Names.java:12: warning: [unchecked] unchecked conversion
return list;
^
required: List<String>
found: List
4 warnings
[Incubating] Problems report is available at: file://<project>/build/reports/problems/problems-report.html
BUILD SUCCESSFUL in 13s
1 actionable task: 1 executed
Four warnings, all from the raw List: List and ArrayList have no element type, the call to add cannot be checked, and the return converts a raw list into a typed one. The code compiles and the build succeeds, because a warning is a warning. The line 4 warnings is the one to look for in a log of a project that just turned -Xlint:all on.
Warnings as errors
-Werror turns every warning into an error. The build script gains one argument:
build.gradle
plugins {
id 'java'
}
version = '1.0.0'
tasks.withType(JavaCompile).configureEach {
options.release = 17
options.compilerArgs += ['-Xlint:all', '-Werror']
}
gradle compileJava --console=plain --offline --no-daemon > build.log 2>&1; sed "s#$PWD#<project>#g" build.log
<project>/src/main/java/com/example/Names.java:8: warning: [rawtypes] found raw type: List
List list = new ArrayList();
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
<project>/src/main/java/com/example/Names.java:8: warning: [rawtypes] found raw type: ArrayList
List list = new ArrayList();
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
<project>/src/main/java/com/example/Names.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
list.add(name);
^
where E is a type-variable:
E extends Object declared in interface List
<project>/src/main/java/com/example/Names.java:12: warning: [unchecked] unchecked conversion
return list;
^
required: List<String>
found: List
error: warnings found and -Werror specified
1 error
4 warnings
[Incubating] Problems report is available at: file://<project>/build/reports/problems/problems-report.html
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava' (registered by plugin class 'org.gradle.api.plugins.JavaBasePlugin').
> Compilation failed; see the compiler output below.
<project>/src/main/java/com/example/Names.java:8: warning: [rawtypes] found raw type: List
List list = new ArrayList();
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
<project>/src/main/java/com/example/Names.java:8: warning: [rawtypes] found raw type: ArrayList
List list = new ArrayList();
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
<project>/src/main/java/com/example/Names.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
list.add(name);
^
where E is a type-variable:
E extends Object declared in interface List
<project>/src/main/java/com/example/Names.java:12: warning: [unchecked] unchecked conversion
return list;
^
required: List<String>
found: List
error: warnings found and -Werror specified
1 error
4 warnings
* Try:
> Check your code and dependencies to fix the compilation error(s)
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
BUILD FAILED in 12s
1 actionable task: 1 executed
The same four warnings, now the reason the build stopped: the compiler exits with a failure, the task fails, and everything that depended on it does not run. That is how a project keeps a warning out of a release, and it is the reason -Xlint:all belongs in the same commit as -Werror.
Related
- Toolchains and sourceCompatibility: which JDK compiles, and which version the class files target.
- The errors a beginner meets: a release the JDK cannot compile.
- The Maven compiler plugin: the same two options in a pom.
Article author: Arthur Isaev