梦想还是要有的,万一忘了咋办?

0%

自定义Java 代码规范-sonar

目录

  • 软件版本
  • 如何自定义规则
  • 确定sonar-java 版本
  • 编译运行
  • 安装插件
  • 问题总结

软件版本

软件 版本 说明
sonarqube 9.9.1-community
postgres 15.3-bookworm 登记下,用不上

如何自定义规则

通过阅读 官方文档 如何自定义规则知晓,下载一个demo 仓库进行改造就可以。实际阅读发现 帮助文档 并没有紧跟这软件版本发布,有延迟。

1 、在主分支帮助文档中每个版本都有这样一句话。

1
2
# https://github.com/SonarSource/sonar-java/blob/master/docs/CUSTOM_RULES_101.md
Looking inside the pom, you will see that both SonarQube and the Java Analyzer versions are hard-coded. This is because SonarSource's analyzers are directly embedded in the various SonarQube versions and are shipped together. For instance, SonarQube 8.9 (previous LTS) is shipped with version 6.15.1.26025 of the Java Analyzer, while the latest SonarQube 10.6 is shipped with a much more recent version 8.0.1.36337 of the Java Analyzer. These versions can not be changed.

2 、 推测每个版本应该都会有这个描述,于是我们找到里面描述 9.9LTS 的

1
2
3
4
5
6
7
# https://github.com/SonarSource/sonar-java/blob/8.5.0.37199/docs/CUSTOM_RULES_101.md
Looking inside the pom, you will see that both SonarQube and the Java Analyzer versions are hard-coded. This is because SonarSource's analyzers are directly embedded in the various SonarQube versions and are shipped together. For instance, SonarQube 8.9 (previous LTS) is shipped with version 6.15.1.26025 of the Java Analyzer, while SonarQube 9.9 (LTS) is shipped with a much more recent version 7.16.0.30901 of the Java Analyzer. These versions can not be changed.
<properties>
<sonar.plugin.api.version>9.14.0.375</sonar.plugin.api.version>
<sonarjava.version>7.16.0.30901</sonarjava.version>
<!-- [...] -->
</properties>

3、下一步时找到 9.9 对应的代码版本;

确定sonar-java 版本

1、通过当前 sonarqube 版本(9.9.1)确定 sonar-java 的版本

1
2
3
4
5
6
7
# 通过 sonarqube 源码查看;
# 源码默认是 master 分支、需要先切换到 9.9.1xxx 的 tag 或者分支;
# https://github.com/SonarSource/sonarqube/blob/9.9.1.69595/build.gradle
...
dependency 'org.sonarsource.jacoco:sonar-jacoco-plugin:1.3.0.1538'
dependency 'org.sonarsource.java:sonar-java-plugin:7.16.0.30901'
...

2、查看 sonar-java对应版本(7.16.0.30901)的文档

1
2
3
4
5
6
7
# https://github.com/SonarSource/sonar-java/blob/7.16.0.30901/docs/CUSTOM_RULES_101.md
# 此时你会发现 本文档是针对 8.9 LTS 版本的,并不是 9.9 的(帮助文档滞后于软件版本)
# 虽然帮助文档没有更新,但是 代码编译一定时没有问题的否则不会发布的。(基准测试程序一部分)
# 我们就选择这个代码就行
git clone https://github.com/SonarSource/sonar-java.git
cd sonar-java
git checkout -b 7.16.0.30901 tags/7.16.0.30901

3、修改 java-plugin 以来信息

用 pom_SQ_8_9_LTS.xml 覆盖了 pom.xml 。

plugin 必须与 sonarqube 携带的 sonar-java 版本保持一致,否则会提示: Plugin Java Custom Rules [javacustom] is ignored because the version 7.28.0.33738 of required plugin [java] is not installed

1
2
3
4
5
6
7
# 将pom.xml 中的 api 依赖信息 修改成 9.9 对应的

<properties>
<sonar.plugin.api.version>9.14.0.375</sonar.plugin.api.version>
<sonarjava.version>7.16.0.30901</sonarjava.version>
<!-- [...] -->
</properties>

4、确定 java 版本

在 demo 工程中并没提示 java 版本信息,向父级工程查看时发现需要 JDK17。

1
2
3
4
# https://github.com/SonarSource/sonar-java/blob/7.16.0.30901/README.md

Java versions
You need Java 17 to build the project and run the Integration Tests (ITs).

编译运行

1
mvn clean package 

直接编译打包,一切正常但多了一个 pom 文件,导致项目依赖出问题。细细分析才知道时用了 maven-shade插件
阅读 插件配置信息发现:直接通过关掉 并不理想,但是 可以通过 将新生成的 pom 文件放到 target 目录下。
修改 pom.xml 中maven-shade-plugin 的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<dependencyReducedPomLocation>
${project.build.directory}/dependency-reduced-pom.xml
</dependencyReducedPomLocation>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

安装插件

通过 docker 运行的 sonarqube 需要将 对应目录 挂在出来,直接将 plugin.jar 文件放到对应目录。重启 服务即可;

问题总结

JDK 版本不对

可以编译成功,但放到服务器上后会提示 找不到 类加载器;

sonarjava.version 不对时

可以编译成功,放到服务器上 不加载。

sonar-java 用最新代码时

编译成功,放到服务器上不加载;将 sonarjava.version 修改为 正确的时,编译失败。各种 api 不兼容;

dependency-reduced-pom.xml 在根目录下

每次 package 后,项目依赖立刻出问题,无法编译通过。只有删了 dependency-reduced-pom.xml文件 刷新 maven 才可以解决;
将 dependency-reduced-pom.xml 放到 target 目录下后 完美解决;