Home Java/J2EE Measure Code Quality With Sonar

Measure Code Quality With Sonar

Published on June 13, 2014, by in Java/J2EE.

About the Sonar 

Sonar is an open source Platform used by development teams to manage source code quality.It also provides code analyzers, reporting tools, defects hunting modules and Time Machine as core functionality.

In terms of languages, Sonar support analysis of Java in the core, but also of Flex (Action Script 3), PHP, PL/SQL and Cobol through plugins (Open Source or commercial) as the reporting engine is language agnostic.

Sonar enables to cover quality on 7 axes

  • Duplicated code
  • Coding standards
  • Unit tests
  • Complex code
  • Potential bugs
  • Comments
  • Design and architecture


Why should we manage source code quality?

“A well-written program is a program where the cost of implementing a feature is constant throughout the program’s lifetime”


How to manage source code quality?

There are seven technical axes that should be looked at when doing source code analysis of a project and Sonar is able to support the management of all seven. In the Sonar team, we like to call them the 7 deadly sins of the developer:

  • non respect of coding standards and best practices
  • lacking comments in the source code, especially in public APIs
  • having duplicated lines of code
  • having complex component or/and a bad distribution of complexity amongst components
  • having no or low code coverage by unit tests, especially in complex part of the program
  • leaving potential bugs
  • having a spaghetti design (package cycles…)


Download Sonar:

  • Download Sonar zip file click here.


Setting up Sonar :

  • Unzip Sonar.zip file and move to specific location (I have moved sonar folder to C:\sonar)
  • In The MySQL  Create Database for sonar (I have created database named “sonar” in MySQL)
  • Update the sonar.properties file, change the default Database of sonar, list of changes are given below
    • For Updating the properties navigate to <sonar root directory>\conf\ and open sonar.properties file.
    • Set the MySQL username and password for that update give below properties ( you have to set the your MySQL username and password)
    • sonar.jdbc.username=root
    • Comment the default embedded database #sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar (putting #)
    • Set the MySQL Database for sonar, for that we need to add given below propertie in the sonar.properties file
    • sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
    • Start the sonar server
    • C:\Sonar\bin\windows-x86-64\StartSonar.bat for Windows,There are several options available for other operating system such as Linux, Solaris.
    • For accessing sonar server Browse to http://localhost:9000
    • To customize the features using the admin console, login/password is admin/admin.


Set Ant for Sonar :

  • Set the ant for sonar, for that you need to download the sonar-ant-task-1.3.jar click here to download
  • Move sonar-ant-task-1.3 jar file to  <Apache Ant Home dir>\lib\


Configure Liferay portlet for sonar :

  • Create one Sample portlet in Liferay
  • Open the build.xml file of sample portlet and add the ant target for sonar
    • Updated build.xml file is given below
    • <target name=”sonar”>
      <property name=”sonar.jdbc.url” value=”jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8″ />

      <property name=”sonar.jdbc.driverClassName” value=”com.mysql.jdbc.Driver” />

      <!the username and password of your database>
      <property name=”sonar.jdbc.username” value=”root” />
      <property name=”sonar.jdbc.password” value=”root” />

      <!list of mandatories Sonar properties>
      <property name=”sonar.sources” value=”docroot/WEB-INF/src,docroot/html” />
      <property name=”sonar.binaries” value=”docroot/WEB-INF/classes” />

      <property name=”sonar.projectKey” value=”sonar-test” />
      <sonar:sonar key=”sonar-test” version=”0.1-SNAPSHOT” xmlns:sonar=”antlib:org.sonar.ant” />

    • Open the command prompt and navigate upto sample portlet
    • For testing the code quality of sample portlet type the “ant sonar” command


Check the Sonar Report :

  • After build successful of  “ant sonar” command open  http://localhost:9000/  in the browser, you can see  your project and code quality of the project.
  • Using different tools of sonar you can show that where you need to change your code or you can get the suggestion from the sonar
  • Screen shot of sonar home page is given below                                                                      mainSonar
  • Screen short of project Report is given below                                                                         sonarReportPage
  • Screen short of Detail Report of project is given below                                                      sonarDetailReport

Note:– For Testing and Demo purpose I have used the Liferay portlet but you can also check the quality of your code in any war file or any other Java, J2EE projects.

Share!Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterFlattr the authorDigg thisPin on PinterestEmail this to someoneShare on StumbleUponShare on RedditShare on TumblrBuffer this pagePrint this page
Copyright © 2017 - Scalsys. All Rights Reserved