07 March, 2012

Sonar in a nutshell and using Sonar Eclipse

What is Sonar?
Sonar is an open platform to manage code quality. Sonar runs as a server application that collects data from your code through a Maven plugin. The analyzed data can either be viewed in the browser or through the Eclipse plugin. Sonar allows to combine metrics altogether and also to mix them with historical measures.

Sonar covers these aspects of code quality :
  • Architecture & Design
  • Coding rules
  • Complexity 
  • Duplications
  • Unit tests
  • Potential bugs
  • Comments
Language Support
Java is built in. Open Source and commercial plugins enable to cover C, C#, Flex, Natural, PHP, PL/SQL, Cobol and Visual Basic 6.

Sonar Plugins
Listed below are few of the plugins to produce additional metrics:
  • JMeter - Retrieve JMeter test results in SONAR.
  • Useless Code - Reports on the number of lines that can be reduced in an application.
  • Artifact Size - Reports on the size of the artifact generated by projects.
  • Quality Index - Calculates a global Quality Index based on coding rules, Style, Complexity and Coverage by unit tests.
Listed below are few of the integration plugins available:
  • Bamboo
  • Hudson/Jenkins
  • Google Calendar
  • Twitter
Integrate to Eclipse

1) Installing and Using Sonar Eclipse
Install Sonar using this site and restart Eclipse. Sonar - http://dist.sonar-ide.codehaus.org/eclipse/
After installation, Eclipse restarts in Sonar perspective.

2) How to do Sonar analysis in the Eclipse?
  • Select your project and right click. In the context menu select Sonar > Analyse > Locally and then select Sonar > Run Local Analysis.
  • This will run checks using FindBugs, Checkstyle, PMD etc. The output reports will be stored in your <<{workspace}\target\sonar-embedder-work>> folder.
  • Check Measures tab
  • Select the root source folder or any package to see Violations.  e.g. Select src/main/java.
3) How to do Sonar Analysis remotely?
  • Goto Window > Preferences > Add. Enter Sonar server URL and click Finish.
  • Associate with Sonar - Select the project and right click. Configure > Associate with Sonar. Select the project the, Enter GroupId and ArtifactId.
  • Access remotely installed Sonar - Select the project and right click. In the context menu select Sonar > Analyse > Remotely, to retrieve results/reports.
  • Check Web tab for the summary of analysis data.
  • Check Hotspots tab to see classified analysis data.

Prerequisites
  • Sonar 2.4 installed on localhost or remote server
  • Eclipse 3.5.x, 3.6.x, 3.7.x
  • Maven 3.x
  • Sample Java project with Maven build
References