前言
團(tuán)隊(duì)開發(fā)中,代碼質(zhì)量的把關(guān),往往決定了一個(gè)團(tuán)隊(duì)的開發(fā)維護(hù)效率。成員的增長(zhǎng),業(yè)務(wù)的擴(kuò)大,不同風(fēng)格、不嚴(yán)謹(jǐn)?shù)拇a,直接導(dǎo)致后續(xù)維護(hù)的高成本。
每個(gè)團(tuán)隊(duì)都有自己的一套代碼檢查方式。對(duì)于小團(tuán)隊(duì),也有很多高效可擴(kuò)展的開源工具。結(jié)合自己的實(shí)踐,介紹下一種解決方案:sonarqube + gerrit + Jenkins。
本篇介紹Mac上sonarqube的安裝,以及添加開源plugin對(duì)objective c支持。環(huán)境配置比較多,文字較長(zhǎng)...
所需安裝工具一覽
- homebrew
- Java JDK (推薦jdk而不是jre,最新的即可)
- maven
- xcode
- xcpretty
- sonarqube
- sonar-scanner
- oclint
- SonarQube Plugin for Objective C
注: 安裝順序推薦如上排序。建議先閱讀下文,了解各工具作用后再進(jìn)行安裝
sonarqube 安裝與使用
SonarQube? software (previously called Sonar) is an open source quality management platform, dedicated to continuously analyze and measure technical quality, from project portfolio to method. If you wish to extend the SonarQube platform with open source plugins, have a look at our plugin library.
1. 環(huán)境要求(具體見官網(wǎng))
* at least 2GB of RAM to run efficiently and 1GB of free RAM for the OS
* disk space you need will depend on how much code you analyze
* Database: [MySQL](http://www.mysql.com/)
* Web Browser
2. 安裝sonarqube
- download Download Page
- Unzip & execute: sonar.sh console(解壓后目錄里有系統(tǒng)對(duì)應(yīng)腳本)
# 主要命令一覽, 建議添加alias到~/.bash_profile
./sonar.sh console #Debug信息
./sonar.sh start #啟動(dòng)服務(wù)
./sonar.sh stop #停止服務(wù)
./sonar.sh restart #重啟服務(wù)
- Log in to http://localhost:9000 & 默認(rèn)登錄/密碼: admin/admin
- follow the tutorial to analyze your first project (無法添加project,需要安裝數(shù)據(jù)庫(kù)和scanner)
- 漢化方式:setting->market->搜Chinese
3. 安裝 & 配置數(shù)據(jù)庫(kù)(這里使用MySQL)
sonarqube內(nèi)嵌有數(shù)據(jù)庫(kù),但不能有額外操作,所以需要另外添加數(shù)據(jù)庫(kù)
# 直接使用homebrew,homebrew安裝方式請(qǐng)自查
brew install mysql #安裝
mysql.server start #啟動(dòng)
mysql_secure_installation #基本配置
brew services start mysql #配置為系統(tǒng)服務(wù)
mysql管理gui,optional:
配置sonar的用戶和數(shù)據(jù)庫(kù)實(shí)例:
- 創(chuàng)建sonar用戶和數(shù)據(jù)庫(kù)sonar,賦予sonar權(quán)限
mysql -u root -p #進(jìn)入MySQL, root默認(rèn)密碼為空
CREATE DATABASE sonar;
CREATE USER 'sonar'@'localhost' IDENTIFIED BY '你要設(shè)置的密碼’;
GRANT ALL PRIVILEGES ON sonar_qube.* TO 'sonar'@'localhost’;
FLUSH PRIVILEGES;
修改sonarqube數(shù)據(jù)庫(kù)配置
- 到sonarquebe目錄配置\conf\sonar.properties中數(shù)據(jù)庫(kù)設(shè)置,原文件相應(yīng)行被注釋
sonar.jdbc.username=sonar
sonar.jdbc.password=你要設(shè)置的密碼
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar_qube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
- restart sonarqube server,正常啟動(dòng),且不再有內(nèi)嵌數(shù)據(jù)庫(kù)提示
4. 安裝sonar-scanner & 配置
The SonarQube?Scanner is recommended as the default launcher to?analyze a project with SonarQube.
- Installation
- 解壓到安裝目錄,推薦添加bin目錄路徑到$PATH
export PATH=`pwd`/bin:$PATH >> ~/.bash_profile # 或直接copy bin路徑到~/.bash_profile
- 配置安裝目錄下的sonar-scanner.properties & 添加host.url
sonar.host.url=http://localhost:9000
對(duì)需檢測(cè)project執(zhí)行scanner命令
到project所在目錄,添加sonar-project.properties(注意命名不要錯(cuò)??)。添加project相關(guān)信息sonar-project.properties中,例如
sonar.projectKey=testOC #projectKey required
sonar.sources=/Users/hotacool/dev/RACTest #project source required
sonar.projectName=test OC #server 展示名字
sonar.projectVersion=1.0 # version
在該工程sonar-project.properties所在目錄下,執(zhí)行sonar-scanner,進(jìn)行掃描
打開server(http://localhost:9000/projects),可以看到projects掃描結(jié)果
5. oclint 安裝 & 使用
sonarqube對(duì)objectivec的代碼檢測(cè),需要依賴oclint對(duì)xcode工程的編譯log的分析結(jié)果。簡(jiǎn)單說下過程,對(duì)xcode工程進(jìn)行xcodebuild,輸出編譯log,oclint對(duì)編譯log進(jìn)行分析,輸出xml,sonarqube根據(jù)xml來顯示可視化結(jié)果。
OCLint is a static code analysis tool for improving quality and reducing defects by inspecting C, C++ and Objective-C code and looking for potential problems.
- 安裝方式見官網(wǎng)。
- Homebrew 簡(jiǎn)單,但可能外網(wǎng)連不上
- 推薦直接download包,解壓執(zhí)行文件路徑加入$PATH
- 安裝完畢,終端執(zhí)行oclint
- xcodebuild | tee xcodebuild.log | xcpretty -r json-compilation-database
- 編譯工程并輸出log,格式化log為json文件
- Xcodebuild 指令需要添加編譯相關(guān)參數(shù)
xcodebuild archive -workspace 項(xiàng)目名稱.xcworkspace
-scheme 項(xiàng)目名稱
-configuration 構(gòu)建配置
-archivePath archive包存儲(chǔ)路徑
CODE_SIGN_IDENTITY=證書
PROVISIONING_PROFILE=描述文件UUID
- oclint的執(zhí)行,需要依賴xcpretty
- Xcpretty 是編譯log format工具,直接gem安裝
sudo gem install xcpretty
- -r json-compilation-database 制定的數(shù)據(jù)的輸出格式為json格式。輸出的數(shù)據(jù)為build/reports/compilation_db.json
- 使用oclint時(shí)需要將build/reports/compilation_db.json 重新命名為 compile_commands.json 并移動(dòng)至當(dāng)前目錄(工程文件目錄),執(zhí)行:
oclint-json-compilation-database -- -report-type pmd -o oclint.xml #輸出xml
oclint-json-compilation-database -e Pods -v -- -report-type html -o report.html #輸出HTML,oclint使用xml
6. SonarQube Plugin for Objective C
-
Sonarqube market有收費(fèi)plugin可供安裝,but,感受一下:
image.png - 選用開源https://github.com/Backelite/sonar-objective-c(還在保持更新的),不過可能存在一些坑,需要自填。筆者fork了一下,更新了一下rule:Hotacool/sonar-objective-c。下面以Backelite為例:
- github download sonar-objective-c,unzip
- Java代碼,需要maven來編譯jar包,安裝maven:
brew install maven- sonar-objective-c 目錄(pom.xml所在目錄)編譯:
mvn package- /sonar-objective-c-plugin/target 目錄下輸出backelite-sonar-objective-c-plugin-0.6.2.jar
- Copy jar包到sonarqube安裝目錄下的/extensions/plugins文件夾
- 修改project的sonar-project.properties(參照sonar-objective-c-plugin的/sample/sonar-project.properties)
sonar.objectivec.oclint.report=oclint.xml sonar.objectivec.project=myApp.xcodeproj sonar.objectivec.appScheme=myApp sonar.objectivec.excludedPathsFromCoverage=.*Tests.*- 重啟sonarqube server & 重新運(yùn)行sonar-scanner
- 在server上可看到objectivec代碼被檢查,bingo~
- 可能存在問題:
- ERROR: The rule 'OCLint:compiler warning' does not exist.
- 原因是開源的sonar-objective-c plugin沒有包含這條rule,而oclint檢出了這個(gè)問題。幾種解決方案:
- oclint.xml 去掉相關(guān)報(bào)錯(cuò)
- 修改sonar-objective-c plugin,添加rule并打包—推薦
- 實(shí)際上,rule文件被存放在/sonar-objective-c-plugin/src/main/resources/org/sonar/plugins/oclint文件夾下,包含兩個(gè)文件:profile-oclint.xml 和 rules.txt
- 參照其他rule,添加compiler warning即可
- 重新打jar包、替換、重啟server 、 sonar-scanner...
參考鏈接:
