前言

通过SonarQube进行代码质量检测

参考教程:

搭建过程

本次搭建通过Rainbond进行搭建,其他构建方式通过docker或者docker-compose方式进行构建

dashboard: http://192.168.5.1:20001/
用户名:admin
密码:123456

添加插件

生成Token

设置 Token 令牌,进入设置界面,如系统所示:

img

首先,在 SonarQube 中生成一个 Token(PS:用 token 代替输入用户名和密码),点击“安 全”菜单,直接输入令牌名称为:SonarQubeToken,点击“生成”按钮生产
token。操作 如下图所示:

注意:生成 token 复制保存下来,因为只显示一次,刷新页面就不再显示了。如下图所示:

添加中文插件

点击 Administration 菜单,然后再点击 Marketplace 菜单,找到 Plugins 栏目的搜 索框输入 Chinese
Pack,最后点击“Install”按钮进行安装,如下图所示

创建

添加完成后进行重启操作

添加PDF报告插件

加不加都行

配置maven配置

SonarScanner 的一般性配置,包括 SonarQube 服务地址,以及 AuthenticationToken 都可以配置进 Settings.xml 全局配置,供 Java
Maven 项目构建时使用。

Rainbond 在针对 Java Maven 类型的项目进行构建时,提供入口配置全局生效的 Settings.xml 。在高级设置——部署属性中,可以点击 *
管理Maven配置* 来编辑默认的 Settings.xml。此处我们已经提供了一份默认的配置,我们需要在 xml 格式下添加以下配置来定义
SonarQube 服务地址,以及 AuthenticationToken

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://9000.grba63fe.d3uaqt1123z0k.17f4cc.grapps.cn
</sonar.host.url>
<sonar.login>
c1041c2b4ac2e89d1fe3f5fa5bb5971bc8dc85b7
</sonar.login>
</properties>
</profile>
</profiles>
</settings>

当然,用户也可以新建一份专用的 Settings.xml 配置,在我的环境中,我将这份配置命名为 sonar-scanner。全局配置只需要定义一次就可以了。

Setting文件实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">

<!-- 本地仓库的位置 -->
<localRepository>D:\develop\.m2\repository</localRepository>
<!-- Apache Maven 配置 -->
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>

<proxies/>

<!-- 私服发布的用户名密码 -->
<servers>
<server>
<id>maven-releases</id>
<username>edwin</username>
<password>123456</password>
</server>
<server>
<id>maven-snapshots</id>
<username>edwin</username>
<password>123456</password>
</server>
</servers>

<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>

<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>maven-public</id>
<name>vyiot Team Repository</name>
<url>https://registry.edwin.com/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
<repository>
<id>maven-snapshots</id>
<url>https://registry.edwin.com/nexus/repository/maven-snapshots/</url>
</repository>
<repository>
<id>maven-releases</id>
<url>https://registry.edwin.com/nexus/repository/maven-releases/</url>
</repository>
<repository>
<id>jenkins</id>
<url>http://repo.jenkins-ci.org/releases/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>maven-public</id>
<url>https://registry.edwin.com/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>protoc-plugin</id>
<url>https://dl.bintray.com/sergei-ivanov/maven/</url>
</pluginRepository>
</pluginRepositories>
</profile>

<profile>
<id>ali</id>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://192.168.5.1:20001
</sonar.host.url>
<sonar.login>
ea60b26f0a2ac944f2660aec10d2d0bb1de16d0be7de
</sonar.login>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>ali</activeProfile>
</activeProfiles>
</settings>

执行Maven命令

1
mvn clean verify sonar:sonar  install

对于每一个不同的项目,需要自定义 -Dsonar.projectName -Dsonar.projectKey 的值。前者定义了在 SonarQube
服务中,这个项目的名字,后者则定义了项目的唯一 ID.

添加阿里巴巴P3C规范插件

  1. 下载sonar-p3c-pmd

sonarqube的版本号与sonar-p3c-pmd是对应的,所以需要选择好版本,楼主版本为7.6,否则会还会报es连接不上的错误。
地址:https://github.com/Leibnizhu/sonar-pmd-p3c-jdk17/releases/download/1.0.0/sonar-pmd-plugin-1.0.0.jar

仓库地址:https://github.com/Leibnizhu/sonar-pmd-p3c-jdk17

  1. 修改配置

删除之前pmd文件,放于sonarqube-7.6\extensions\plugins中,重启服务。

  1. 创建p3c规则

新增配置类别。

image.png

激活配置规则

image.png

选择资源库中的pmd(之前导入的p3c-pdm) 然后选择包中的规则激活即可,楼主这里只激活了p3c的51条。

image.png

设为默认即可

image-20231008165917897

然后再次扫描即就是使用了新规范扫描了