
[TOC]
# 1. SonarQube上配置
**1. 關閉審查結果上傳到SCM功能**

**2. 添加項目**




```
daae43d6a75f1367245e7d5acc79cb97f3c60bd8
```

```
mvn sonar:sonar \
-Dsonar.projectKey=web-demo-pipeline-sonarqube \
-Dsonar.host.url=http://192.168.1.25:9000/sonarqube \
-Dsonar.login=daae43d6a75f1367245e7d5acc79cb97f3c60bd8
```
<br/>
# 2. Jenkins上配置
**1. 安裝插件 SonarQube Scanner**
**2. 添加SonarQube憑證**

>[warning]提醒:不是一個sonarqube項目就需要在jenkins中添加一個sonarqube憑證,首次添加這個憑證后,后面在sonarqube創建的所有項目都可以共享這個憑證與jenkins交互。
**3. 配置SonarQube servers**
Manage Jenkins(系統管理) -> Configure System(系統配置) -> SonarQube servers。

**4. 配置SonarQube Scanner**
Manage Jenkins(系統管理) -> Global Tool Configuration(全局工具配置) -> SonarQube Scanner。

<br/>
# 3. 項目上配置
在項目添加SonarQube代碼審查。
<br/>
## 3.1 非流水線項目
如果是非流水線項目按照本章步驟在項目中添加sonarqube審查代碼。
**1. 在項目根目錄下添加文件`sonar-project.properties`**

>[warning]1. 文件名必須是`sonar-project.properties` 。
> 2. 必須放在項目根目錄下,不能放在resources目錄下。
```properties
# must be unique in a given SonarQube instance
sonar.projectKey=web-demo-maven-sonarqube
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=web-demo-maven-sonarqube
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
```
**2. 記得把修改提交到gitlab上**
<br/>
## 3.2 流水線項目
如果是流水線項目按照本章步驟在項目中添加sonarqube審查代碼。
**1. 項目根目錄下添加文件`sonar-project.properties`**

>[warning]1. 文件名必須是`sonar-project.properties` 。
> 2. 必須放在項目根目錄下,不能放在resources目錄下。
```properties
# must be unique in a given SonarQube instance
sonar.projectKey=web-demo-pipeline-sonarqube
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=web-demo-pipeline-sonarqube
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
```
**2. 在`Jenkinsfile`文件中加入SonarQube代碼審查階段**
```groovy
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']]
, doGenerateSubmoduleConfigurations: false
, extensions: [], submoduleCfg: []
, userRemoteConfigs: [[credentialsId: 'e4e02eb6-f6bb-4040-b842-c1423c397493'
, url: 'git@gitlab.master.com:itheima_group/web-demo-pipeline-sonarqube.git']]])
}
}
stage('編譯構建') {
steps {
sh label: '', script: 'mvn clean package'
}
}
stage('SonarQube代碼審查') {
steps{
script {
scannerHome = tool 'sonarqube-scanner'
}
withSonarQubeEnv('sonarqube-8.9.6.50800') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('項目部署') {
steps {
deploy adapters: [tomcat9(credentialsId: '748ce750-b1af-4730-9e43-1b6b4905a8dc', path: '', url: 'http://tomcat.master.com:8080')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext body: '${FILE,path="src/main/resources/email.html"}',
subject: '構建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} !',
to: 'xxxb@163.com,xxxx10@qq.com'
}
}
}
```
**3. 記得把修改提交到gitlab上**
<br/>
# 4. Jenkins上構建并到SonarQube查看審查結果
在Jenkins上構建后到SonarQube查看審查結果。

- 相關概念
- 軟件開發生命周期
- 軟件開發瀑布模型
- 軟件的敏捷開發
- 持續集成
- Jenkins介紹
- Jenkins是什么
- Jenkins的特征
- Jenkins環境搭建
- 搭建架構說明
- Gitlab安裝與配置
- Jenkins安裝與配置
- Tomcat安裝和配置
- Jenkins構建項目
- 自由風格軟件項目構建
- Maven項目構建
- Pipeline流水線項目構建
- Pipeline是什么
- Pipeline語法
- 流水線項目構建演示
- Pipeline Script from SCM
- 構建觸發器
- 觸發遠程構建
- 其他工程構建后觸發
- 定時構建
- 輪詢SCM
- Git hook自動觸發構建
- 參數化構建
- 配置郵箱發送構建結果
- SonarQube代碼審查平臺
- SonarQube是什么
- SonarQube平臺搭建
- 安裝jdk11
- 安裝數據庫PostgreSQL12
- 安裝SonarQube
- SonarQube實現代碼審查
- Jenkins+Docker+SpringCloud(1)
- 流程說明
- 環境搭建
- 服務器列表
- Docker安裝與配置
- Harbor安裝與配置
- Nginx安裝與配置
- 微服務持續集成演示
- Jenkins上配置
- 微服務項目配置
- 部署前端靜態web網站