代碼檢測平臺Sonarqube +Sonar Scanner 搭建與部署

一、準備工作

 

1、jdk(1.8及以上)

2、SonarQube:http://www.sonarqube.org/downloads/(推薦下載社區版)

3、Sonar Scanner:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/

4、mysql數據庫(5.6至8.0版本)

 

二、安裝配置sonarqube

 

說明:SonarQube官網最新版是7.9,已經不再支持mysql,如果需要數據庫需要部署到Mysql,請下低版本。

 

1、下載完成後,下載後,解壓,即安裝成功。

 

2、配置 (mysql數據庫)

 

 

修改conf目錄下的sonar.properties文件

sonar.jdbc.url=jdbc:mysql://192.168.24.53:3306/dev_orders?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=order
sonar.jdbc.password=order@123
sonar.sorceEncoding=UTF-8





 

3、環境變量

 

 

SONAR_HOME    D:\sonar\sonarqube-7.8

 

Path   ;%SONAR_HOME%\bin;

 

 

4、運行

 

打開bin目錄下StartSonar.bat 啓動,window 64位 啓動路徑 :D:\sonar\sonarqube-7.8\bin\windows-x86-64

 

啓動成功後,在關閉Sonarqube服務,注意用快捷鍵Ctrl+C ,然後輸入Y退出。

不要直接關閉,不然再次啓動時會出現進程被佔用的問題。

 

5、訪問首頁

 

啓動瀏覽器,訪問http://localhost:9000,如出現下圖則表示啓動成功.(我這裏已經部署了項目,新搭建的的項目數爲0)

 

6、安裝中文包

 

 

三、安裝配置SonarScanner

 

 

1、安裝

 

下載後,解壓,即安裝成功。

 

 

2、配置

 

 

修改conf目錄下的sonar-runner.properties文件。

sonar.host.url=http://localhost:9000

sonar.jdbc.url=jdbc:mysql://192.168.24.53:3306/dev_orders?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false


sonar.jdbc.username=order
sonar.jdbc.password=order@123

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password=admin


 

 

3、環境變量

 

 

  • SONAR_RUNNER_HOME   D:\sonar\sonar-scanner-4.0.0.1744-windows
     
  • Path   ;%SONAR_RUNNER_HOME%\bin;
  • sonar-scanner -version,出現以下信息,則表示環境變量設置成功

 

 

4、打開要進行代碼分析的項目根目錄,新建sonar-project.properties文件

 

# must be unique in a given SonarQube instance

sonar.projectKey=member-center

# this is the name displayed in the SonarQube UI

sonar.projectName=member-center

sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.

# Since SonarQube 4.2, this property is optional if sonar.modules is set.

# If not set, SonarQube starts looking for source code from the directory containing

# the sonar-project.properties file.

sonar.sources=src

sonar.java.binaries=target

sonar.language=java

# Encoding of the source code. Default is default system encoding

sonar.sourceEncoding=UTF-8



 

 

其中:projectName是項目名字,sources是源文件所在的目錄,binaries是class文件所在的目錄

 

6.設置成功後,啓動sonarqube服務,並啓動cmd

 

 

7.在cmd進入項目所在的根目錄,輸入命令:sonar-scanner,分析成功後會出現下圖

 

 

8.打開http://localhost:9000/,我們會看到主頁出現了分析項目的概要圖

 

9、點擊項目member-center ,查看問題,可以看到具體的代碼bug信息

 

四、參考文檔

 

https://docs.sonarqube.org/latest/

https://www.cnblogs.com/lyh421/p/6727774.html?tdsourcetag=s_pctim_aiomsg

https://my.oschina.net/shuming/blog/2052375

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章