文章目录
1. SonarQube 简介
SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前支持 java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy
等二十几种编程语言的代码质量管理与检测,底层使用 elasticsearch 作为代码检索工具。
2. 安装 SonarQube
2.1 环境要求
软件 | 服务器 | 版本 |
---|---|---|
JDK | 192.168.10.30 | 1.8 |
MySQL | 192.168.10.30 | 5.7 |
SonarQube | 192.168.10.30 | 6.7.4 |
2.2 安装 MySQL
**参考:**
数据库简介与 Mysql 服务基础
需要远程连接授权
2.3 在 MySQL 创建 sonar 数据库
mysql -uroot -p123456
#-----------------------------------
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database sonar;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonar |
| sys |
+--------------------+
5 rows in set (0.00 sec)
2.4 安装 SonarQube
下载安装包
解压 sonar,并设置权限
yum install unzip -y
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
# 内容如下
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 # 取消注释即可
注意: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 logs/sonar.logs #查看日志
访问 sonar
http://192.168.10.30:9000
3. Sonarqube 登录配置
3.1 登录 sonar
默认账户
:admin/admin
3.2 创建 token
随便输个字符串
生成加密 token(和 Jenkins 整合会使用此密钥)
注意:此 Token 要记下来。
syhj: 169c0ccf5c97b9833e22b006510c1a9be326c4a8
4. Sonarqube 代码审查配置
4.1 安装 SonarQube Scanner 插件
4.2 安装 SonarQube
4.3 添加 SonarQube 凭证
4.4 Jenkins 进行 SonarQube 配置
Manage Jenkins -> Configure System -> SonarQube servers
应用保存。
5. 在项目添加 SonarQube 代码审查(非流水线项目)
5.1 添加代码审查步骤
以自由风格为例:打开 web_demo_freestyle
自由风格项目参考:https://blog.csdn.net/shenyuanhaojie/article/details/122883240
# 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 服务器上刷新,查看结果
5.2 测试错误代码
新建 Java 和 resource 目录
配置 pom.xml 文件添加对 servlet 的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
需要下一些依赖组件,下载不下来查找原因。
新建编写 Servlet 文件
package com.itzc.hello; //注意自己的包名
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");
}
}
将 pom.xml 和 HelloServlet.java 提交到 Gitlab
进行构建测试结果
查看 sonarqube
5.3 注意
解决方法:
6. 在项目添加 SonarQube 代码审查(流水线项目)
6.1 项目根目录下,创建 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
6.2 修改 Jenkinsfile,加入 SonarQube 代码审查阶段
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '0b127895-eb97-4f8f-b471-1277e5549b54', url: 'git@192.168.10.20:test-group/web_demo.git']]])
}
}
stage('code checking') {
steps {
script {
//引入了 sonarqube-scanner 工具
scannerHome = tool 'sonar-scanner'
}
//引入了 sonarqube 服务器系统环境
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('build project') {
steps {
sh 'mvn clean package'
}
}
stage('deploy item') {
steps {
deploy adapters: [tomcat8(credentialsId: '1cf9c5dd-8e2d-4eb6-8c00-bb60e0f027ca', path: '', url: 'http://192.168.10.40:8080/')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext(
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',body: '${FILE,path="email.html"}', to: '1520509800@qq.com'
)
}
}
}
6.3 把更改后的 sonar-project.properties 和 Jenkinsfile 进行提交
6.4 修改 jdk 版本
和非流水线里面一样,需要修改 jdk 版本。
6.5 开始构建 web_demo_pipeline
6.5 查看 sonarqube