Checker Framework, -Xlint:all and JUnit



我试图从一开始就用非常干净和严格的设置来保持项目,包括:

  1. Checker框架的使用
  2. 启用所有编译器警告并将其视为错误(-Xlint:all-Werror(
  3. JUnit的使用

以下是Maven的pom.xml:中的相关部分

<dependencies>
<!-- Annotations: nullness, etc -->
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>${checkerframework.version}</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>jdk8</artifactId>
<version>${checkerframework.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.checkerframework</groupId>
<artifactId>checker</artifactId>
<version>${checkerframework.version}</version>
</path>
</annotationProcessorPaths>
<annotationProcessors>
<!-- Add all the checkers you want to enable here -->
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker
</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-Xbootclasspath/p:${annotatedJdk}</arg>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

不幸的是,当我引入一个使用@Test注释的测试类时,我会收到以下编译警告,从而出现构建错误:

警告:java:没有处理器声明这些注释中的任何一个:org.unit.jupiter.api.Test

如何避免此警告?

找到了一个解决方案:这个特定的警告可以用-Xlint:-processing:静音

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
...
<compilerArgs>
<arg>-Xbootclasspath/p:${annotatedJdk}</arg>
<arg>-Xlint:all</arg>
<!-- Silence warning "No processor claimed any of these annotations". One of the
annotations that would trigger it is org.junit.jupiter.api.Test -->
<arg>-Xlint:-processing</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

最新更新