Oclint is a static scanning analysis tool for C, C + + and objective C code, while Sonarqube is an open source code quality management platform. This article implements the import of Oclint scan results into Sonarqube, which has been implemented to manage Objective C code quality.
Operating system:
Mac OS X 10.9
The tools you need:
- 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.html Choose Mac OS X or Darwin's package
- Xcodebuild:xcode 5.x-https://developer.apple.com/xcode/downloads/
Required Components:
Sonar Plugin for Objective C
Can download Sonar-objective-c-plugin-0.3.2-snapshot.jar directly
You can also download the source code in Https://github.com/octo-technology/sonar-objective-c/tree/oclint, and perform the build-and-deploy.sh compilation.
Environment Construction:
- Download and install MySQL;
- Create sonar database and users;
CREATE DATABASE sonar CHARACTER SET UTF8 COLLATE utf8_general_ci; CREATE USER ' sonar ' identified by ' sonar '; GRANT all on sonar.* to ' sonar ' @ '% ' of ' identified by ' sonar '; GRANT all on sonar.* to ' sonar ' @ ' localhost ' identified by ' sonar '; FLUSH privileges;
3. Download and unzip the Sonarqube (for example: "/etc/sonarqube")
4. Place the Sonar-objective-c-plugin-0.3.2-snapshot.jar in the Sonarqube extension directory (for example: "/etc/sonarqube/extensions/plugins")
5. Configure the Sonar.properties (for example: "/etc/sonarqube/conf")
1234567 |
# 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 MySQL sonar.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 unzip the sonar Runner (for example: "/etc/sonar-runner")
9. Configure 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 unzip the 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:
- Enter the code directory in bash (the directory where the. xcodeproj file is located), execute Xcodebuild | Tee Xcodebuild.log
- Execute Oclint-xcodebuild xcodebuild.log in bash
- Perform oclint-json-compilation-database---report-type pmd-o sonar-reports/oclint.xml in bash.
- Store the sonar-project.properties in the Code directory, edit the corresponding items according to the situation, and 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) #-a project:configure only sonar.objectivec.project# If you had a workspace:configure sonar.objectivec.workspace and sonar.objectivec.project# and use the later to SPE Cify 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 has any tests) sonar.objectivec.tes Tscheme=myapplicationtests ########################### Optional Configuration ############################ Encoding of the source codesonar.sourceencoding=utf-8# JUnit report generated by run-sonar.sh are stored in sonar-reports/test-report.xml# change It is only if you generate the file on your own# the XML files has to is prefixed by T Est-otherwise They is not processed # sonar.junit.reportspath=sonar-reports/# Cobertura report generated by run-sonar.sh is stored in sonar-reports/coverage.xml# it's only if you generate the file on your own# SONAR.OBJECTIVEC.COVERAGE.R eportpattern=sonar-reports/coverage*.xml# oclint Report generated by run-sonar.sh are stored in Sonar-reports/oclint.xml # change it generate the file on your own# sonar.objectivec.oclint.report=sonar-reports/oclint.xml# Paths to E Xclude from coverage report (tests, 3rd party libraries etc.) # sonar.objectivec.excludedpathsfromcoverage=pattern1,pattern2sonar.objectivec.excludedpathsfromcoverage=.* tests.*
6. Perform sonar Runner in bash
/etc/sonar-runner/bin/sonar-runer.sh
7. View the results in Sonarqube
Http://localhost:9000/
Exception handling:
- If execution/etc/sonar-runner/bin/sonar-runer.sh fails, an error is indicated:
Rror:error during Sonar runner executionerror:unable to execute sonarerror:caused by:you must install a plugin that Su Pports the language ' OBJC ' ERROR:ERROR:To see the full stack trace of the errors, re-run SonarQube Runner with The-e SWI Tch. 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 into/etc/sonarqube/extensions/plugins
2. If execution/etc/sonar-runner/bin/sonar-runer.sh fails, prompt error:
Error:error during Sonar runner executionerror:unable to execute sonarerror:caused by:the rule ' Oclint:switch statemen TS 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.
Error message the rule ' XXX ' does not exist description Oclint scan out the issue in Sonar Plugin for Objective C ( Sonar-objective-c-plugin-0.3.2-snapshot.jar) does not exist in the rule definition, you can only append the rule to sonar plugin for Objective C and recompile the jar package. To append a rule, the method is:
Edit Profile-oclint.xml and Rules.txt under Sonar-objective-c-master/src/main/resources/org/sonar/plugins/oclint
For example the above error, add the following code to 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 priority and severity cannot exceed 3 in the 0.3.2 version, otherwise the compiled jar will cause the Sonarqube server to fail to start)
Switch statements don ' t need default when fully covered----------Summary:priority:3severity:3category:oclint
Finally, you need to restart the Sonarqube server
/etc/sonarqube/bin/macosx-universal-64/sonar.sh restart
3. If the execution of/etc/sonarqube/bin/macosx-universal-64/sonar.sh console fails, an error is indicated:
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 in/etc/sonarqube/bin/macosx-universal-64/ The Wrapper.log file is generated and the configuration file cannot be found, there is an error in the Sonar-objective-c-plugin-0.3.2-snapshot.jar file that may have been recompiled after the rule was chased, especially rules.txt The priority and severity of one of the items exceeds 3
Objective c static Code scanning and code Quality Management Oclint + SonarQube