Sonar
环境
192.168.48.53 jenkins maven sonar
192.168.48.56 web mysql
准备
sonarqube-6.7.7.zip
链接: https://pan.baidu.com/s/1CpQ1X40v-VDMJ9e6wf1KJw 提取码: jsim
sonar-scanner-cli-3.3.0.1492-linux.zip
链接: https://pan.baidu.com/s/1zKa4H_wM0TThbEza90sbew 提取码: 6v4c
安装mysql
wget -c https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
yum -y install yum-utils
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
yum install mysql-community-server -y
# 启动mysql
systemctl start mysqld
# 开机启动
systemctl enable mysqld
# 查看root临时密码
grep 'temporary password' /var/log/mysqld.log
# 使用mysql临时登录,修改root密码
[root@web ~]# grep 'temporary password' /var/log/mysqld.log
2019-06-29T14:18:26.766190Z 1 [Note] A temporary password is generated for root@localhost: kLCg-okJR76g
[root@web ~]# mysql -uroot -p'kLCg-okJR76g'
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'p@ssW0rd';
创建sonar库,用户
[root@web ~]# mysql -uroot -p'p@ssW0rd'
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE USER 'sonar' IDENTIFIED BY 'p@ssW0rd';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'p@ssW0rd';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'p@ssW0rd';
Query OK, 0 rows affected, 1 warning (0.00 sec)
安装sonar
解压文件包
[root@jenkins ~]# cd /home/
[root@jenkins home]# unzip sonarqube-6.7.7.zip
创建软链接
[root@jenkins home]# ln -s /home/sonarqube-6.7.7 /home/sonarqube
添加环境变量
[root@jenkins home]# vim /etc/profile
export SONAR_HOME=/home/sonarqube-6.7.7
export PATH=$PATH:/home/sonarqube-6.7.7/bin/linux-x86-64
[root@jenkins home]# source /etc/profile
配置sonar.properties
[root@jenkins home]# cd /home/sonarqube/conf/
[root@jenkins conf]# ll -h
total 24K
-rw-r--r-- 1 root root 18K Apr 16 15:39 sonar.properties
-rw-r--r-- 1 root root 3.3K Apr 16 15:39 wrapper.conf
[root@jenkins conf]# vim sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=p@ssW0rd
sonar.jdbc.url=jdbc:mysql://192.168.48.56:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.web.host=0.0.0.0
sonar.web.port=9000
创建sonar用户
[root@jenkins conf]# adduser sonar
[root@jenkins conf]# id sonar
uid=1000(sonar) gid=1000(sonar) groups=1000(sonar)
[root@jenkins conf]# echo "sonar" | passwd --stdin sonar
Changing password for user sonar.
passwd: all authentication tokens updated successfully.
# 给sonarqube设置用户和组
[root@jenkins ~]# chown -R sonar:sonar /home/sonarqube-6.7.7
启动sonar
[root@jenkins ~]# su - sonar
[sonar@jenkins ~]$ cd /home/sonarqube-6.7.7/bin/linux-x86-64
[sonar@jenkins linux-x86-64]$ ./sonar.sh start
Starting SonarQube...
Started SonarQube.
访问
登录
默认登陆名 admin ,密码 admin
汉化
安装插件,然后重启
安装SonarQube Scanner
解压文件包
[root@jenkins ~]# cd /home/
[root@jenkins home]# unzip sonar-scanner-cli-3.3.0.1492-linux.zip
添加环境变量
[root@jenkins ~]# vim /etc/profile
export SONAR_SCANNER_HOME=/home/sonar-scanner-3.3.0.1492-linux
export PATH=$PATH:${SONAR_SCANNER_HOME}/bin
[root@jenkins ~]# source /etc/profile
配置sonar-scanner
[root@jenkins ~]# cd /home/sonar-scanner-3.3.0.1492-linux/conf/
[root@jenkins conf]# ll -h
total 4.0K
-rw-r--r-- 1 root root 310 Jan 8 12:17 sonar-scanner.properties
[root@jenkins conf]# vim sonar-scanner.properties
sonar.host.url=http://192.168.48.53:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=p@ssW0rd
sonar.jdbc.url=jdbc:mysql://192.168.48.56:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.login=admin
sonar.password=admin
测试
[root@jenkins ~]# sonar-scanner -h
INFO:
INFO: usage: sonar-scanner [options]
INFO:
INFO: Options:
INFO: -D,--define <arg> Define property
INFO: -h,--help Display help information
INFO: -v,--version Display version information
INFO: -X,--debug Produce execution debug output
准备java项目
链接: https://pan.baidu.com/s/1EDc8alSYHRc5YwErMTwbxA 提取码: u5ug
[root@jenkins ~]# ll
total 4
-rw-r--r-- 1 root root 2307 Jun 29 14:53 hello-world-war-master.zip
[root@jenkins ~]# unzip hello-world-war-master.zip
[root@jenkins ~]# cd hello-world-war-master/
[root@jenkins hello-world-war-master]# ll -h
total 4.0K
-rw-r--r-- 1 root root 962 Jun 29 14:53 pom.xml
drwxr-xr-x 3 root root 18 May 23 2014 src
maven配置集成sonar
[root@jenkins ~]# vim /home/apache-maven-3.6.1/conf/settings.xml
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:mysql://192.168.48.56:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>p@ssw0rd</sonar.jdbc.password>
<sonar.host.url>http://192.168.48.53:9000</sonar.host.url>
</properties>
</profile
</profiles>
</settings>
在maven项目的pom.xml文件中,添加以下信息
[root@jenkins hello-world-war-master]# vim /root/hello-world-war-master/pom.xml
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.6.0.1398</version>
</plugin>
执行maven命令
[root@jenkins hello-world-war-master]# mvn clean package sonar:sonar
编写sonar-scan配置文件
[root@jenkins ~]# cd hello-world-war-master/
[root@jenkins hello-world-war-master]# vim sonar-project.properties
sonar.projectKey=hell0-world
sonar.projectName=test
sonar.projectVersion=1.0
sonar.sources=src
sonar.language=java
sonar.sourceEncoding=UTF-8
[root@jenkins hello-world-war-master]# ll -h
total 8.0K
-rw-r--r-- 1 root root 962 Jun 29 14:53 pom.xml
-rw-r--r-- 1 root root 142 Jun 29 23:57 sonar-project.properties
drwxr-xr-x 3 root root 18 May 23 2014 src
执行sonar-scanner命令
[root@jenkins hello-world-war-master]# sonar-scanner -X