一、SonaQube 简介二、部署安装
1. 安装MySQL数据库2. 安装SonarQube3. 访问 sonar 服务器 三、实现代码审查
1. 在项目添加SonaQube代码审查(非流水线项目)2. 在项目添加SonaQube代码审查(流水线项目)
一、SonaQube 简介
SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前支持 java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测,底层使用 elasticsearch 作为代码检索工具。
官方网站
二、部署安装环境要求
| 软件 | 服务器 | 版本 |
|---|---|---|
| JDK | 192.168.8.19 | 1.8 |
| MySQL | 192.168.8.19 | 5.7 |
| SonarQube | 192.168.8.19 | 6.7.4 |
#!/bin/bash #mkdir /data #把安装包放在data目录中 tar zxvf /data/mysql-5.7.17.tar.gz -C /opt tar zxvf /data/boost_1_59_0.tar.gz -C /usr/local mv /usr/local/boost_1_59_0 /usr/local/boost yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make git perl expat-devel pcre-devel pcre useradd -s /sbin/nologin mysql cd /opt/mysql-5.7.17/ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_INNObase_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=/usr/local/boost -DWITH_SYSTEMD=1 cd /opt/mysql-5.7.17/ make -j 4 make install echo > /etc/my.cnf cat > /etc/my.cnf<> /etc/profile source /etc/profile cd /usr/local/mysql/bin/ ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ systemctl daemon-reload && systemctl start mysqld && systemctl enable mysqld ln -s /usr/local/mysql/bin/mysql /usr/local/sbin/ pgrep "mysqld" &> /dev/null if [ $? -eq 0 ];then echo -e " 33[32mmysqld服务运行正常 33[0m" else echo -e " 33[31mmysqld服务运行异常,请检查 33[0m" fi sleep 2 echo ' ' echo -e " 33[32mMySQL 没有设置密码,执行 mysql 命令登录 33[0m"
mysql
set password = password('abc123');
use mysql;
2. 安装SonarQube
下载sonar压缩包
在MySQL创建sonar数据库
#授权远程登录 mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option; mysql> create database sonar; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sonar | | sys | +--------------------+ 5 rows in set (0.00 sec)
解压sonar,并设置权限
yum install -y unzip unzip sonarqube-6.7.4.zip mkdir /opt/sonar mv sonarqube-6.7.4target/** 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服务器上刷新,查看结果
测试错误代码,新建 Java 和 resource 目录
配置pom.xml文件添加对servlet的依赖
javax.servlet javax.servlet-api 4.0.1
新建编写Servlet文件
package com.root;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//模拟错误代码
int i = 100/0;
//模拟代码冗余
int j = 100;
j = 200;
resp.getWriter().write("hello Servlet");
}
}
代码提交
进行构建测试结果
如果发生下面的报错,解决方法如下
代码检查后,发现问题
发现代码BUG和未使用变量等问题
项目根目录下,创建sonar-project.properties文件
# must be unique in a given SonarQube instance sonar.projectKey=web_demo_pipeline # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=web_demo_pipeline 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
修改 Jenkinsfile,加入 SonarQube 代码审查阶段
stage('code checking') {
steps {
script {
//引入了sonarqube-scanner工具
scannerHome = tool 'sonar-scanner'
}
//引入了sonarqube服务器系统环境
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
把更改后的sonar-project.properties和Jenkinsfile进行提交
开始构建web_demo_pipeline
查看检测结果
邮件通知也会收到



