Sunday, December 15, 2013

Static code analysis and reporting for your Java projects with Sonar - Integrate with your Eclipse (How To)

SONAR REPORTING AND ANALYSIS FOR YOUR PROJECTS

Summary

This Document tries to help you out to install sonar, analyze your project with your sonar installation, integrate with your Eclipse, clean up violations dynamically and practice better coding.

Table of Contents

  1. Sonar Installation
  2. Download Sonar
  3. Unzip and Install
  4. Run Sonar
  5. Sonar Console
  6. Access your Sonar installation
  7. Generate Sonar Report
  8. Update your POM with SONAR configurations
  9. Example
  10. Access your project in Sonar
  11. Integrate SONAR with Eclipse
  12. Eclipse Sonar Plug-In Installation
  13. Eclipse Integration (To install this plugin in the Eclipse IDE)  - With Eclipse Market Place
  14. Eclipse Integration (To install this plugin in the Eclipse IDE)  - With Eclipse Software Update
  15. Configure Sonar in your Eclipse
  16. Link your project for the first time
  17. Analyze and clean up the code violations
  18. Run Sonar Analysis in Local

Sonar Installation

Download Sonar

Download the sonar here http://dist.sonar.codehaus.org/sonar-3.5.1.zip  and unzip the download to your favorite folder

Unzip and Install

After Unzip you will see folder structure would look something like as follows.. 

Figure 1 – Sonar Dir Structure

Run Sonar

Depends on your OS, you need to run the executable , for an instance if you are running linux-x86 and 64 bit, then you need to run start.sh

Figure 2 – Run Sonar

Sonar Console

After you start the sonar you will see some info as follows after you run the sonar

Figure 3 - Sonar Console

Access your Sonar installation

Now you can browse your sonar installation http:localhost:9000

Generate Sonar Report

Update your POM with SONAR configurations

After we have the sonar installed, we can generate the reports for any maven project, by adding the following lines in your project pom.xml (sonar hosts in your properties section)

Figure 4 - POM XML for Sonar Generation


Example

Let’s take an example of project-common; do the following steps
·         Checkout the latest code from repository to your work space
·         Do mvn clean install
·         Modify your pom.xml (pom.xml) to have the following under properties section
·         <sonar.host.url> http://localhost:9000/ </sonar.host.url>
·         Save the file
·         Do mvn sonar:sonar in your command / terminal

·         You will see some messages as following.

Figure 5 - Sonar report Generation - I

Note: And after few minutes (depends on the size of the modules the sonar report would even take longer)

Figure 6 - Sonar Report Generation - II

Finally you would see the following that indicates the sonar reporting is completed..

Figure 7 - Sonar Report Generation Successful


Access your project in Sonar

Now go to you http://localhost:9000 you would see the project report that you ran for

Figure 8 - Sonar Project Report at your Local 

Integrate SONAR with Eclipse

Eclipse Sonar Plug-In Installation

Eclipse Integration (To install this plugin in the Eclipse IDE)  - With Eclipse Market Place

Figure 9 - Sonar Eclipse Plug-in Install (Market Place)


Figure 10 - Sonar Eclipse Plug-in Install (Market Place) II

Eclipse Integration (To install this plugin in the Eclipse IDE)  - With Eclipse Software Update

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:

Figure 11- Sonar Eclipse Plug-in Install (With Install New Software Menu)


Choose Sonar Java, follow the steps and install the plugin
Note: Please make sure the project that you want to associate with sonar has already analyzed in your sonar installation


Configure Sonar in your Eclipse

Configure your local/remote sonar in your Eclipse
Go to Window > Preferences > Sonar > Servers.
Sonar Eclipse is pre-configured to access a local Sonar server listening on http://localhost:9000/. You can edit this server, delete it or add a new one.

Figure 12 - Configure Sonar Server in Eclipse

Link your project for the first time

Once the Sonar server is defined, the next step is to link your Eclipse projects with projects defined and analyzed on this Sonar server.
To do so, right-click on the project in the Project Explorer, and then Configure > Associate with Sonar...:

Figure 13 - Configure / Associate your Eclipse Project with Sonar


In the Sonar project text field, start typing the name of the project and select it in the list box:

Figure 14 - Associate your Eclipse Project with Sonar II


Click Finish. Your project is now associated to one analyzed on your Sonar server.

Analyze and clean up the code violations

Do local analysis and clean the violations

Figure 15 - Configure Modules





Figure 16 - configure sonar modules from Eclipse

Note
Please make sure you have started your local sonar server (as described in Run sonar section) otherwise you would not able to see the right sonar project that you intend to configure


Run Sonar Analysis in Local

Figure 17.a – Set Sonar Analysis on Local Mode



Figure 17:b - Run Sonar Analysis on Local



Figure 18 - sonar violation analysis console



Figure 19 - Sonar violation analysis console II


Figure 20 - Sonar violations Markers


-- End  of Document --


--Happy Clean Coding

No comments:

Post a Comment