文檔更新說(shuō)明
- 2017-09-17 v1.0 初稿
- 2017-09-28 v1.1 增加單元測(cè)試相關(guān)內(nèi)容
- 2019-01-02 fix error
項(xiàng)目在研發(fā)過(guò)程中需要時(shí)刻關(guān)注代碼質(zhì)量酿愧,我們通常會(huì)使用 OCLint 工具來(lái)生成靜態(tài)代碼分析報(bào)告昌犹,使用 Slather 生成單元測(cè)試覆蓋率報(bào)告嫡锌,但這些報(bào)告相對(duì)來(lái)說(shuō)都太過(guò)簡(jiǎn)陋,而 Sonar Qube 以一種可視化的方法來(lái)展示代碼中存在的問(wèn)題,本文將介紹如何使用 Sonar Qube 展示 OCLint 掃描后的報(bào)告。
安裝軟件
這里以 Ubuntu 16.04 為例介紹如何安裝 SonarQube,SonarQube 運(yùn)行依賴幾個(gè)東西:JDK 和 MySQL瓶颠,所以在安裝 SonarQube 之前需要先安裝這兩個(gè)東西。
安裝 JDK 和 MySQL
安裝 JDK
- sudo add-apt-repository ppa:webupd8team/java
- sudo apt-get update
- sudo apt-get install oracle-java8-installer
執(zhí)行上述命令后刺桃,在終端里面輸入 java -version
粹淋,如能正常顯示 JDK 版本則說(shuō)明安裝成功。
安裝 MySQL
- sudo apt-get update
- sudo apt-get install mysql-server
- sudo mysql_secure_installation
安裝完畢后瑟慈,在終端中輸入 systemctl status mysql.service
桃移,如能有如下輸出則說(shuō)明安裝成功:
安裝 SonarQube
解壓安裝包至安裝目錄
安裝包下載地址:https://www.sonarqube.org/downloads/
創(chuàng)建數(shù)據(jù)庫(kù)及相應(yīng)用戶
新建數(shù)據(jù)庫(kù)
- CREATE DATABASE sonar;
創(chuàng)建用戶并賦予權(quán)限
- 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;
配置數(shù)據(jù)庫(kù)訪問(wèn)參數(shù)
編輯 <install_directory>/conf/sonar.properties,根據(jù)使用的數(shù)據(jù)庫(kù)類型配置相應(yīng)的參數(shù)葛碧,這里選用了 MySQL 數(shù)據(jù)庫(kù)借杰,因而需要配置其用戶名、密碼以及 jdbc url进泼。
配置 Web Server
同樣編輯 <install_directory>/conf/sonar.properties蔗衡,設(shè)置 Web Server host 主機(jī)的 ip、端口以及訪問(wèn)的上下文乳绕。
sonar.web.host=192.0.0.1
sonar.web.port=80
sonar.web.context=/sonar
上述配置完成后即可啟動(dòng) SonarQube:
- On Linux/Mac OS:
bin/<YOUR OS>/sonar.sh start
- On Windows:
bin/windows-x86-XX/StartSonar.bat
在實(shí)際使用中會(huì)出現(xiàn)MySQL 數(shù)據(jù)庫(kù)無(wú)法訪問(wèn)的情況绞惦,通常有如下幾種問(wèn)題:
1.用戶沒(méi)有權(quán)限,加權(quán)限即可
mysql>GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'%' IDENTIFIED BY 'sonar' WITH GRANT OPTION;
2.默認(rèn)3306端口只允許本地訪問(wèn)刷袍,修改 /etc/mysql/mysql.conf.d/mysqld.cnf
注掉 bind-address = 127.0.0.1
上述軟件安裝完畢后,SonarQube 就搭建完成樊展,在瀏覽器中輸入 http://192.0.0.1/sonar 即可使用SonarQube 來(lái)檢查代碼質(zhì)量呻纹。
SonarQube 默認(rèn)并不支持對(duì) Objective-C 代碼質(zhì)量掃描,官方提供的插件每年需要3000歐专缠,對(duì)于普通開(kāi)發(fā)者來(lái)說(shuō)太貴雷酪,幸好 Github 上有開(kāi)源的插件,下載好源碼后使用 Maven 編譯后將 jar 包放置 <install_directory>/extensions/plugins 目錄中涝婉,重啟 SonarQube后在系統(tǒng)控制面板中如能看到如下內(nèi)容則說(shuō)明安裝成功哥力。
編寫腳本生成 OCLint 報(bào)告并上傳至 SonarQube
軟件安裝
OCLint 所需軟件請(qǐng)翻閱 OCLint 代碼靜態(tài)分析 ,OCLint 生成的 pmd 文件需要通過(guò) sonar-scanner 上傳至 SonarQube墩弯,下載地址:https://github.com/SonarSource/sonar-scanner-cli/releases
配置
sonar-project.properties
在項(xiàng)目根目錄添加 sonar-project.properties吩跋,配置內(nèi)容如下:
##########################
# Required configuration #
##########################
sonar.projectKey=tztHuaTaiZLMobile
sonar.projectName=tztHuaTaiZLMobile
sonar.projectVersion=4.3.0
sonar.language=objc
# Project description
sonar.projectDescription=tztHuaTaiZLMobile
# Path to source directories
sonar.sources=tztHuaTaiZLMobile
# Path to test directories (comment if no test)
# sonar.tests=testSrcDir
# Destination Simulator to run tests
# As string expected in destination argument of xcodebuild command
# Example = sonar.swift.simulator=platform=iOS Simulator,name=iPhone 6,OS=9.2
sonar.objectivec.simulator=iOS Simulator,name=iPhone 7,OS=10.3.1
# 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=tztMobileApp_HTSC.xcworkspace
# Scheme to build your application
sonar.objectivec.appScheme=tztHuaTaiZLMobile
# 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=build/sonar-reports/oclint.xml
# FauxPas report generated by run-sonar.sh is stored in sonar-reports/fauxpas.json
# Change it only if you generate the file on your own
# sonar.objectivec.fauxpas.report=sonar-reports/fauxpas.json
# Paths to exclude from coverage report (tests, 3rd party libraries etc.)
# sonar.objectivec.excludedPathsFromCoverage=pattern1,pattern2
sonar.objectivec.excludedPathsFromCoverage=.*Tests.*,.*Specs.*
# Project SCM settings
# sonar.scm.enabled=true
# sonar.scm.url=scm:git:https://...
編寫腳本生成 OCLint 報(bào)告并上傳 SonarQube
在項(xiàng)目根目錄添加 scripts 目錄,并在該目錄下新建shell腳本渔工,腳本內(nèi)容如下:
#!/bin/sh
function testIsInstalled() {
hash $1 2>/dev/null
if [ $? -eq 1 ]; then
echo >&2 "ERROR - $1 is not installed or not in your PATH"; exit 1;
fi
}
echo "change to parent folder"
cd ..
echo "check xcodebuild, oclint installed is installed"
testIsInstalled xcodebuild
testIsInstalled xcpretty
testIsInstalled oclint
echo "xcodebuild clean"
xcodebuild clean -workspace DemoApp.xcworkspace \
-scheme tztHuaTaiZLMobile
echo "xcodebuild analyze | tee xcodebuild.log | xcpretty --report json-compilation-database"
xcodebuild -workspace DemoApp.xcworkspace \
-configuration Debug \
-scheme DemoApp analyze COMPILER_INDEX_STORE_ENABLE=NO | tee xcodebuild.log | \
xcpretty -r json-compilation-database
echo "mv compilation_db.json compile_commands.json"
mv ./build/reports/compilation_db.json ./compile_commands.json
echo "check folder existence"
if [ ! -d "build/sonar-reports" ]; then
mkdir -p build/sonar-reports
fi
echo "oclint-json-compilation-database"
oclint-json-compilation-database \
-v \
-- \
-report-type pmd -o build/sonar-reports/oclint.xml \
-max-priority-1=99999 -max-priority-2=99999 -max-priority-3=99999 \
-rc LONG_METHOD=300 \
-rc LONG_VARIABLE_NAME=50 \
-rc LONG_CLASS=3000 \
-rc NCSS_METHOD=300 \
-rc NESTED_BLOCK_DEPTH=8 \
echo "upload generated oclint report to sonar qube server"
sonar-scanner -X
echo "clean up"
rm -rf .scannerwork
rm -rf xcodebuild.log
rm -rf compile_commands.json
rm -rf build/sonar-reports/oclint.xml
結(jié)果展示
報(bào)告上傳后锌钮,SonarQube 會(huì)根據(jù)報(bào)告內(nèi)容生成可視化的界面,描述每一項(xiàng)存在的問(wèn)題引矩,如可能存在的 Bug梁丘、代碼缺陷侵浸、需要重構(gòu)的代碼塊等等。
本項(xiàng)目分析完畢后效果如下圖所示:
fix error
1.oclint: error: one compiler command contains multiple jobs:
運(yùn)行環(huán)境:MacOS 10.14.2, XCode Version 9.4, OCLint 0.13.
xcodebuild執(zhí)行命令中添加COMPILER_INDEX_STORE_ENABLE=NO
參數(shù)選項(xiàng)氛谜。
1.cd current project folder
2.xcodebuild -target Target -configuration Debug -scheme Scheme -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' CLANG_ENABLE_MODULE_DEBUGGING=NO CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ENABLE_BITCODE=NO COMPILER_INDEX_STORE_ENABLE=NO | tee xcodebuild.log | xcpretty -r json-compilation-database --output compile_commands.json
3.oclint-json-compilation-database -- -max-priority-1 '10' -max-priority-2 '2000' -max-priority-3 '5000' -report-type pmd -o pmd.xml
2.pre-commit can not execute fastlane in SourceTree
>>> source ~/.bash_profile
>>> open ~/Applications/SourceTree.app
3.CocoaPods requires your terminal to be using UTF-8 encoding and it aborts to run pod
>>> export LANG=en_US.UTF-8