CentOS 7 安装SonarQube和使用

Posted by Dayong Chan on 2019-10-13
Words 1.4k and Reading Time 8 Minutes
Viewed Times

一、安装

SonarQube一般有2种安装方式、一种是通过Zip包来安装,另外一种是通过Docker镜像进行安装,我们这里介绍的是第二种。

直接在已经安装好Docker的服务器上执行下面的命令

script
1
docker run -d --name sonarqube -p 9000:9000 sonarqube:7.8-community

等命令执行完了再执行下面的命令看下是否正常启动了
1
2
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                    NAMES
738df5e39afa sonarqube:7.8-community "bin/run.sh bin/sona…" 38 minutes ago Up 38 minutes 0.0.0.0:9000->9000/tcp sonarqube

接下来访问系统的页面 http://IP:9000 来进行使用

默认使用admin账号登录,密码也是admin

二、使用

添加配置

首先要在项目的pom.xml上添加Sonar的插件

1
2
3
4
5
6
7
8
9
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.6.1.1688</version>
</plugin>
</plugins>
</build>

然后再到全局的.m2目录下面的settings.xml文件上添加Sonar的配置

先找到pluginGroups,在里面添加上下面的配置

1
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>

然后再找到profiles,在里面添加上下面的配置
1
2
3
4
5
6
7
8
9
10
11
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://sonarqube.myhost.com
</sonar.host.url>
</properties>
</profile>

运行项目

可以通过执行

script
1
mvn clean verify sonar:sonar

或者在IDE里执行运行
mvn-execute-sonar.png

运行结果

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
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.dayong.demo:spring-boot-quartz >-----------------
[INFO] Building spring-boot-quartz 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- sonar-maven-plugin:3.6.1.1688:sonar (default-cli) @ spring-boot-quartz ---
[INFO] User cache: C:\Users\Kitty\.sonar\cache
[INFO] SonarQube version: 7.8
[INFO] Default locale: "zh_CN", source code encoding: "UTF-8"
[INFO] Load global settings
[INFO] Load global settings (done) | time=173ms
[INFO] Server id: BF41A1F2-AXRN33a0ydR7yjV_BOcS
[INFO] User cache: C:\Users\Kitty\.sonar\cache
[INFO] Load/download plugins
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=89ms
[INFO] Load/download plugins (done) | time=147ms
[INFO] Process project properties
[INFO] Process project properties (done) | time=21ms
[INFO] Execute project builders
[INFO] Execute project builders (done) | time=9ms
[INFO] Project key: com.dayong.demo:spring-boot-quartz
[INFO] Base dir: D:\IdeaProjects\Java\learning-demo\spring-boot\spring-boot-quartz
[INFO] Working dir: D:\IdeaProjects\Java\learning-demo\spring-boot\spring-boot-quartz\target\sonar
[INFO] Load project settings for component key: 'com.dayong.demo:spring-boot-quartz'
[INFO] Load project settings for component key: 'com.dayong.demo:spring-boot-quartz' (done) | time=354ms
[INFO] Load quality profiles
[INFO] Load quality profiles (done) | time=98ms
[INFO] Load active rules
[INFO] Load active rules (done) | time=2553ms
[INFO] Indexing files...
[INFO] Project configuration:
[INFO] 24 files indexed
[INFO] 0 files ignored because of scm ignore settings
[INFO] Quality profile for java: Sonar way
[INFO] Quality profile for xml: Sonar way
[INFO] ------------- Run sensors on module spring-boot-quartz
[INFO] Load metrics repository
[INFO] Load metrics repository (done) | time=77ms
[INFO] Sensor JavaSquidSensor [java]
[INFO] Configured Java source version (sonar.java.source): 8
[INFO] JavaClasspath initialization
[INFO] JavaClasspath initialization (done) | time=22ms
[INFO] JavaTestClasspath initialization
[INFO] JavaTestClasspath initialization (done) | time=9ms
[INFO] Java Main Files AST scan
[INFO] 22 source files to be analyzed
[INFO] Load project repositories
[INFO] Load project repositories (done) | time=333ms
[INFO] 22/22 source files have been analyzed
[INFO] Java Main Files AST scan (done) | time=6154ms
[INFO] Java Test Files AST scan
[INFO] 1 source files to be analyzed
[INFO] Java Test Files AST scan (done) | time=103ms
[INFO] Java Generated Files AST scan
[INFO] 0 source files to be analyzed
[INFO] 1/1 source files have been analyzed
[INFO] 0/0 source files have been analyzed
[INFO] Java Generated Files AST scan (done) | time=1ms
[INFO] Sensor JavaSquidSensor [java] (done) | time=6629ms
[INFO] Sensor SonarCSS Rules [cssfamily]
[INFO] No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
[INFO] Sensor SonarCSS Rules [cssfamily] (done) | time=4ms
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
[INFO] No report imported, no coverage information will be imported by JaCoCo XML Report Importer
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=15ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [D:\IdeaProjects\Java\learning-demo\spring-boot\spring-boot-quartz\target\surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=5ms
[INFO] Sensor JavaXmlSensor [java]
[INFO] 1 source files to be analyzed
[INFO] Sensor JavaXmlSensor [java] (done) | time=219ms
[INFO] Sensor HTML [web]
[INFO] 1/1 source files have been analyzed
[INFO] Sensor HTML [web] (done) | time=8ms
[INFO] Sensor XML Sensor [xml]
[INFO] 1 source files to be analyzed
[INFO] 1/1 source files have been analyzed
[INFO] Sensor XML Sensor [xml] (done) | time=210ms
[INFO] ------------- Run sensors on project
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=29ms
[INFO] Sensor Java CPD Block Indexer
[INFO] Sensor Java CPD Block Indexer (done) | time=78ms
[INFO] SCM Publisher SCM provider for this project is: git
[INFO] SCM Publisher 1 source file to be analyzed
[INFO] SCM Publisher 0/1 source files have been analyzed (done) | time=139ms
[WARNING] Missing blame information for the following files:
[WARNING] * pom.xml
[WARNING] This may lead to missing/broken features in SonarQube
[INFO] CPD Executor 8 files had no CPD blocks
[INFO] CPD Executor Calculating CPD for 14 files
[INFO] CPD Executor CPD calculation finished (done) | time=39ms
[INFO] Analysis report generated in 127ms, dir size=181 KB
[INFO] Analysis report compressed in 408ms, zip size=73 KB
[INFO] Analysis report uploaded in 528ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://sonarqube.myhost.com/dashboard?id=com.dayong.demo%3Aspring-boot-quartz
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://sonarqube.myhost.com/api/ce/task?id=AXRN-sZJydR7yjV_BTF-
[INFO] Analysis total time: 14.993 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.917 s
[INFO] Finished at: 2019-09-13T16:41:53+08:00
[INFO] ------------------------------------------------------------------------

访问 http://sonarqube.myhost.com/dashboard?id=com.dayong.demo%3Aspring-boot-quartz 就可以看到报告了

execute-result.png

访问 http://sonarqube.myhost.com/api/ce/task?id=AXRN-sZJydR7yjV_BTF- 可以查看任务执行的情况

1
{"task":{"id":"AXRN-bLlydR7yjV_BTF9","type":"REPORT","componentId":"AXRN-bIeydR7yjV_BTFy","componentKey":"com.dayong.demo:spring-boot-quartz","componentName":"spring-boot-quartz","componentQualifier":"TRK","analysisId":"AXRN-budmwdqF0EzNBju","status":"SUCCESS","submittedAt":"2020-09-02T08:40:44+0000","startedAt":"2020-09-02T08:40:45+0000","executedAt":"2020-09-02T08:40:49+0000","executionTimeMs":4699,"logs":false,"hasScannerContext":true,"organization":"default-organization","warningCount":1,"warnings":[]}}

更多内容请查考
https://docs.sonarqube.org/latest/setup/overview/
https://docs.sonarqube.org/latest/setup/install-server/


本站版权使用署名-非商业性使用-禁止演绎 4.0 国际,转载请保留原文链接及作者。