OClint是針對C, C++及Objective C代碼的靜態掃描分析工具,而SonarQube是一個開源的代碼質量管理平臺。本文將實現將OClint的掃描結果導入到SonarQube中,已實現對Objective C代碼質量的管理。html
操做系統:mysql
Mac OS X 10.9git
所需工具:github
SonarQube : sonarqube-4.4 - http://www.sonarqube.org/downloads/objective-c
Sonar Runner : sonar-runner-dist-2.4 - http://www.sonarqube.org/downloads/sql
MySQL 5.x : 5.0.90 MySQL Community Server (GPL) - http://dev.mysql.com/downloads/mysql/數據庫
OClint : oclint-0.9.dev.5f3418c - http://oclint.org/downloads.html選擇mac os x或者darwin的包macos
xcodebuild: Xcode 5.x - https://developer.apple.com/xcode/downloads/json
所需組件:xcode
Sonar Plugin for Objective C
能夠直接下載sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar
也能夠在https://github.com/octo-technology/sonar-objective-c/tree/oclint 下載源碼,並執行其中的build-and-deploy.sh編譯
環境搭建:
下載並安裝MySQL;
建立sonar數據庫及用戶;
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;CREATE USER 'sonar' IDENTIFIED BY 'sonar';GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar'; FLUSH PRIVILEGES;
3. 下載並解壓SonarQube (例如:"/etc/sonarqube")
4. 將sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar放到SonarQube的擴展插件目錄下 (例如:"/etc/sonarqube/extensions/plugins")
5. 配置sonar.properties (例如:"/etc/sonarqube/conf")
1
2
3
4
5
6
7
|
# H2 embedded database server listening port, defaults to
9092
#sonar.embeddedDatabase.port=
9092
#----- MySQL
5
.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url=jdbc:mysql:
//localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
|
6. 啓動SonarQube服務器
$ /etc/sonarqube/bin/macosx-universal-64/sonar.sh console Running SonarQube... wrapper | --> Wrapper Started as Console wrapper | Launching a JVM... jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.orgjvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. jvm 1 | jvm 1 | 2014.09.06 14:45:53 INFO Web server is started
7. 測試SonarQube
http://localhost:9000/
8. 下載並解壓Sonar Runner (例如:"/etc/sonar-runner")
9. 配置Sonar Runner下的sonar-runner.properties (例如:"/etc/sonar-runner/conf/ sonar-runner.properties")
#----- Default SonarQube server sonar.host.url=http://localhost:9000#----- MySQL sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8#----- Global database settings sonar.jdbc.username=sonar sonar.jdbc.password=sonar
10. 下載並解壓OClint(例如:"/etc/oclint")
11. 測試OClint
$ /etc/oclint/bin/oclint oclint: Not enough positional command line arguments specified!Must specify at least 1 positional arguments: See: oclint -help
12. 測試xcodebuild
$ xcodebuild -version Xcode 5.0.2Build version 5A3005
掃描代碼:
在bash中進入代碼目錄(.xcodeproj文件所在目錄), 執行 xcodebuild | tee xcodebuild.log
在bash中執行 oclint-xcodebuild xcodebuild.log
在bash中執行oclint-json-compilation-database -- -report-type pmd -o sonar-reports/oclint.xml。
將sonar-project.properties存放到代碼目錄中,根據具體狀況編輯對應的項,須要特別注意其中的sonar.objectivec.project和sonar.objectivec.appScheme
########################## # Required configuration # ########################## sonar.projectKey=my-project sonar.projectName=My project sonar.projectVersion=1.0sonar.language=objc # Project description sonar.projectDescription=Fake description # Path to source directories sonar.sources=srcDir1,srcDir2 # Xcode project configuration (.xcodeproj or .xcworkspace) # -> If you have a project: configure only sonar.objectivec.project # -> If you have a workspace: configure sonar.objectivec.workspace and sonar.objectivec.project # and use the later to specify which project(s) to include in the analysis (comma separated list) sonar.objectivec.project=myApplication.xcodeproj # sonar.objectivec.workspace=myApplication.xcworkspace # Scheme to build your application sonar.objectivec.appScheme=myApplication # Scheme to build and run your tests (comment following line of you don't have any tests) sonar.objectivec.testScheme=myApplicationTests ########################## # Optional configuration # ########################## # Encoding of the source code sonar.sourceEncoding=UTF-8# JUnit report generated by run-sonar.sh is stored in sonar-reports/TEST-report.xml # Change it only if you generate the file on your own # The XML files have to be prefixed by TEST- otherwise they are not processed # sonar.junit.reportsPath=sonar-reports/ # Cobertura report generated by run-sonar.sh is stored in sonar-reports/coverage.xml # Change it only if you generate the file on your own # sonar.objectivec.coverage.reportPattern=sonar-reports/coverage*.xml # OCLint report generated by run-sonar.sh is stored in sonar-reports/oclint.xml # Change it only if you generate the file on your own # sonar.objectivec.oclint.report=sonar-reports/oclint.xml # Paths to exclude from coverage report (tests, 3rd party libraries etc.) # sonar.objectivec.excludedPathsFromCoverage=pattern1,pattern2 sonar.objectivec.excludedPathsFromCoverage=.*Tests.*
6. 在bash中執行Sonar Runner
/etc/sonar-runner/bin/sonar-runer.sh
7. 在SonarQube中查看結果
http://localhost:9000/
異常狀況處理:
若是執行/etc/sonar-runner/bin/sonar-runer.sh 失敗,提示錯誤:
RROR: Error during Sonar runner execution ERROR: Unable to execute Sonar ERROR: Caused by: You must install a plugin that supports the language 'objc'ERROR: ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch. ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
說明sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar沒有加載到/etc/sonarqube/extensions/plugins
2. 若是執行/etc/sonar-runner/bin/sonar-runer.sh 失敗,提示錯誤:
ERROR: Error during Sonar runner execution ERROR: Unable to execute Sonar ERROR: Caused by: The rule 'OCLint:switch statements don't need default when fully covered' does not exist.ERROR: ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch. ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
錯誤提示 The rule ‘XXX’ does not exist說明Oclint掃描出來的問題在Sonar Plugin for Objective C(sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar)的規則定義中不存在,這時候只能把規則追加到Sonar Plugin for Objective C中,並從新編譯jar包。追加規則的方法爲:
編輯sonar-objective-c-master/src/main/resources/org/sonar/plugins/oclint下的 profile-oclint.xml和rules.txt
例如上面的錯誤,將下面的代碼加入profile-oclint.xml
<rule> <repositoryKey>OCLint</repositoryKey> <key>switch statements don't need default when fully covered</key> </rule>
將下面的代碼加入rules.txt(注意在0.3.2版本中Priority和Severity不能超過3,不然編譯出來的jar包會形成SonarQube服務器沒法啓動)
switch statements don't need default when fully covered ---------- Summary: Priority: 3Severity: 3Category: OCLint
最後須要重啓SonarQube服務器
/etc/sonarqube/bin/macosx-universal-64/sonar.sh restart
3. 若是執行/etc/sonarqube/bin/macosx-universal-64/sonar.sh console失敗,提示錯誤:
wrapper | --> Wrapper Started as Console wrapper | Launching a JVM... jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.orgjvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. jvm 1 | wrapper | <-- Wrapper Stopped
同時在/etc/sonarqube/bin/macosx-universal-64/中生成wrapper.log文件,並提示沒法找到配置文件,則由多是追擊規則後從新編譯過的sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar文件出錯,特別是rules.txt 中某一項的Priority和Severity超過了3