6

请关于 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 information see: https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html

我显然去阅读了该网站,但我仍然很难理解。

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

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

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

4

1 回答 1

3

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

注:参考

编辑 1

步骤1

首先,有一个cache目录<user_home>/.sonar/cache(对我来说Windows 10是C:\Users\<myuser>\.sonar\cache),请删除sub directoriescache目录下的所有内容,以便让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与我们的Sonar服务器实例版本匹配

第 3 步

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

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

mvn 干净安装

mvn 声纳:声纳 ...

那么这里会有2个步骤,mvn clean install先让完成,然后mvn sonar:sonar ...再做。

编辑 2

maven-checkstyle-plugin还可以指定https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.htmlcheckstyle 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

于 2021-05-15T05:21:33.563 回答