SonarQube構架
SonarQube(簡稱Sonar)是管理代碼質量的開放平臺,它可以快速地對代碼質量進行分析,並給出合理的解決方案,提高管理效率,保證代碼質量。
SonarQube框架包含以下四個部分:
Project
SonarQube Scanner
SonarQube Server
SonarQube Database
Project
是需要被分析的源碼,如我們的app工程源碼,SonarQube支持多種語言和多種工程結構,Andriod是屬於一種多模塊的Java工程。
SonarQube Scanner
是用於執行代碼分析的工具,在Project的根目錄下執行,我們還需要在Project下進行SonarQube配置,其中指定了工程的相關信息,還指定了SonarQube Server的地址,SonarQube Scanner分析完畢之後,會將結果上報到該Server。
注:官方對Scanner的描述是“The SonarQube Scanner is recommended as the default launcher to analyze a project with SonarQube”,個人理解是可以用任何其它的工具替代,只要能對Source進行分析,並生成Server能構解析的數據格式上報給Server。
SonarQube Server
顯示分析結果的Web Server,在SonarQube Scanner第一次將一個工程的分析結果上報給SonarQube
Server後,Server上會自動創建一個工程顯示分析的結果,可以在Server上設置代碼質量管理相關的各種配置,如設置代碼檢查規則(Rule)和質量門限(Quality
Gate)等。
SonarQube配置
SonarQube支持多種工程構建方式的配置,也對應需要用不同的Scanner來執行分析過程:
Ant
Maven
MSBuild(Microsoft Build Engine)
Gradle
Sonar Runner
一、安裝配置Sonar
1、需要下載的安裝包有下面兩個
https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.6.zip #sonar安裝包
https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip #sonar scanner安裝包
2、修改數據庫配置
#mysql -u root -p mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> CREATE USER 'sonar' IDENTIFIED BY 'Sonar123!'; mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'Sonar123!'; mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'Sonar123!'; mysql> FLUSH PRIVILEGES;
3、解壓修改配置
# unzip sonarqube-5.6.6.zip # mv sonarqube-5.6.6 sonarqube # cd sonarqube 修改sonar.properties配置 # vim conf/sonar.properties sonar.jdbc.username=sonar sonar.jdbc.password=Sonar123! sonar.jdbc.url=jdbc:mysql://10.61.100.32:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin
4、啓動sonarqube
# /home/sonarqube/bin/linux-x86-64/sonar.sh start # netstat -ntpl |grep 9000 tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 2045/java
此時通過瀏覽器就可以打開sonarqube界面,下面開始通過配置sonar scanner來分析代碼並在瀏覽器顯示。
二、安裝配置Sonar scanner
1、解壓並修改配置
# unzip sonar-scanner-cli-3.0.3.778-linux.zip # mv sonar-scanner-3.0.3.778-linux/ sonar-scanner # cd sonar-scanner # vim conf/sonar-scanner.properties sonar.host.url=http://10.61.100.34:9000 sonar.sourceEncoding=UTF-8 sonar.jdbc.username=sonar sonar.jdbc.password=Sonar123! sonar.jdbc.url=jdbc:mysql://10.61.100.32:3306/sonar?useUnicode=true&characterEncoding=utf8 onar.login=admin sonar.password=admin
2、拉取代碼做測試用
# git clone http://yull:[email protected]/rd/canbaobao.git
3、分析測試
# cd /home/coding/canbaobao #切換到git代碼根目錄 # /home/sonarqube/sonar-scanner/bin/sonar-scanner #成功執行完畢會有下面提示 INFO: ANALYSIS SUCCESSFUL, you can browse http://10.61.100.34:9000/dashboard/index/my:project INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report INFO: More about the report processing at http://10.61.100.34:9000/api/ce/task?id=AV0v7l4rYf3zHFnt0hqQ INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 55.900s INFO: Final Memory: 50M/464M INFO: ------------------------------------------------------------------------
4、此時即可在瀏覽器中查看代碼分析情況