1.安装SonarQube 这个就不演示了,上一篇文章有介绍过,这篇文章着重演示Jenkins和SonarQube的整合
2.Jenkins安装SonarQube插件 SonarQube Scanner 安装成功
3.配置SonarQube工具 4.添加SonarQube凭证 5.绑定SonarQube 6.自由风格项目构建 进入设置
# must be unique in a given SonarQube instance # 必须是唯一的在一个给定的SonarQube实例 sonar.projectKey=test1 # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. # 这是SonarQube UI中显示的名称和版本。在SonarQube 6.1之前是强制性的 sonar.projectName=test1 sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" onWindows. # Path是相对于声纳项目的。属性文件。将“\”替换为“/”。 # This property is optional if sonar.modules is set. # 这个属性是可选的,如果声纳。模块设置。 #源码目录 sonar.sources=. # 需要排除的目录 sonar.exclusions=**/test/**,**/target/** # 二进制文件路径 sonar.java.binaries=./target/classes # JDK版本 sonar.java.source=1.8 sonar.java.target=1.8 # Encoding of the source code. Default is default system encoding # 非编码的源代码。默认是默认的系统编码 sonar.sourceEncoding=UTF-8保存应用
7.构建项目
8.查看SonarQube客户端 搞定!!!
9.测试代码审查 添加异常 从新构建 查看SonarQube客户端 进入审查列表 进入bug 查看详情
10.流水线项目构建 项目中根目录创建sonar-project.properties文件和Jenkinsfile文件 sonar-project.properties
# must be unique in a given SonarQube instance sonar.projectKey=test1_pip # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=test1_pip sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" onWindows. # This property is optional if sonar.modules is set. sonar.sources=. sonar.exclusions=**/test/**,**/target/** sonar.java.binaries=./target/classes sonar.java.source=1.8 sonar.java.target=1.8 # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8Jenkinsfile
pipeline { agent any stages { stage('pull code') { steps { echo '拉取拉取代码' checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'b52a369a-9e90-4971-bd55-6ba17eb273f1', url: 'http://192.168.0.188:8888/gitlab/liutao/test.git']]]) } } stage('code checking') { steps { echo '代码检查' script { //引入Jenkins全局配置中配置的SonarQube工具名字 //自由风格会默认去找全局配置中的这个工具 scannerHome = tool 'SonarQube' } //SonarQube引入SonarQube的服务器环境 就是全局配置SonarQube servers withSonarQubeEnv('SonarQube') { sh "${scannerHome}/bin/sonar-scanner" } } } stage('build project') { steps { echo 'Maven译编译打包' sh label: '', script: 'mvn clean package' } } } }推送远程仓库
11.创建流水线项目类型
12.开始构建 13查看SonarQube客户端