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<<EOF
[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
auto-rehash
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
EOF
chown -R mysql.mysql /usr/local/mysql/
chown mysql.mysql /etc/my.cnf
echo "PATH=$PATH:/usr/local/mysql/bin" >> /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 "\033[32mmysqld服务运行正常\033[0m"
else
echo -e "\033[31mmysqld服务运行异常,请检查\033[0m"
fi
sleep 2
echo ' '
echo -e "\033[32mMySQL 没有设置密码,执行 mysql 命令登录\033[0m"
mysql
set password = password('abc123');
use mysql;
下载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.4/* /opt/sonar
useradd sonar #创建sonar用户,必须sonar用于启动,否则报错
chown -R sonar. /opt/sonar #更改sonar目录及文件权限
修改sonar配置文件
vim /opt/sonar/conf/sonar.properties
内容如下:
16 sonar.jdbc.username=root
17 sonar.jdbc.password=abc123
26 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useS SL=false
(26行取消注释即可)
注意:sonar默认监听9000端口,如果9000端口被占用,需要更改。
启动sonar(注意:切换sonar用户)
cd /opt/sonar/
su sonar ./bin/linux-x86-64/sonar.sh start #启动
su sonar ./bin/linux-x86-64/sonar.sh status #查看状态
su sonar ./bin/linux-x86-64/sonar.sh stop #停止
tail -f /opt/sonar/logs/sonar.log #查看日志
http://192.168.8.19:9000
默认账户:admin/admin
0ab295d001bc785e1e58e9f886a542820107793e
安装SonarQube Scanner插件
安装SonarQube
添加SonarQube凭证
Jenkins进行SonarQube配置
以自由风格为例:打开web_demo_freestyle
# must be unique in a given SonarQube instance
sonar.projectKey=web_demo_freestyle
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=web_demo_freestyle
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服务器上刷新,查看结果
测试错误代码,新建 Java 和 resource 目录
配置pom.xml文件添加对servlet的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
新建编写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");
}
}
代码提交
进行构建测试结果
如果发生下面的报错,解决方法如下
代码检查后,发现问题
项目根目录下,创建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
邮件通知也会收到