4

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

  1. 检查器框架的使用。
  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.junit.jupiter.api.Test

如何避免这个警告?

4

1 回答 1

3

找到了解决方案:这个特定的警告可以通过以下方式静音-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>
于 2018-11-11T04:52:57.523 回答