Sonar Installation and Eclipse Plugin
Sonar Installation and Eclipse Plugin
Join the DZone community and get the full member experience.Join For Free
You've been hearing a lot about agile software development, get started with the eBook: Agile Product Development from 321 Gang.
This Document tries to help you install Sonar, analyze your project with your Sonar installation, integrate with Eclipse, clean up violations dynamically, and practice better coding.
Table of Contents
- Sonar Installation
- Download Sonar
- Unzip and Install
- Run Sonar
- Sonar Console
- Access your Sonar installation
- Generate Sonar Report
- Update your POM with SONAR configurations
- Access your project in Sonar
- Integrate SONAR with Eclipse
- Eclipse Sonar Plug-In Installation
- Eclipse Integration (To install this plugin in the Eclipse IDE) - With Eclipse Market Place
- Eclipse Integration (To install this plugin in the Eclipse IDE) - With Eclipse Software Update
- Configure Sonar in your Eclipse
- Link your project for the first time
- Analyze and clean up the code violations
- Run Sonar Analysis in Local
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
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
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
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
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
Opinions expressed by DZone contributors are their own.