1. 程式人生 > >linux安裝sonarQube的安裝、配置與使用

linux安裝sonarQube的安裝、配置與使用

轉自:https://www.cnblogs.com/qiaoyeye/p/5249786.html

SonarQube是管理程式碼質量一個開放平臺,可以快速的定位程式碼中潛在的或者明顯的錯誤,下面將會介紹一下這個工具的安裝、配置以及使用。

準備工作;

1、jdk(不再介紹)

2、sonarqube:http://www.sonarqube.org/downloads/

3、SonarQube+Scanner:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.5.zip

4、mysql資料庫(不再介紹)

一、安裝篇

1.下載好sonarqube後,解壓開啟bin目錄,啟動相應OS目錄下的啟動檔案。

linux目錄:/opt/softs

解壓命令:

sudo unzip -q sonarqube-6.7.2.zip -d /opt/softs
啟動命令:
cd /opt/softs/sonarqube-6.7.2/bin/linux-x86-64

./sonar.sh start

上面連結解決之後,出現下面第二步

2.啟動瀏覽器,訪問http://localhost:9000,如出現下圖則表示安裝成功。

二、配置MySql資料庫篇

1.開啟mysql,新建一個數據庫。

2.開啟sonarqube安裝目錄下的/opt/softs/sonarqube-6.7.2/conf/sonar.properties檔案

3.在mysql5.X節點下輸入以下資訊

複製程式碼
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=sonar
sonar.jdbc.password=Test1234

url是資料庫連線地址,username是資料庫使用者名稱,jdbc.password是資料庫密碼,login是sonarqube的登入名,sonar.password是sonarqube的密碼

4.重啟sonarqube服務,再次訪問http://localhost:9000,會稍微有點慢,因為要初始化資料庫資訊

5.資料庫初始化成功後,登入;預設使用者名稱和密碼是admin;admin

三、安裝中文外掛

1.登入之後依次選擇下面,參照圖片安裝

“Administrator” -> “Marketplace”


2.等待安裝完畢之後重啟。

3.再次登入時;介面就變成中文的了。


四、win10安裝SonarQube+Scanner使用篇

2.開啟D:\sonar\sonar-scanner-2.8\conf\sonar-runner.properties檔案

3.mysql節點下輸入以下資訊

sonar.jdbc.url=jdbc:mysql://localhost:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

注意:如果測試專案與伺服器不在同一臺機子,則需要新增伺服器的IP:

#----- Default SonarQube server
sonar.host.url=http://XXX.XXX.XXX.XXX:9000

3.配置環境變數

a.新建變數,name=SONAR_RUNNER_HOME。value=D:\sonar\sonar-scanner-2.8

b.開啟path,輸入%SONAR_RUNNER_HOME%\bin;

c.sonar-runner -version,出現以下資訊,則表示環境變數設定成功

五、linux安裝SonarQube+Scanner使用篇

2.解壓到目錄(可解壓到任意目錄)。修改系統路 徑 path,如下:2.1.切換到root使用者

2.2.通過修改profile檔案:
vim /etc/profile

2.3.輸入如下內容:

#sonar-scanner profile 
export
 SONAR_SCANNER_HOME=/opt/softs/sonarqube-6.7.2/sonar-scanner-2.8
export
 PATH=${SONAR_SCANNER_HOME}/bin:${PATH}
2.4要讓剛才的修改馬上生效,需要執行以下程式碼  # source /etc/profile 

3.驗證是否配置正確;輸入:

$ sonar-runner -v

出現下圖就證明ok


六、使用篇


1.開啟要進行程式碼分析的專案根目錄,新建sonar-project.properties檔案

2.輸入以下資訊

複製程式碼
# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=apiautocore
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
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
複製程式碼

其中:projectName是專案名字,sources是原始檔所在的目錄

3.設定成功後,啟動sonarqube服務,並啟動cmd

4.在cmd進入專案所在的根目錄,輸入命令:sonar-runner,分析成功後會出現下圖

5.開啟http://localhost:9000/,我們會看到主頁出現了分析專案的概要圖

6.我們點選專案,選擇問題連結,會看到分析程式碼的bug,哇,好多

7.選擇一個最嚴重的bug,看看

原來是這個地方會出現空指標異常,原因是我沒有進行初始化就使用這個變量了。