sonar程式碼質量檢測工具的配置和使用
背景:我們一直在重構,調整架構,但是一直不知道我們做的效果如何,也沒有辦法量化,所以需要引入一個工具和標準來量化我們的工作。
簡介:sonarqube是一個開源的,程式碼質量檢測的工具,我們可以使用它來檢測我們的程式碼。
如何安裝:
2.將安裝包解壓到指定目錄
進入以下目錄:
/Users/xiepengchong/opensource/sonarqube-5.6.3/bin/macosx-universal-64
執行:
./sonar.sh start
即可啟動該功能。
如何使用:
下載sonarqube runner
配置sonar-runner的環境變數
然後進入我們需要執行檢查的工程下面。
新建一個名字是 sonar-project.properties的檔案。(內容如下)
sonar.projectKey=UCarNew sonar.projectName=UCarNew sonar.projectVersion=5.0 # Path to the parent source code directory. # 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=./ # Encoding of the source code sonar.sourceEncoding=UTF-8 # Additional parameters sonar.my.property=value
最後直接執行:
sonar-runner
就可以進行檢查了,
如何檢視結果:
檢查有如下日誌:
15:08:46.758 WARN - This may lead to missing/broken features in SonarQube
15:08:46.758 INFO - Sensor SCM Sensor (done) | time=21631ms
15:08:46.758 INFO - Sensor Zero Coverage Sensor
15:08:46.962 INFO - Sensor Zero Coverage Sensor (done) | time=204ms
15:08:46.962 INFO - Sensor Code Colorizer Sensor
15:08:46.977 INFO - Sensor Code Colorizer Sensor (done) | time=15ms
15:08:46.977 INFO - Sensor CPD Block Indexer
15:08:46.978 INFO - JavaCpdBlockIndexer is used for java
15:08:47.976 INFO - Sensor CPD Block Indexer (done) | time=999ms
15:08:48.120 INFO - Calculating CPD for 1179 files
15:08:48.432 INFO - CPD calculation finished
15:08:50.380 INFO - Analysis report generated in 1556ms, dir size=14 MB
15:08:53.133 INFO - Analysis reports compressed in 2753ms, zip size=6 MB
15:08:53.357 INFO - Analysis report uploaded in 224ms
15:08:53.358 INFO - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
開啟網址,可以看到如下頁面。
通過切換不同的tab,可以檢視詳細資訊。
最後說一下如何提高我們的程式碼質量:
1.恰當的使用if,switch等判斷邏輯的使用。因為每一個邏輯的判斷都是一個複雜度的引入,
2.減少重複的程式碼,每一段重複的程式碼將會造成維護工作量的double,
3.減少每個檔案的大小,檔案的大小與維護成本成指數級別增加。
PS:時隔幾天之後,啟動的時候,突然不能用了。
wudideMacBook-Pro:bin xiepengchong$ ./sonar-runner -X
SonarQube Runner 2.4
Java 1.8.0_101 Oracle Corporation (64-bit)
Mac OS X 10.10.5 x86_64
INFO: Error stacktraces are turned on.
INFO: Runner configuration file: /Users/xiepengchong/opensource/sonar-runner-2.4/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "zh_CN", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Work directory: /Users/xiepengchong/opensource/sonar-runner-2.4/bin/./.sonar
ERROR: Sonar server 'http://localhost:9000' can not be reached
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 0.032s
Final Memory: 2M/245M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
以為是版本的問題,後來發現是我啟動的不同/Users/xiepengchong/Downloads/sonarqube-5.6.3/bin/macosx-universal-64
在這個路徑下,啟動,就解決了,真是很奇怪,我以為路徑沒關係呢。
最近(2017年12.4日)再次未新公司的程式碼做檢查的時候,在linux伺服器上搭建了一個環境,(sonarqube-6.7.zip)
發現竟然報錯了
Please provide compiled classes of your project with sonar.java.binaries property
之後又在配置檔案中增加了一下路徑:
sonar.projectKey=PowerPlus
sonar.projectName=PowerPlus
sonar.projectVersion=1.0
# Path to the parent source code directory.
# 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=./
# Encoding of the source code
sonar.sourceEncoding=UTF-8
# Additional parameters
sonar.my.property=value
sonar.java.binaries=app/build/intermediates/classes