Oclint is a static scanning and analysis tool for C, C ++, and Objective C code, while sonarqube is an open-source code quality management platform. This article imports oclint scan results to sonarqube, and manages the quality of Objective C code.
Operating System:
Mac OS X 10.9
Required tools:
- Sonarqube: sonarqube-4.4-http://www.sonarqube.org/downloads/
- Sonar Runner: sonar-runner-dist-2.4-http://www.sonarqube.org/downloads/
- 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.htmlselect Mac OS X or Darwin package
- Xcodebuild: xcode 5.x- https://developer.apple.com/xcode/downloads/
Required components:
Sonar plugin for Objective C
You can download sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar directly
You can also download the source code in the https://github.com/octo-technology/sonar-objective-c/tree/oclint and execute the build-and-deploy.sh Compilation
Environment setup:
- Download and install MySQL;
- Create sonar databases and users;
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. Download and decompress sonarqube (for example, "/etc/sonarqube ")
4. Place the sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar In the extensions directory of sonarqube (for example: "/etc/sonarqube/extensions/plugins ")
5. Configure sonar. properties (for example, "/etc/sonarqube/conf ")
# 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 MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
6. Start the sonarqube Server
$ /etc/sonarqube/bin/macosx-universal-64/sonar.sh consoleRunning SonarQube...wrapper | --> Wrapper Started as Consolewrapper | 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. Test sonarqube
Http: // localhost: 9000/
8. Download and decompress sonar runner (for example, "/etc/sonar-runner ")
9. Configure the sonar-runner.properties under sonar runner (for example: "/etc/sonar-runner/CONF/sonar-runner.properties ")
#----- Default SonarQube serversonar.host.url=http://localhost:9000#----- MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8#----- Global database settingssonar.jdbc.username=sonarsonar.jdbc.password=sonar
10. Download and decompress oclint (for example, "/etc/oclint ")
11. Test oclint
$ /etc/oclint/bin/oclintoclint: Not enough positional command line arguments specified!Must specify at least 1 positional arguments: See: oclint -help
12. Test xcodebuild
$ xcodebuild -versionXcode 5.0.2Build version 5A3005
Scan code:
- In bash, enter the code directory (. xcodeproj file directory) and run xcodebuild | tee xcodebuild. log.
- Execute oclint-xcodebuild. Log in bash
- Execute oclint-JSON-compilation-database ---Report-type pMD-O sonar-Reports/oclint. XML in bash.
- Store the sonar-project.properties in the Code directory and edit the corresponding items as needed. Pay special attention to the sonar. objectivec. Project and sonar. objectivec. appscheme
########################### Required configuration ###########################sonar.projectKey=my-projectsonar.projectName=My projectsonar.projectVersion=1.0sonar.language=objc # Project descriptionsonar.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 applicationsonar.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 codesonar.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,pattern2sonar.objectivec.excludedPathsFromCoverage=.*Tests.*
6. Execute sonar runner in bash
/etc/sonar-runner/bin/sonar-runer.sh
7. view the results in sonarqube
Http: // localhost: 9000/
Exception Handling:
- If the execution of/etc/sonar-runner/bin/sonar-runer.sh fails, an error is prompted:
RROR: Error during Sonar runner executionERROR: Unable to execute SonarERROR: 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.
Description sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar not loaded to/etc/sonarqube/extensions/plugins
2. If the execution of/etc/sonar-runner/bin/sonar-runer.sh fails, an error is prompted:
ERROR: Error during Sonar runner executionERROR: Unable to execute SonarERROR: 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 error message the rule 'xxx' does not exist indicates that the problem that oclint scans does not exist in the Rule definition of the sonar plugin for Objective C (sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar, at this time, you can only append the rule to sonar plugin for Objective C and re-compile the jar package. The method for appending rules is:
Edit profile-oclint.xmland rules.txt in sonar-objective-C-master/src/main/resources/org/Sonar/plugins/oclint
For example, the above error, add the following code to the profile-oclint.xml
<rule> <repositoryKey>OCLint</repositoryKey> <key>switch statements don‘t need default when fully covered</key> </rule>
Add the following code to rules.txt (note that in version 0.3.2, priority and severity cannot exceed 3; otherwise, the compiled jar package will cause the sonarqube server to fail to start)
switch statements don‘t need default when fully covered----------Summary:Priority: 3Severity: 3Category: OCLint
Restart the sonarqube server.
/etc/sonarqube/bin/macosx-universal-64/sonar.sh restart
3. If the/etc/sonarqube/bin/macosx-universal-64/sonar. Sh console fails, an error is prompted:
wrapper | --> Wrapper Started as Consolewrapper | 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
At the same time, the priority and severity of one of the/etc/sonarqube/bin/macosx-universal-64/conditions exceeds 3
Objective C static code scanning and code Quality Management oclint + sonarqube