Monday, April 30, 2012

How to run sonar for Maven based java project


To run sonar, either we can directly run using maven script or we can run sonar from eclipse plugins.
To run sonar report, first we have to install maven & sonar in our local machine.


  1. Download and extract Maven in your C drive from http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.zip
  2. Install Jdk 
  3. Download and extract Sonar in your D drive from http://dist.sonar.codehaus.org/sonar-3.0.zip
  4. Now go to D:\sonar-3.0\bin\windows-x86-32 and double click on StartSonar.batNow sonar server will start locally on http://localhost:9000
  5.  Go to parent folder of your project, and run following command
    mvn clean compile install sonar:sonar as below
  6. After its completion, browse http://localhost:9000/ where you will see the project list with code quality report as below

How to Install Sonar in Eclipse plugins?

Installing Sonar Eclipse plugins for java


Name Sonar Eclipse
License LGPL v3
Authors Evgeny Mandrikov (SonarSource), Jérémie Lagarde
Latest version 2.3.0
Update site http://dist.sonar-ide.codehaus.org/eclipse/
Eclipse Marketplace http://marketplace.eclipse.org/content/sonar
JIRA Issue Tracker http://jira.codehaus.org/browse/SONARIDE/component/14315
Sources https://github.com/SonarSource/sonar-eclipse

Compatibility Matrix

Plugin 1.0.0 2.0.0 2.1.0 2.2.0+
Sonar 2.0.1+ 2.4+ 2.4+ 2.4+
Eclipse 3.5.x, 3.6.x 3.5.x, 3.6.x 3.5.x, 3.6.x 3.5.x, 3.6.x, 3.7.x
Mylyn (optional) – – 3.2.0+ and requires Sonar 2.8+ 3.2.0+ and requires Sonar 2.9+

Installation

To install this plugin in the Eclipse IDE:
  1. Select Help -> Install New Software. This should display the "Install" dialog.
  2. Paste the Update Site URL into the field named "Work with:" and press Enter. Pressing Enter should cause Eclipse to update list of available plugins and components.
  3. Choose the component listed under Sonar: "Sonar Integration for Eclipse (Required)".
  4. Click Next. Eclipse will then check to see if there are any issues which would prevent a successful installation.
  5. Click Finish to begin the installation process. Eclipse will then download and install the necessary components.
  6. Once the installation process is finished, Eclipse will ask you if you want to restart the IDE. It's strongly recommended that you restart IDE.

Empty GroupId error when trying to Associate with Sonar

How/Where to add GroupId and artifactId when project associating with sonar in eclipse.
First time when I started using Sonar (project quality tools) for my Java based web project, i google a lot & read articles about it. I started installing sonar in eclipse ide.
But unfortunately I got following error 'Empty GroupId error when trying to Associate with Sonar'.
Finally its solved by giving groupId and aftifactId. Actually because of lack of interactive GUI in eclipse most of the time people get confused.
As in below image, just double click on respective column and give groupID and artifactID same in pom.xml

how to solve Empty GroupId error when trying to associate with Sonar