February 29, 2012

Sonar’s Eclipse Plugin

Sonar is an amazing tool that you should check out if you haven't already.  Start by looking at Sonar's analysis of its own code.  I think most Sonar users have it running as part of a regular build process, such as a nightly build, and review the results via the web application.  But sometimes you want faster feedback without the context-switching required when flipping between a web browser and your code.  In these cases, Sonar's Eclipse plugin can be handy.

To install the plugin for Eclipse 3.7:

1. Select Help from the top menu bar and then select Install New Software…

2. An Install window will appear.  Type "http://dist.sonar-ide.codehaus.org/eclipse/" in the Work With field and press enter.  A Sonar component should appear in the list below.  Expand the component and check the Sonar Integration for Eclipse (Required) checkbox and then press Next

3. If Eclipse can install Sonar, the Finish button will become enabled.  Press this button to install the plugin.

To configure the plugin:

1. You first must configure the location of your Sonar server.  Choose Window and then Preferences.  Choose Sonar from the listings on the left.  An Add button will appear on the right, allowing you to add a new Sonar server.

2. Press the Add button.  An Add Server Configuration window will appear.  Enter the URL of your Sonar server instance, as well as a username and password, if needed.  Then press Finish.

3. Using the Navigator or Package Explorer view, navigate to the project you want to analyze, right-click on it, and choose Configure and then Associate with Sonar…

4. An Associate with Sonar window will appear.  Choose the Sonar server you configured from the top dropdown.  Select the checkbox for the project you want analyzed, and enter the Maven GroupId and ArtifactId for your project.  If you're not using Maven, then enter your Sonar projectName value for the GroupId and your version number for the ArtifactId.  Then press Finish.

5. You can now open the Sonar perspective by choosing Window and then Open Perspective and then Other.  An Open Perspective window will appear.  Choose Sonar and then press OK.

The plugin's default behavior is to aggregate and display data from the Sonar server, forgoing any local analysis.  This works great most of the time, but there are times when you're writing new code or modifying existing code so extensively that the data from the server is no longer useful.  In these circumstances, you can opt to run a local analysis instead.  The local analysis is just a subset of the analysis performed by the server, but it covers the big pieces and gives you a general sense of your code's quality.

To run a local analysis:

1. Right-click on your project from the Navigator or Package Explorer view and choose Sonar, then Analyse…, and then Locally

2. Then right-click on the project and choose Sonar and Run Local Analysis

3 comments:

  1. Tom, thank you so much for posting how to use this tool! Why is it with open-source (I come from Microsoft background where user documentation is well defined) world that at the http://docs.codehaus.org/display/SONAR/ Installing+Sonar+Eclipse site they have no documentation??? Anyway...because of you I'm able to use SONAR in Eclipse. Thanks so much!!!

    ReplyDelete
    Replies
    1. Actually, the page you listed, along with the following - http://docs.codehaus.org/display/SONAR/Sonar+Eclipse - have steps for installing and configuring Sonar with Eclipse. There's even screenshots and a video! Nonetheless, I'm glad you found this post useful.

      In regards to documentation for open source projects, you have to remember that most of these projects rely on community contributions, including things like documentation. People are often eager to offer assistance in writing code but aren't as eager to help with the documentation - which is why many projects state that a great way to get started on contributing to the project is to write some documentation. This is a stark contrast to commercial products that have paid staff who focus strictly on documentation.

      Delete
  2. Installing Sonar and PMD plugins for Eclipse
    :: http://madhukaudantha.blogspot.com/2012/04/installing-sonar-and-pmd-plugins-for.html

    ReplyDelete