一、安裝工具
1、Java環(huán)境與MySQL安裝
1.1 進(jìn)入jdk1.8安裝界面,下載macos版本
1.2 進(jìn)入MySQL 下載界面难述,下載之后安裝踩身,安裝完成后胀茵,進(jìn)入系統(tǒng)偏好設(shè)置,可看到MySql選項(xiàng)挟阻,代表已安裝成功
2琼娘、SonarQube安裝
下載地址
下載完成解壓之后直接把文件夾放到 /usr/local/ 路徑下
3、其它必備插件的安裝
安裝OCLint
OCLint是Sonar掃描代碼的主要工具附鸽,所以必須安裝脱拼。若電腦中已安裝Homebrew,可打開終端使用下面的命令安裝
brew tap oclint/formulae
brew install oclint
安裝xctool
xctool是用于編譯構(gòu)建和測(cè)試iOS坷备、Mac項(xiàng)目的工具熄浓,打開終端使用下面的命令安裝
brew install xctool
安裝gcovr
gcovr是一個(gè)將單元測(cè)試中的代碼覆蓋率以多種方式(包括列表方式、XML文件方式击你、HTML網(wǎng)頁方式等)展示出來的工具玉组,打開終端使用下面的命令安裝
brew install gcovr
xcpretty安裝
用于對(duì)xcodebuild的輸出進(jìn)行格式化。并包含輸出report功能丁侄。
github 鏈接
如果按照如下方法進(jìn)行安裝惯雳,一般會(huì)報(bào)錯(cuò),
$ gem install xcpretty
You don’t have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
因?yàn)檫@時(shí)你使用的是 Apple 家自帶的 ruby鸿摇,權(quán)限不夠石景,使用以下命令即可安裝完成,
$ sudo gem install xcpretty -n/usr/local/bin
這樣就成功了拙吉!
安裝Sonar-Scanner
Sonar-Scanner是Sonar中真正用于實(shí)現(xiàn)代碼掃描的工具,進(jìn)入 [Sonar-Scanner下載頁面](https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner)潮孽,如下圖所示
下載完成同樣需要解壓到/usr/local/文件夾下
二、環(huán)境變量與數(shù)據(jù)庫的配置
配置環(huán)境變量
打開終端筷黔,創(chuàng)建.bash_profile配置文件(已有文件可跳過此步驟)
touch ~/.bash_profile
編輯.bash_profile配置文件
vim ~/.bash_profile
下列代碼為我的配置往史,可直接復(fù)制到.bash_profile中使用,復(fù)制時(shí)注意修改安裝路徑與工具版本號(hào)為自己電腦上的情況
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export OCLINT_HOME=/usr/local/oclint-0.13
export PATH=$OCLINT_HOME/bin:$PATH
編輯完成后保存佛舱,并執(zhí)行下面的命令
source ~/.bash_profile
MySql數(shù)據(jù)庫配置
- 創(chuàng)建sonar數(shù)據(jù)庫和用戶
打開終端椎例,輸入 mysql -u root -p 挨决,再輸入數(shù)據(jù)庫密碼,進(jìn)入MySql订歪,輸入下面sql語句創(chuàng)建sonar所需的數(shù)據(jù)庫脖祈。不知道數(shù)據(jù)庫初始密碼的可參考這篇文章
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;
三、配置SonarQube
配置SonarQube須在Java環(huán)境與MySql數(shù)據(jù)庫都已創(chuàng)建配置好的情況下進(jìn)行
配置sonar.properties
打開下面路徑的文件(sonar版本不同刷晋,路徑也會(huì)不同)
/usr/local/sonarqube-6.7.5/conf/sonar.properties
修改參數(shù)
sonar.jdbc.username=你的數(shù)據(jù)庫賬號(hào)(我的是root)
sonar.jdbc.password=你的數(shù)據(jù)庫密碼 (123)
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.maxActive=60
sonar.jdbc.maxIdle=5
sonar.jdbc.minIdle=2
sonar.jdbc.maxWait=5000
sonar.jdbc.minEvictableIdleTimeMillis=600000
sonar.jdbc.timeBetweenEvictionRunsMillis=30000
修改完成保存后盖高,在終端中啟動(dòng)sonar
sudo /usr/local//sonarqube-6.7.5/bin/macosx-universal-64/sonar.sh start
完成啟動(dòng)后,打開瀏覽器訪問http://localhost:9000
若能看到下圖頁面則證明配置成功
安裝中文插件
SonarQube中的應(yīng)用市場(chǎng)中是有免費(fèi)的中文插件的眼虱,在配置成功后可以去下載喻奥,下載方式如下圖所示,圖中是已經(jīng)完成插件安裝的情況安裝sonar-objective-c-plugin
由于SonarQube官方插件中支持OC語言的插件非常昂貴蒙幻,所以我這里使用的是來自GitHub上的一個(gè)開源插件映凳,將jar包復(fù)制到下面的路徑中,重啟sonar即安裝成功
/usr/local/sonarqube-6.7.5/extensions/plugins
四邮破、本地項(xiàng)目中的配置
將文件 run-sonar.sh 與 sonar-project.properties 拷貝到待分析的項(xiàng)目目錄下诈豌,如下圖所示
配置sonar-project.properties文件
sonar.projectKey=你的項(xiàng)目名
sonar.projectName=你的項(xiàng)目名
sonar.projectVersion=顯示在sonar中的項(xiàng)目版本號(hào)
sonar.sources=代碼存放的文件夾
sonar.objectivec.project=項(xiàng)目名.xcodeproj
sonar.objectivec.workspace=項(xiàng)目名.xcworkspace
sonar.objectivec.appScheme=項(xiàng)目名
到這里本地項(xiàng)目中的配置就完成了。
五抒和、開始使用
在終端中使用cd命令進(jìn)入項(xiàng)目所在目錄矫渔,再執(zhí)行run-sonar.sh
cd /Users/chuqingqin/Desktop/項(xiàng)目文件夾
./run-sonar.sh
oc插件安裝,下載地址自找
分析完成后庙洼,打開瀏覽器訪問http://localhost:9000,可看到項(xiàng)目分析報(bào)告
六镊辕、踩過的坑
1油够、不要使用Mysql8.0版本,因?yàn)槠渲械呐渲冒l(fā)生改變征懈,與SonarQube會(huì)有沖突石咬,導(dǎo)致無法啟動(dòng)SonarQube。
2卖哎、如果要更新sonar,請(qǐng)同時(shí)看看sonar-objective-c-plugin是否需要更新鬼悠,如果該插件作者未更新,則最好不要更新sonar亏娜,因?yàn)榭赡軐?dǎo)致OC插件不兼容焕窝。 插件下載地址
https://github.com/culeo/sonar-objective-c
作者的開源代碼中未提供jar包,需要下載下來后自行安裝maven環(huán)境運(yùn)行 build-and-deploy.sh 文件维贺,生成jar包它掂。
附件(run-sonar.sh)
#!/bin/bash
## INSTALLATION: script to copy in your Xcode project in the same directory as the .xcodeproj file
## USAGE: ./run-sonar.sh
## DEBUG: ./run-sonar.sh -v
## WARNING: edit your project parameters in sonar-project.properties rather than modifying this script
#
trap "echo 'Script interrupted by Ctrl+C'; stopProgress; exit 1" SIGHUP SIGINT SIGTERM
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
}
function readParameter() {
variable=$1
shift
parameter=$1
shift
eval $variable="\"$(sed '/^\#/d' sonar-project.properties | grep $parameter | tail -n 1 | cut -d '=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\""
}
# Run a set of commands with logging and error handling
function runCommand() {
# 1st arg: redirect stdout
# 2nd arg: command to run
# 3rd..nth arg: args
redirect=$1
shift
command=$1
shift
if [ "$nflag" = "on" ]; then
# don't execute command, just echo it
echo
if [ "$redirect" = "/dev/stdout" ]; then
if [ "$vflag" = "on" ]; then
echo "+" $command "$@"
else
echo "+" $command "$@" "> /dev/null"
fi
elif [ "$redirect" != "no" ]; then
echo "+" $command "$@" "> $redirect"
else
echo "+" $command "$@"
fi
elif [ "$vflag" = "on" ]; then
echo
if [ "$redirect" = "/dev/stdout" ]; then
set -x #echo on
$command "$@"
returnValue=$?
set +x #echo off
elif [ "$redirect" != "no" ]; then
set -x #echo on
$command "$@" > $redirect
returnValue=$?
set +x #echo off
else
set -x #echo on
$command "$@"
returnValue=$?
set +x #echo off
fi
if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
stopProgress
echo "ERROR - Command '$command $@' failed with error code: $returnValue"
exit $returnValue
fi
else
echo "--------------------------------"
echo $command
echo "$@"
if [ "$redirect" = "/dev/stdout" ]; then
$command "$@" > /dev/null
elif [ "$redirect" != "no" ]; then
$command "$@" > $redirect
else
$command "$@"
fi
returnValue=$?
if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
stopProgress
echo "ERROR - Command '$command $@' failed with error code: $returnValue"
exit $?
fi
echo
fi
}
## COMMAND LINE OPTIONS
vflag=""
nflag=""
oclint="on"
while [ $# -gt 0 ]
do
case "$1" in
-v) vflag=on;;
-n) nflag=on;;
-nooclint) oclint="";;
--) shift; break;;
-*)
echo >&2 "Usage: $0 [-v]"
exit 1;;
*) break;; # terminate while loop
esac
shift
done
# Usage OK
echo "Running run-sonar.sh..."
# 檢查依賴是否已經(jīng)安裝 xcpretty and oclint
testIsInstalled xcpretty
testIsInstalled oclint
# 檢查有沒有 sonar-project.properties 文件
if [ ! -f sonar-project.properties ]; then
echo >&2 "ERROR - No sonar-project.properties in current directory"; exit 1;
fi
# 從 sonar-project.properties 讀出參數(shù)
# .xcworkspace/.xcodeproj filename
workspaceFile=''; readParameter workspaceFile 'sonar.objectivec.workspace'
projectFile=''; readParameter projectFile 'sonar.objectivec.project'
# 源文件
srcDirs=''; readParameter srcDirs 'sonar.sources'
# Scheme
appScheme=''; readParameter appScheme 'sonar.objectivec.appScheme'
# test
testScheme=''; readParameter testScheme 'sonar.objectivec.testScheme'
if [ "$vflag" = "on" ]; then
echo "Xcode workspace file is: $workspaceFile"
echo "Xcode project file is: $projectFile"
echo "Xcode application scheme is: $appScheme"
echo "Xcode test scheme is: $testScheme"
fi
# 檢查必須參數(shù)
if [ -z "$projectFile" -o "$projectFile" = " " ]; then
if [ ! -z "$workspaceFile" -a "$workspaceFile" != " " ]; then
echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties. You must specify which projects (comma-separated list) are application code within the workspace $workspaceFile."
else
echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties (name of your .xcodeproj)"
fi
exit 1
fi
if [ -z "$srcDirs" -o "$srcDirs" = " " ]; then
echo >&2 "ERROR - sonar.sources parameter is missing in sonar-project.properties. You must specify which directories contain your .h/.m source files (comma-separated list)."
exit 1
fi
if [ -z "$appScheme" -o "$appScheme" = " " ]; then
echo >&2 "ERROR - sonar.objectivec.appScheme parameter is missing in sonar-project.properties. You must specify which scheme is used to build your application."
exit 1
fi
## SCRIPT
# Create sonar-reports/ for reports output
if [[ ! (-d "sonar-reports") && ("$nflag" != "on") ]]; then
if [ "$vflag" = "on" ]; then
echo 'Creating directory sonar-reports/'
fi
mkdir sonar-reports
if [[ $? != 0 ]] ; then
stopProgress
exit $?
fi
fi
# Extracting project information needed later
echo 'Extracting Xcode project information'
xcodebuild clean
export LC_ALL="en_US.UTF-8"
if [[ "$workspaceFile" != "" ]] ; then
echo "xcodebuild clean"
xcodebuild clean -workspace "${workspaceFile}" -scheme "${appScheme}" -sdk iphonesimulator -configuration Release
echo "xcodebuild analyze"
xcodebuild -workspace "${workspaceFile}" -scheme "${appScheme}" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' ONLY_ACTIVE_ARCH=NO -configuration build | tee xcodebuild.log | xcpretty -r json-compilation-database --output compile_commands.json
else
echo "xcodebuild clean"
xcodebuild clean -project "${projectFile}" -scheme "${appScheme}" -configuration Release
echo "xcodebuild analyze"
xcodebuild -project "${projectFile}" -scheme "${appScheme}" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' ONLY_ACTIVE_ARCH=NO -configuration build | tee xcodebuild.log | xcpretty -r json-compilation-database --output compile_commands.json
fi
if [ "$oclint" = "on" ]; then
# OCLint
echo -n 'Running OCLint...'
# Build the --include flags
currentDirectory=${PWD##*/}
includedCommandLineFlags=""
echo "$srcDirs" | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
while read word; do
includedCommandLineFlags+=" --include .*/${currentDirectory}/${word}"
done < tmpFileRunSonarSh
rm -rf tmpFileRunSonarSh
if [ "$vflag" = "on" ]; then
echo
echo -n "Path included in oclint analysis is:$includedCommandLineFlags"
fi
# Run OCLint with the right set of compiler options
maxPriority=10000
oclint-json-compilation-database -- -max-priority-1 $maxPriority -max-priority-2 $maxPriority -max-priority-3 $maxPriority -rc LONG_LINE=150 -report-type pmd -o sonar-reports/oclint.xml
else
echo 'Skipping OCLint (test purposes only!)'
fi
# SonarQube
echo 'Running SonarQube using SonarQube Scanner'
sonar-scanner
rm -rf sonar-reports
rm -rf build
rm -rf compile_commands.json
rm -rf xcodebuild.log
rm -rf .scannerwork
exit 0