sonar-oclint集成-oc代码质量静态检查

Sonar Oclint集成文档

==========================

  • 参考链接
  • http://www.cnblogs.com/ishawn/p/3959521.html
  • https://github.com/octo-technology/sonar-objective-c

安装步骤:

  • 安装[Homebrew]
    执行命令:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 搭建SonarQube服务端

    下载地址:http://www.sonarqube.org/downloads/

    选择一个版本,我选择的是5.5的版本

    下载完之后再下载mysql服务器,我是用brew安装的

    在命令行输入:brew install 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;

    这样sonar数据库就创建好了

    然后配置conf/sonar.properties文件

    设置端口号:sonar.web.port=9002(默认端口为9000)

    设置数据库版本:sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

    设置数据库账号密码:

    sonar.jdbc.username=sonar

    sonar.jdbc.password=sonar

    启动sonar服务端:进入/sonarqube-5.5/bin/macosx-universal-64文件夹,
    给sonar.sh设置权限

    chmod 777 sonar.sh

    然后使用./sonar.sh console来启动服务器

    在浏览器输入http://localhost:9000/来测试服务器是否启动成功

sonar-oclint集成-oc代码质量静态检查_第1张图片
061615067503765.png
看到这个界面就表示成功了

Example iOS SonarQube dashboard

  • 安装 SonarQube Runner 使用 (HomeBrew installed and brew install sonar-runner)

    安装完的默认路径为:/usr/local/Cellar/sonar-runner

    然后配置sonar-runner.properities文件

    /usr/local/Cellar/sonar-runner/2.5/libexec/conf/sonar-runner.properties

    打开注释

    sonar.host.url=http://localhost:9002/(这个是sonar服务端的地址)

    sonar.jdbc.username=sonar(5.5不支持 ,写上也没事)
    sonar.jdbc.password=sonar(5.5不支持 ,写上也没事)

    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8(这个是数据库的地址)

  • 安装 xctool (HomeBrew installed and brew install xctool).

  • 安装OCLint (HomeBrew installed and brew install https://gist.githubusercontent.com/TonyAnhTran/e1522b93853c5a456b74/raw/157549c7a77261e906fb88bc5606afd8bd727a73/oclint.rb).
    可以去官网下载 也可以通过brew install oclint来下载

  • 安装 gcovr brew install gcovr

  • 安装objective-c的sonar插件放入/sonarqube-5.5/extensions/plugins目录下

  • 下载插件 the plugin

  • 拷贝 run-sonar.sh 放入要扫描的文件夹

  • 重启sonar服务器.(使用./sonar.sh restart来重启)

  • 下载扫描项目的配置文件 sonar-project.properties in your Xcode project root folder (along your .xcodeproj file)
    编辑配置文件:

    sonar.projectKey=GyyxGuangYuTong(写上你的项目名)

    sonar.projectName=GyyxGuangYuTong(写你的项目名)

    sonar.projectVersion=1.0(版本号 随意)

    sonar.language=objectivec(扫描语言)

    sonar.projectDescription=GyyxGuangYuTong for sonar-objective-c(描述文字 随意)
    sonar.sources=GyyxGuangYuTong(项目文件夹的名字)

    sonar.objectivec.project= GyyxGuangYuTong.xcodeproj(工程名后缀必须有)

    sonar.objectivec.appScheme=GyyxGuangYuTong(这里写的就是appScheme得名字)

    sonar.objectivec.coverage.reportPattern=sonar-reports/coverage*.xml(打开即可。。)

    剩下的文件可以酌情处理,我就配置了这几个
    有可能报错需要配置的
    sonar.objectivec.oclint.report=oclint.xml(这个是报告的名字)

    sonar.objectivec.oclint.reportPath=sonar-reports/oclint.xml(这个是报告的路径)

  • 这些都配置好之后 在工程目录下执行./run-sonar.sh文件
    出现这样的 就表示扫描成功了


    Example iOS SonarQube dashboard

  • 这是网站最后的截图


    Example iOS SonarQube dashboard

中途可能遇到的问题

  • 扫描成功后最后查看网站,代码重复率有数字,其他代码质量为0
    解决办法就是查看project:sonar-project.properties文件,配置这2句 可以尝试。。我自己在4.7的时候配置这条数据会报错,不过是与规则相关的。

    sonar.objectivec.oclint.report=oclint.xml(这个是报告的名字)

    sonar.objectivec.oclint.reportPath=sonar-reports/oclint.xml(这个是报告的路径)

  • 在执行./run-sonar.sh文件的时候,遇到

    ERROR: Caused by: The rule 'OCLint:switch statements don't need default when fully covered' does not exist.
    这种错误,一般都是没找到规则。这时候只能把规则追加到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

        
            OCLint
            switch statements don't need default when fully covered
        
    

    将下面的代码加入rules.txt(注意在0.3.2版本中Priority和Severity不能超过3,否则编译出来的jar包会造成SonarQube服务器无法启动)

        switch statements don't need default when fully covered
    
    
    Summary:
    
    Priority: 3
    Severity: 3
    Category: OCLint
    

    最后需要重启SonarQube服务器

    使用./sonar.sh restart来重启

  • 如果执行/etc/sonarqube/bin/macosx-universal-64/sonar.sh console失败,提示错误:
    需要查看日志来确定问题 我遇到的都是数据库问题。。。。

你可能感兴趣的:(sonar-oclint集成-oc代码质量静态检查)