检测到旧版本的 checkstyle考虑更新到 >= v8.30 步骤 1步骤 2步骤 3

问题描述

关于 SonarQube + Checkstyle 警告的小问题。

目前,在我的应用程序中,在我的 pom 中,我使用以下 Checkstyle 插件

          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <outputFile>.out/reports/checkstyle/checkstyle-result.xml</outputFile>
                    <outputDirectory>target/reports/checkstyle</outputDirectory>
                    <outputFileFormat>xml</outputFileFormat>
                </configuration>
            </plugin>

这个插件正在发挥作用,不用担心。

不过,当我运行 SonarQube 时,我收到此警告

Old version of checkstyle detected. Consider updating to >= v8.30
For more @R_259_4045@ion see: https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html

我明明去看了那个网站,但还是很难理解。

我拥有的 Checkstyle 插件是最新的,版本 3.1.2,已在 Maven 中心等上检查。

在 SonarQube 中,我运行的是最新版本 8.9 LTS,以及最新版本的 Checkstyle 插件

请问我错过了什么?我是否使用了某种错误插件

解决方法

它是一个名为 sonar-checkstyleSonarQube 插件,需要在 SonarQube 服务器实例上安装或升级。当前版本为 8.40

注意:参考

编辑 1

步骤 1

首先,cache 处有一个 <user_home>/.sonar/cache 目录(对于我在 Windows 10 上是 C:\Users\<myuser>\.sonar\cache),请删除此 sub directories 下的所有 cache目录,目的是让 org.sonarsource.scanner.maven:sonar-maven-plugin 最新版本从我们的 SonarQube 服务器实例下载它,并确保所有相关插件在升级/安装后都是全新的SonarQube 服务器实例。 (不要忘记在完成升级/安装后重新启动它以确保重新加载所有新的)

步骤 2

其次,确保我们没有在我们的项目 org.sonarsource.scanner.maven:sonar-maven-plugin 中指定 pom.xml,无论是在父级还是其他任何地方,目的都是为了确保在执行过程中,它将是最新版本 匹配我们的 SonarQube 服务器实例 version

无论如何,正式文档 (https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-maven/) 也提到了如何修复 Maven 插件版本如下:-

如何修复 Maven 插件版本

建议锁定 Maven 插件的版本:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>
        <!--Version that matched with our Sonar server instance version --> 
        </version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

最新版本可以在https://search.maven.org/artifact/org.codehaus.mojo/sonar-maven-pluginhttps://search.maven.org/artifact/org.sonarsource.scanner.maven/sonar-maven-plugin浏览最新版本3.9.0.2155注意:?.y.z版本与我们的声纳匹配服务器实例版本)

步骤 3

最后但并非最不重要的一点,如果我们的项目是 multi-module projects,那么在正式文件 (https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-maven/) 中提到如下:-

在某些情况下,您可能希望将 sonar:sonar 目标作为 专用步骤。确保使用安装作为多模块的第一步 项目

mvn 全新安装

mvn sonar:sonar ...

这里将有 2 个步骤,先是 mvn clean install 以便完成,然后是 mvn sonar:sonar ...

编辑 2

maven-checkstyle-plugin 还可以指定 https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html 中提到的 checkstyle version,其中重要的句子为

Maven Checkstyle 插件带有一个默认的 Checkstyle 版本:用于 maven-checkstyle-plugin 3.1.2,默认使用Checkstyle 8.29

然后 maven-checkstyle-plugin 的配置将如下所示:-

    <project>
      ...
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
              <version>3.1.2</version>
              <dependencies>
                <dependency>
                  <groupId>com.puppycrawl.tools</groupId>
                  <artifactId>checkstyle</artifactId>
                  <version>...choose your version...</version>
                </dependency>
              </dependencies>
            </plugin>
          </plugins>
        </pluginManagement>
      <build>
      ...
    </project>

最新版本可以在https://search.maven.org/artifact/com.puppycrawl.tools/checkstyle浏览,最新版本是8.42