Linux下sonarqube安裝使用方法(程式碼check工具)
背景
1、 虛擬機器CentOS release 7.2
2、 sonarqube-6.2
3、 sonar-scanner-2.8
一、 sonarqube-6.2的安裝
這裡下載的是sonarqube-6.2,下載完成之後解壓下載的zip壓縮包。會產生下圖所示的檔案目錄。
2、 配置sonarqube
進入conf資料夾,開啟sonar.properties進行配置。Sonarqube預設ip為localhost,預設埠為9000。如果想修改,可以將sonar.properties檔案中
sonar.web.host=localhost
sonar.web.port=9000
的註釋去掉,並修改ip和port,如果使用預設,不去掉註釋也是可以的。
單純做測試時可以直接使用sonarqube預設的Embedded Database,不需要安裝其他資料庫。這裡使用的就是預設的Embedded Database。
3、 Sonarqube啟動。
首先啟動sonarqube服務,進入到bin目錄下,選擇相應linux系統,這裡是linux-x86-64,進入到inux-x86-64目錄,執行./sonar.sh start啟動sonarqube。
同時可以去logs檔案中檢視日誌,確認啟動正常。
正常啟動後會在inux-x86-64目錄下產生一個SonarQube.pid檔案。
點選右上角的Login按鈕,登陸sonarqube,預設使用者名稱和密碼為admin/admin。
登陸後畫面如下:
二、 sonar-scanner-2.8的安裝
如果要對程式碼進行檢查,必須要安裝sonar-scanner。
1、 到sonarqube官網下載sonar-scanner-2.8,當然可以使用其他版本,但是要注意版本支援問題,詳情去https://www.sonarqube.org/官網檢視。
下載完成之後解壓sonar-scanner-2.8.zip,產生下圖所示的檔案目錄:
如果sonarqube都是預設設定,這裡也不需要配置,如果sonarqube有做設定,這裡在conf目錄下的sonar-scanner.properties檔案中做對應的設定。
2、 建立sonar-project.properties。
對程式碼進行分析,必須要建立sonar-project.properties檔案,執行sonar-scanner時,會對根據sonar-project.properties檔案進行搜尋,該檔案要放在與專案(如下圖中src)同一目錄下。
例:舉例建立sonar-project.properties檔案。
設定引數具體意義參考:
3、 進入到bin目下,可以看到很多可執行檔案,如下圖:
在linux系統下,綠色可執行,這裡執行sonar-scanner就可以了。
為了方便sonar-scanner執行,可將其設定為環境變數,投入下列命令:
exportSONAR_RUNNER_HOME=/root/zfl/sonar-scanner-2.8
exportPATH=$SONAR_RUNNER_HOME/bin:$PATH
目錄根據自己存放的位置進行選擇。
4、 執行sonar-scanner。
進入到存放sonar-project.properties檔案的目錄,執行sonar-scanner,執行結果顯示EXECUTION SUCCESS,則解析成功。
Quality Gate:
A quality gate is the best way to enforce a qualitypolicy in your organization. It's there to answer ONE question : can I delivermy project to production today or not ?
Issues:
BLOCKER:Bug with a high probability to impact the behavior of theapplication in production: memory leak, unclosed JDBC connection, .... The codeMUST be immediately fixed.
CRITICAL:Either a bug with a low probability to impact the behavior ofthe application in production or an issue which represents a security flaw:empty catch block, SQL injection, ... The code MUST be immediatelyreviewed.
MAJOR:Quality flaw which can highly impact the developerproductivity: uncovered piece of code, duplicated blocks, unused parameters,...
MINOR:Quality flaw which can slightly impact the developerproductivity: lines should not be too long, "switch" statementsshould have at least 3 cases, ...
INFO:Neither a bug nor a quality flaw, just a finding.
總結:一般Quality Gate顯示passed,則說明程式碼質量是OK的,如果顯示Failed,進入project中,檢視有哪些問題,根據顯示進行修改。
Project 檢視參照下列網址:
對於sonarqube網頁顯示結果的分析,具體可參照
https://docs.sonarqube.org/display/SONAR/Analyzing+Source+Code。