Target audience: Beginner
Estimated reading time: 4'
Introduction
It is fair to say that setting up and maintaining a source code quality management is not on the top of priorities list of developers and managers alike. Despite all the good intentions, costs and/or time to market relegate the tasks of setting up a code analyzer for instances and enforcing coding best practices to the back burner. Unfortunately, those tools have gotten the reputation to be difficult to maintain and costly to license. This is not the case anymore.
Sonar is an open source Platform used by development teams to manage
source code quality. The main purpose of the project is to simplify code quality management.
As such, Sonar supports analysis of Java in the core,
but also up to 10 different programming languages through plug-ins. The automation process associated to code quality management can be broken down along two distinct objectives:
- Continuous integration: Build automation, static code analysis & code coverage reports
- Continuous reviews: Best practices violations, code improvement and refactoring, stability and maintainability
Readability of source code is quite often evaluated using Fresch-Kincaid test that was originally developed for measuring the readability of academic English. The method scores the "complexity" of any document from 100 (11th grade student) to 0 domain-experts and scholars, with 50 for articles in "Time Magazine".
Dashboard
|
Best practices violation
One of a "side" benefit of any static code analyzer is to force the engineering to define and maintain a set of best practices. Sonar uses a severity ranking similar to most common defect database of classify violation of coding standards. The following table display the severity and type of violation of best practice rule.
By default, the current version of Sonar, 3.0, contains 600 coding off-the-shelf rules. The user can create custom rules or override existing ones using XPath expressions.
Inner Assignment
checkstyle : com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck
Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);
Avoid Throwing Raw Exception Types
Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead.
checkstyle : com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck
Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);
Avoid Throwing Raw Exception Types
Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead.
Sonar allows the developers to take a snapshot of the overall quality of an application and view the evolution of quality measures across snapshots with the TimeMachine service. But this was not sufficient to provide at quick answers to the fundamental question: What changed over the past X days?
Recently, Sonar added the differential dashboard that allows developers to visualize the difference between two quality snapshots. Those quality snapshots can be assigned a version, and purged according to a
configurable policy.
Getting started
Sonar is made of 3 components:- Database that stores the configuration and results of quality analyses
- Web Server that is used to navigate the results of the analyzes and make configuration
- Client that runs source code analyzers to compute data on projects
Installation of Sonar
Here are the set-up to install Sonar1. Download the latest version of Sonar from Sonar Downloads
2. Unzip the installation package
3. In the directory sonar-xx/conf open and edit the Sonar property file
4. Override the credentials
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar
5. By default, Sonar is bundled with Apache Derby databasesonar.jdbc.password: sonar
sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
6. If you want to use your own database you need to create the database and
relevant tables
7. Then specify the JDBC drivers URL and name of your database
7. Then specify the JDBC drivers URL and name of your database
sonar.jdbc.url: jdbc:oracle:thin:@localhost:1521/instance-name
sonar.jdbc.driverClassName: oracle.jdbc.driver.OracleDriver
or
sonar.jdbc.url:jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 sonar.jdbc.driverClassName:com.mysql.jdbc.Driver
sonar.jdbc.driverClassName: oracle.jdbc.driver.OracleDriver
or
sonar.jdbc.url:jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 sonar.jdbc.driverClassName:com.mysql.jdbc.Driver
Installation Eclipse plug-in
Assuming Eclipse version 3.7 or later is installed...- Go to Help > Install New Software... This should display the Install dialog box.
- Paste the Update Site URL (http://dist.sonar-ide.codehaus.org/eclipse/) into the field Work with and press Enter. This should display the list of available plugins and components:
- Check the component you wish to install.
- Click Next. Eclipse will then check to see if there is any issue which would prevent a successful installation
- Click Finish to begin the installation process. Eclipse will then download and install the necessary components.
- Once the installation process is finished, Eclipse will ask if you want to restart the IDE. It is strongly recommended that you restart the IDE.
Integration with Jenkins
Assuming that Jenkins continuous integration server is already installed, you need to- login into the Jenkins installation 's management screen
- click on Manage Plugins menu
- select the Available tab. If the list of plug-ins is empty, then select Advanced tab and force to check for new updates by clicking Check now.
- select the check box corresponding to the Sonar plug-in for a particular language
- select the Install without restart option. The installation will complete
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.