【问题标题】:Checker Framework, -Xlint:all and JUnit检查器框架、-Xlint:all 和 JUnit
【发布时间】:2018-11-11 04:49:39
【问题描述】:

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

  1. 使用the Checker Framework
  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

如何避免这个警告?

【问题讨论】:

    标签: java maven junit checker-framework


    【解决方案1】:

    找到了解决方案:可以使用-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>
    

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 2013-02-28
      • 1970-01-01
      • 2017-09-13
      • 2021-01-17
      • 1970-01-01
      • 2022-06-13
      • 2013-10-19
      • 1970-01-01
      相关资源
      最近更新 更多