ios项目
我提供了一个sample的项目,首先git clone该项目到本地。

git clone https://github.com/DoctorQ/ios_test.git
Cloning into ‘ios_test’…
remote: Counting objects: 88, done.
remote: Total 88 (delta 0), reused 0 (delta 0), pack-reused 87
Unpacking objects: 100% (88/88), done.
Checking connectivity… done.

安装OCLint
因为sonar-objective-c使用的是OCLint来扫描代码的,所以需要安装OCLint,使用下面的命令来安装 (前提是你有brew工具)。

brew install https://gist.githubusercontent.com/TonyAnhTran/e1522b93853c5a456b74/raw/157549c7a77261e906fb88bc5606afd8bd727a73/ocli nt.rb

安装sonar
看我另外两篇关于sonar的安装使用。

sonarQube
sonarRunner
其他工具安装
安装XCtool
详细安装方法参考官网

brew install xctool

安装gcovr
详细安装方法参考官网

brew install gcovr

xcode安装
直接app store上安装xcode*新版

添加sonar-objective-c文件
文件添加
如果想要分析代码,需要在待分析项目根目录下添加2个文件:

run-sonar.sh
sonar-project.properties
文件修改
sonar-project.properties文件中修改一些配置信息。详细的配置说明我已经写在各个脚本的注释上了:

##########################
# Required configuration #
##########################
#sonar服务器上的访问地址后缀,比如我们可以设置下面的属性为ios,就可以通过http://server:port/dashboard/index/ios来访问该项目的数据
sonar.projectKey=ios
#项目的名称
sonar.projectName=ios
#版本号
sonar.projectVersion=1.0
sonar.language=objc

# Project description
sonar.projectDescription=test for sonar-objective-c

# 要检测的源码的目录
sonar.sources=0403_test
# 要检测的测试源码目录,如果没有注释掉即可
# sonar.tests=testSrcDir

# 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)
#项目根目录下的xcodeproj的名称
sonar.objectivec.project=0403_test.xcodeproj
# sonar.objectivec.workspace=myApplication.xcworkspace

# 应用的scheme信息,如果不知道没关系,先执行该脚本,它会提示你当前项目的scheme信息
sonar.objectivec.appScheme=0403_test
# 同上,只是用于测试,如果没有就直接注释掉
# 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.*

# Project SCM settings
# sonar.scm.enabled=true
# sonar.scm.url=scm:git:https://…

执行sonar检测
命令行定位到项目根目录下,执行sh run-sonar.sh命令

sh run-sonar.sh
Running run-sonar.sh…
-n Extracting Xcode project information
-n .

-n .

Skipping tests as no test scheme has been provided!
-n Running OCLint…

-n Running SonarQube using SonarQube Runner
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .

执行成功后,查看结果如下:

%title插图%num

DONE!