[TOC]
### 安裝mysql運行環境
#### 下載mysql:5.7 鏡像
```cmd
docker pull mysql:5.7
```
#### 啟動mysql:5.7 容器
數據掛載到宿主機的/data/mysql5.7-data目錄下,端口為3306,
```cmd
docker run --name mysql5.7 -v /data/mysql5.7-data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
```
#### 進入容器中創建數據庫與用戶
1. 進入容器
```cmd
docker exec -it mysql5.7 bash
```
2. 連接數據庫
```cmd
mysql -uroot -p123456
```
3. 創建數據庫db_sonar
```cmd
create database db_sonar character set utf8 collate utf8_general_ci;
flush privileges;
```
4. 創建數據庫用戶,用戶名密碼都為`sonar`
```cmd
grant all privileges on db_sonar.* to 'sonar'@'%'identified by 'sonar' with grant option;
flush privileges;
```
#### 查看mysql容器ip
```cmd
root@ubuntu:/data# docker exec -it mysql5.7 bash
root@ec7039cd8020:/#
root@ec7039cd8020:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 ec7039cd8020
```
結果:
由上面命令可以查到,我的容器ip為`172.17.0.2`
### 安裝SonarQube
#### 下載 sonarqube:6.7.5 鏡像
```cmd
docker pull sonarqube:6.7.5
```
#### 啟動sonarqube容器
* -v 掛載相關配置到 /data/sonar/ 目錄下
* -e 指定數據庫相關參數
```cmd
docker run -d --name sonar -p 9000:9000 -p 9092:9092 -v /data/sonar/conf:/opt/sonarqube/conf -v /data/sonar/data:/opt/sonarqube/data -v /data/sonar/logs:/opt/sonarqube/logs -v /data/sonar/extensions:/opt/sonarqube/extensions -e "SONARQUBE_JDBC_USERNAME=sonar" -e "SONARQUBE_JDBC_PASSWORD=sonar" -e "SONARQUBE_JDBC_URL=jdbc:mysql://172.17.0.2:3306/db_sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false" sonarqube:6.7.5
```
### SonarQube 實例操作
1. 訪問瀏覽器:http://192.168.147.128:9000 ,這里`192.168.147.128`是我的宿主機ip。

2. 登錄,默認賬號密碼都是admin,首次登錄后,會有一個使用教程,如下
根據提示,創建token

3. 實例掃描一個maven工程
* step1:準備一個maven工程
maven下載地址: https://pan.baidu.com/s/1WEvNvMNggXW6s2YIfEFXLw 提取碼: 9fx4
demo 下載地址:https://pan.baidu.com/s/1gkMR0sLday9RREu6o6fzBA 提取碼: cv47
這里我的maven解壓地址為:D:\apache-maven-3.6.0
* step2:這里以掃描maven項目為例,進入到工程根目錄下,執行掃描命令為:
`
D:\apache-maven-3.6.0\bin\mvn sonar:sonar -Dsonar.host.url=http://192.168.147.128:9000 -Dsonar.login=b20387fd392e64e9e1cf14501900e3c5149acbcb
`
構建成功后,如下:
```cmd
[INFO] ANALYSIS SUCCESSFUL, you can browse http://192.168.147.128:9000/dashboard/index/com.rfchina:utils
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://192.168.147.128:9000/api/ce/task?id=AWgw2wxiWWteYCl4dwII
[INFO] Task total time: 5.034 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.265 s
[INFO] Finished at: 2019-01-09T12:24:49+08:00
[INFO] ------------------------------------------------------------------------
```
* step3:重新訪問Sonar后,會發現在項目列表中已經加入了該項目的分析報告。

### SonarQube Scanner 代碼分析
#### 安裝 Scanner
通過 [SonarQube Scanner](https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner) 進行代碼分析
1. 下載Scanner
Scanner 下載地址為:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
(百度云盤,win64: https://pan.baidu.com/s/1aFRCi7iu1QTwgPZynlltfA 提取碼: s6ra)
2. 解壓,到目錄E:\tools\sonar-scanner
3. 修改全局配置 E:\tools\sonar-scanner\conf\sonar-scanner.properties
```
#----- Default SonarQube server
sonar.host.url=http://192.168.147.128:9000
```
4. 將E:\tools\sonar-scanner\bin目錄添加到環境變量中
5. 檢查sonar-scanner 配置結果
```cmd
C:\Users\guanfuchang>sonar-scanner -h
INFO:
INFO: usage: sonar-scanner [options]
INFO:
INFO: Options:
INFO: -D,--define <arg> Define property
INFO: -h,--help Display help information
INFO: -v,--version Display version information
INFO: -X,--debug Produce execution debug output
```
#### 使用Scanner
在項目根目錄下,創建配置文件`sonar-project.properties`
```cmd
# must be unique in a given SonarQube instance
sonar.projectKey=rfchina:utils
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=rfchina_utils
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.java.binaries=./target/classes
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
```
在項目根目錄下,執行命令:
```cmd
sonar-scanner
```
執行成功后,如下
```cmd
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.147.128:9000/dashboard/index/rfchina:utils
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.147.128:9000/api/ce/task?id=AWgxHkcAWWteYCl4dwIW
INFO: Task total time: 3.285 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 4.854s
INFO: Final Memory: 18M/380M
INFO: ------------------------------------------------------------------------
```
訪問Sonar,將會顯示出該項目的分析結果
