準備工作:
首先下載SonarQube環境配置所需要的軟件
①SonarQube 社區版7.7 下載地址:
https://www.sonarqube.org/downloads/
②MySql5.6以上版本的數據庫(這裏以5.7版爲例):
https://dev.mysql.com/downloads/windows/installer/8.0.html
③SonarSource3.3掃描儀,此插件用與掃描代碼:
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
一、安裝
1.下載好sonarqube後,解壓打開bin目錄,啓動相應OS目錄下的StartSonar。如本手順演示使用的是win的64位系統,則打開C:\sonarqube-developer-7.7\sonarqube-7.7\bin\windows-x86-64\StartSonar.bat,不要關閉.Bat執行結果否則服務關閉啓動成功如下圖所示:
2.啓動瀏覽器,訪問http://localhost:9000,如出現下圖則表示安裝成功。
3、安裝mysql5.7數據庫,安裝步驟略。安裝完成後進行如下操作
①創建名爲sonar數據庫:數據語句爲
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
②執行數據庫腳本,創建數據庫及用戶,語句如下:
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar'; #login:sonar
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar'; # password:sonar
FLUSH PRIVILEGES;
4、打開sonarqube安裝目錄下的D:\sonarqube-developer-7.7\sonarqube-7.7\conf\sonar.properties文件
5.在sonar.properties文件中進行如下配置
# User credentials.節點下輸入以下信息(用戶名密碼是第三步設置的)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- MySQL >=5.6 && <8.0 節點輸入以下信息(用戶名密碼默認配置)
sonar.login=admin
sonar.password=admin
sonar.sorceEncoding=UTF-8
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
url是數據庫連接地址,username是數據庫用戶名,jdbc.password是數據庫密碼,login是sonarqube的登錄名,sonar.password是sonarqube的密碼
6.重啓sonarqube服務,再次訪問http://localhost:9000,會稍微有點慢,因爲要初始化數據庫信息
7.數據庫初始化成功後,登錄,用戶名和密碼默認都是admin,登陸成功後如下圖:
8、打開SonarSource3.3掃描儀中配置文件 sonar-scanner.properties文件
9、打開配置文件後,發現新版的scanner文件註釋內容較少,因此直接在下方加入Mysql節點配置情況。同sonarqube中的sonar.properties文件配置,配置如下:
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
#sonar.host.url=http://localhsot:9000
sonar.host.url=http://ip:9000
sonar.jdbc.url=jdbc:mysql://ip:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.login=admin
sonar.password=admin
10、環境變量配置
【此電腦】–右鍵【屬性】–【高級系統設置】–【環境變量】
先新增用戶變量在添加環境變量,但是如果直接新增path可以不用考慮。
a.新建變量,name=SONAR_RUNNER_HOME。value=C:\Sonar\sonar-scanner-3.3.0(該路徑爲掃描儀路徑)
b.打開path,輸入%SONAR_RUNNER_HOME%\bin
11、環境測試
打開命令提示符窗口(不用管理員身份),輸入sonar-scanner -version,如果配置成功出現版本信息,如下圖。
配置基本完成
在eclipse中安裝SonarLint插件:https://blog.csdn.net/qqq1994_0810/article/details/90260726
在windows系統下sonarQube使用:https://blog.csdn.net/qqq1994_0810/article/details/90260625