1. 程式人生 > >使用OCLint和Sonar對iOS代碼分析和質量管理

使用OCLint和Sonar對iOS代碼分析和質量管理

環境 測試 版本 ann true onf brew Coding 模板

OCLint 是一個強大的靜態代碼分析工具,可以用來提高代碼質量,查找潛在的bug,主要針對c,c++和Objective-c的靜態分析。

Sonar 是一個用於代碼質量管理的開放平臺。通過插件機制,Sonar 可以集成不同的測試工具,代碼分析工具,以及持續集成工具。

SonarQube安裝
1 brew install sonarqube
SonarQube漢化

漢化插件

https://github.com/SonarQubeCommunity/sonar-l10n-zh

下載後放入如下目錄:

1 /usr/local/Cellar/sonarqube/6.5/libexec/extensions/plugins
Mysql安裝
1 brew install mysql

創建sonar數據庫

配置sonar連接mysql
1 2 3 4 5 6 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false sonar.jdbc.username=root sonar.jdbc.password=root sonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin
安裝xcpretty
1 gem install xcpretty
安裝OCLint
1 2 brew tap oclint/formula brew install oclint
安裝sonar-scanner
1 brew install sonar-scanner

安裝sonar-objective-c-plugin

backelite-sonar-objective-c-plugin-0.6.2.jar

下載後放入如下目錄:

1 /usr/local/Cellar/sonarqube/6.5/libexec/extensions/plugins
重啟sonar
1 sonar restart

到此Sonar環境已經基本配置好了,打開 http://127.0.0.1:9000

技術分享圖片

工程配置

run-sonar.shsonar-project.properties模板放到根目錄下, 修改.properties文件的內容, 然後執行run-sonar.sh就可以了。文件地址https://github.com/LeoXu92/SonarShell-iOS

最終效果
技術分享圖片

(ps.技術債累積的比較多,是時候改改了。)

遇到的問題

執行sonar-scanner時提示”The rule ‘XXX’ does not exist”

在oclint在新版本版本會新增了些規則, 需要更新sonar服務器上對應的規則列表.

先下載https://github.com/LeoXu92/sonar-objective-c代碼。

然後後在路徑: sonar-objective-c-develop/sonar-objective-c-plugin/src/main/resources/org/sonar/plugins/oclint 下的profile-oclint.xmlrules.txt這兩個文件。添加對應的規則。

然後再執行 build-and-deploy.sh 編譯出新的jar包。

如果啟動不了SonarQube

刪除sonarqube/6.7.1/libexec/temp文件夾重啟試試。

使用OCLint和Sonar對iOS代碼分析和質量管理