SonarQube是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前
支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测。
官网:https://www.sonarqube.org/
SonarQube 7.9以上版本已不再支持mysql
使用阿里云服务(47.96.14.220),使用docker创建mysql
docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
# 获取到停止的容器id
docker ps -a
#根据容器id 开启容器
docker start 容器id
创建数据库:sonar
mysql> create database sonar;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonar |
| sys |
+--------------------+
5 rows in set (0.00 sec)
[root@zwb software]# yum install unzip
[root@zwb software]# unzip sonarqube-6.7.4.zip
[root@zwb software]# mkdir /opt/sonar
[root@zwb software]# mv sonarqube-6.7.4/* /opt/sonar/
[root@zwb software]# useradd sonar #创建用户,必须sonar用于启动,否则报错
[root@zwb software]# chown -R sonar. /opt/sonar/
[root@zwb software]# cd /opt/sonar/
[root@zwb sonar]# ll
total 44
drwxr-xr-x 8 sonar sonar 4096 May 30 2018 bin
drwxr-xr-x 2 sonar sonar 4096 May 30 2018 conf
-rw-r--r-- 1 sonar sonar 7651 May 30 2018 COPYING
drwxr-xr-x 2 sonar sonar 4096 May 30 2018 data
drwxr-xr-x 7 sonar sonar 4096 May 30 2018 elasticsearch
drwxr-xr-x 4 sonar sonar 4096 May 30 2018 extensions
drwxr-xr-x 9 sonar sonar 4096 May 30 2018 lib
drwxr-xr-x 2 sonar sonar 4096 May 30 2018 logs
drwxr-xr-x 2 sonar sonar 4096 May 30 2018 temp
drwxr-xr-x 9 sonar sonar 4096 May 30 2018 web
修改sonar配置文件
[root@zwb sonar]# vim /opt/sonar/conf/sonar.properties
新增数据库配置:
sonar.jdbc.username=root
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
启动:
[root@zwb linux-x86-64]# pwd
/opt/sonar/bin/linux-x86-64
[root@zwb linux-x86-64]# su sonar ./sonar.sh
Usage: ./sonar.sh { console | start | stop | restart | status | dump }
[root@zwb linux-x86-64]# su sonar ./sonar.sh start
Starting SonarQube...
Started SonarQube.
[root@zwb linux-x86-64]# su sonar /opt/sonar/bin/linux-x86-64/sonar.sh start
报错1:
[root@zwb linux-x86-64]# ps -ef|grep sonar
root 24889 8787 0 16:34 pts/2 00:00:00 grep --color=auto sonar
[root@zwb linux-x86-64]# tail -f ../../logs/sonar.log
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
2021.02.23 16:34:30 INFO app[][o.e.p.PluginsService] no modules loaded
2021.02.23 16:34:30 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2021.02.23 16:34:44 INFO app[][o.s.a.SchedulerImpl] Process[es] is up
2021.02.23 16:34:44 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/opt/sonar]: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-0.el7_9.x86_64/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonar/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:./lib/server/*:/opt/sonar/lib/jdbc/mysql/mysql-connector-java-5.1.42.jar org.sonar.server.app.WebServer /opt/sonar/temp/sq-process5928721999126032601properties
2021.02.23 16:34:52 WARN app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 137
2021.02.23 16:34:52 INFO app[][o.s.a.SchedulerImpl] Process [es] is stopped
2021.02.23 16:34:54 INFO app[][o.s.a.SchedulerImpl] Process [web] is stopped
2021.02.23 16:34:54 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped
<-- Wrapper Stopped
查看es日志:
[root@zwb logs]# pwd
/opt/sonar/logs
[root@zwb logs]# ll
total 16
-rw-r--r-- 1 sonar sonar 0 Feb 23 16:19 access.log
-rw-r--r-- 1 sonar sonar 5632 Feb 23 16:34 es.log
-rw-r--r-- 1 sonar sonar 4086 Feb 23 16:34 sonar.log
-rw-r--r-- 1 sonar sonar 2752 Feb 23 16:34 web.log
[root@zwb logs]# cat es.log
...
2021.02.23 16:34:41 INFO es[][o.e.t.TransportService] publish_address {127.0.0.1:9001}, bound_addresses {127.0.0.1:9001}
2021.02.23 16:34:41 WARN es[][o.e.b.BootstrapChecks] max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
2021.02.23 16:34:41 WARN es[][o.e.b.BootstrapChecks] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决:增加vm.max_map_count
[root@zwb logs]# vim /etc/sysctl.conf
[root@zwb logs]# sysctl -p
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
kernel.sysrq = 1
vm.max_map_count = 262144
在/etc/sysctl.conf中添加vm.max_map_count=262144后执行sysctl -p使配置生效
报错2:
2021.02.23 16:44:22 ERROR es[][o.e.b.Bootstrap] Exception
java.lang.RuntimeException: can not run elasticsearch as root
不能以root运行,以sonar账号运行
[root@zwb bin]# su sonar ./linux-x86-64/sonar.sh start
报错3:文件无访问权限
2021.02.23 16:46:11 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonar/temp
WrapperSimpleApp: Encountered an error running main: java.nio.file.AccessDeniedException: /opt/sonar/temp/conf/es/log4j2.properties
java.nio.file.AccessDeniedException: /opt/sonar/temp/conf/es/log4j2.properties
重新授权:
[root@zwb conf]# ll
total 4
drwxr-xr-x 2 root root 4096 Feb 23 16:44 es
[root@zwb conf]# chown -R sonar. /opt/sonar/
[root@zwb conf]# ll
total 4
drwxr-xr-x 2 sonar sonar 4096 Feb 23 16:44 es
报错4:
[root@zwb logs]# cat es.log
2021.02.23 16:51:16 INFO es[][o.e.t.TransportService] publish_address {127.0.0.1:9001}, bound_addresses {127.0.0.1:9001}
2021.02.23 16:51:16 WARN es[][o.e.b.BootstrapChecks] max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
解决:
[root@zwb logs]# vim /etc/security/limits.conf
增加sonar用户:
sonar hard nofile 65536
sonar soft nofile 65536
切换用户后生效:
[root@dw logs]# su sonar
[sonar@dw logs]$ ulimit -Hn
65536
报错5:
[root@zwb logs]# cat web.log
2021.02.23 17:16:10 INFO web[][o.s.s.e.EsClientProvider] Connected to local Elasticsearch: [127.0.0.1:9001]
2021.02.23 17:16:10 INFO web[][o.s.s.p.LogServerVersion] SonarQube Server / 6.7.4.38452 / b62b59da1adf991c41ed0730684f813d4116f6cc
2021.02.23 17:16:10 INFO web[][o.sonar.db.Database] Create JDBC data source for jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
021.02.23 17:16:11 ERROR web[][o.s.s.p.Platform] Web server startup failed: Unsupported mysql version: 5.5. Minimal supported version is 5.6.
mysql版本过低,需要5.6以上。
报错6:
[root@zwb logs]# cat sonar.log
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000eaaa0000, 89522176, 0) failed; error='Not enough space' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 89522176 bytes for committing reserved memory.
# An error report file with more information is saved as:
解决:修改es的内存
把默认的2g内存改为256m
[root@zwb config]# pwd
/opt/sonar/elasticsearch/config
[root@zwb config]# vim jvm.options
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms256m
-Xmx256m
报错:
[root@zwb logs]# cat sonar.log
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000e0000000, 44695552, 0) failed; error='Not enough space' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 44695552 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /opt/sonar/hs_err_pid16736.log
内存不足,无法启动sonar;最少4g内存以上
安装docker和docker-compose
[root@zengqingfa sonar-compose]# docker version
Client: Docker Engine - Community
Version: 20.10.4
API version: 1.41
Go version: go1.13.15
Git commit: d3cb89e
Built: Thu Feb 25 07:06:20 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.4
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Thu Feb 25 07:04:45 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.3
GitCommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version: 1.0.0-rc92
GitCommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@zengqingfa sonar-compose]# docker-compose version
docker-compose version 1.21.2, build a133471
docker-py version: 3.3.0
CPython version: 3.6.5
OpenSSL version: OpenSSL 1.0.1t 3 May 2016
[root@zengqingfa bin]# cd /home
[root@zengqingfa home]# ll
total 0
[root@zengqingfa home]# mkdir sonar-compose
[root@zengqingfa home]# cd sonar-compose/
[root@zengqingfa sonar-compose]# ll
total 0
创建docker-compose.yml
version: "3"
services:
sonarqube:
image: sonarqube:7.4-community
ports:
- "9000:9000"
networks:
- sonarnet
environment:
- SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
volumes:
- sonarqube_conf:/opt/sonarqube/conf
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins
db:
image: postgres:11.1
networks:
- sonarnet
environment:
- POSTGRES_USER=sonar
- POSTGRES_PASSWORD=sonar
volumes:
- postgresql_data:/var/lib/postgresql/data
networks:
sonarnet:
driver: bridge
volumes:
sonarqube_conf:
sonarqube_data:
sonarqube_extensions:
sonarqube_bundled-plugins:
postgresql_data:
启动运行:
[root@zengqingfa sonar-compose]# vim docker-compose.yml
[root@zengqingfa sonar-compose]# docker-compose up -d
Creating network "sonar-compose_sonarnet" with driver "bridge"
Creating volume "sonar-compose_sonarqube_conf" with default driver
Creating volume "sonar-compose_sonarqube_data" with default driver
Creating volume "sonar-compose_sonarqube_extensions" with default driver
Creating volume "sonar-compose_sonarqube_bundled-plugins" with default driver
Creating volume "sonar-compose_postgresql_data" with default driver
Pulling sonarqube (sonarqube:7.4-community)...
7.4-community: Pulling from library/sonarqube
cd8eada9c7bb: Pull complete
c2677faec825: Pull complete
fcce419a96b1: Pull complete
045b51e26e75: Pull complete
88e50f3a5916: Pull complete
a7c546cdc7ce: Pull complete
73b0460146c0: Pull complete
428095c4a908: Pull complete
098f954c845f: Pull complete
65b343e54a36: Pull complete
234d81172884: Pull complete
ec0b6ad513f7: Pull complete
Digest: sha256:7d7d88000121d368cc31d97bf2e98636cd743afb779878df41d075c16471c4e4
Status: Downloaded newer image for sonarqube:7.4-community
Pulling db (postgres:11.1)...
11.1: Pulling from library/postgres
6ae821421a7d: Pull complete
060245e5c056: Pull complete
d6c2e60cfc1c: Pull complete
38d23c24a9df: Pull complete
9998068fb35e: Pull complete
2bb2385a4bb9: Pull complete
2ad14c51497d: Pull complete
77519615ed40: Pull complete
5fe5eb7fad2f: Pull complete
9f6cb81d6d43: Pull complete
72210502cae4: Pull complete
1e967d04951b: Pull complete
f1b5a9dd4ff0: Pull complete
67d715e4939e: Pull complete
Digest: sha256:f310592cf3964f038dbaefac2dc2088982e5ab06312a590bcacc97749ee5db69
Status: Downloaded newer image for postgres:11.1
Creating sonar-compose_db_1 ... done
Creating sonar-compose_sonarqube_1 ... done
[root@zengqingfa sonar-compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a533d8d6ca9e sonarqube:7.4-community "./bin/run.sh" 11 seconds ago Up 5 seconds 0.0.0.0:9000->9000/tcp sonar-compose_sonarqube_1
fe0e36ea0cd7 postgres:11.1 "docker-entrypoint.s…" 11 seconds ago Up 5 seconds 5432/tcp sonar-compose_db_1

SonarQube Scanner

Manage Jenkins->Configure System->SonarQube servers

配置:
# must be unique in a given SonarQube instance
sonar.projectKey=freestyle-demo01
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=freestyle-demo01
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. .表示当前目录下所有的文件
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
构建。
查看sonarqube扫描结果:
1、在项目根目录下新建文件:sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=test01-pipeline02
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=test01-pipeline02
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
2、修改Jenkinsfile,加入SonarQube代码审查阶段
pipeline {
agent any
stages{
stage('拉取代码') { // for display purposes
steps{
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '6d5b718d-ff66-4c53-ba96-dec11e2f67cd', url: 'https://gitlab.com/tools-study/hello-world-web-jenkins.git']]])
}
}
stage('编译构建') {
steps{
sh label: '', script: 'mvn clean package'
}
}
stage('SonarQube代码审查') {
steps{
script {
scannerHome = tool 'sonarqube-scanner'
}
withSonarQubeEnv('sonarqube7.4') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('项目部署') {
steps{
deploy adapters: [tomcat8(credentialsId: '559012b0-74e9-4a6b-ab28-b6ea6cfb3942', path: '', url: 'http://121.196.169.191:8080/')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext(
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}',
to: '810305803@qq.com,zengqingfa_java@163.com'
)
}
}
}
3)项目构建
查看sonar
(‘拉取代码’) { // for display purposes
steps{
checkout([
c
l
a
s
s
:
′
G
i
t
S
C
M
′
,
b
r
a
n
c
h
e
s
:
[
[
n
a
m
e
:
′
∗
/
class: 'GitSCM', branches: [[name: '*/
class:′GitSCM′,branches:[[name:′∗/{branch}‘]], extensions: [], userRemoteConfigs: [[credentialsId: ‘6d5b718d-ff66-4c53-ba96-dec11e2f67cd’, url: ‘https://gitlab.com/tools-study/hello-world-web-jenkins.git’]]])
}
}
stage(‘编译构建’) {
steps{
sh label: ‘’, script: ‘mvn clean package’
}
}
stage(‘SonarQube代码审查’) {
steps{
script {
scannerHome = tool ‘sonarqube-scanner’
}
withSonarQubeEnv(‘sonarqube7.4’) {
sh "KaTeX parse error: Expected 'EOF', got '}' at position 45: …er" }̲ } …{PROJECT_NAME} - Build # ${BUILD_NUMBER} -
B
U
I
L
D
S
T
A
T
U
S
!
′
,
b
o
d
y
:
′
{BUILD_STATUS}!', body: '
BUILDSTATUS!′,body:′{FILE,path=“email.html”}’,
to: ‘810305803@qq.com,zengqingfa_java@163.com’
)
}
}
}
3)项目构建
查看sonar
[外链图片转存中...(img-UNj7qHG9-1666275631291)]