1. Install Sonar Server In Windows
In order to connect sonar maven plugin and/or sonar eclipse plugin to the local sonar server (for example in windows PC), you need to install the sonar server.
Here are the steps :
- Download and unpack the sonar server zip file from here.
- Start sonar server:
bin\windows-x86-32\StartSonar.bat (for 32 bit Windows)
- Open Sonar admin page “http://localhost:9000“. Default credentials – admin/admin
- Either create a new sonar profile by importing all rules files – (a) Edit configuration given in top right web page: Configuration-> Create . Enter a profile name and select Checkstyle, Findbugs and PMD configuration files.
(b) Click “Create” and set the new profile as default
- Or restore the configuration from a backup – Configuration->System->Backup->Restore configuration.
2. Install Sonar plugin in the Eclipse IDE
- Help> Install New Software
- Paste “http://dist.sonar-ide.codehaus.org/eclipse/“ into the field named “Work with:” and press Enter.
- Select: “Sonar Integration for Eclipse..
- Click Next. Eclipse will then check to see if there are any issues which would prevent a successful installation.
- Click Finish to begin the installation process. Eclipse will then download and install the necessary components.
- 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.
3. Eclipse Configuration After Sonar plugin installation
(a) Server Configuration In Eclipse:-
- Window > Preferences > Sonar
- Check if Sonar server lists shows a link like http://localhost:9000. If it doesn’t then add the link as New Sonar Server Connection > Sonar Server URL.
- Default credentials: admin/admin
- Click Test Connection . (will get Successfully connected popup)
- Ok …
(b) Download “clover.licence” file from here. This is a FREE TRIAL VERSION and it is valid for only 30 days.
(c) Edit the pom.xml of the maven project to include the below for which you need to run the sonar. Edit proper “licenceLocation” field.
<plugin> <groupId>com.atlassian.maven.plugins</groupId> <artifactId>maven-clover2-plugin</artifactId> <version>2.6.3</version> <configuration> <licenseLocation>Give the absolute location of the “clover.license” file</licenseLocation> </configuration> <executions> <execution> <phase>verify</phase> <goals> <goal>instrument</goal> <goal>check</goal> </goals> </execution> </executions> </plugin>
4. Running the Maven project for analysing the code through Sonar.
(a) Run Sonar on the maven project
mvn clean install sonar:sonar
(b) Once the build is succesfull, login to sonar admin page to see the violations. Below is a snippet for the same:
5. Configuring Sonar Eclipse
(a) Link projects to Sonar Eclipse
This linkage operation can be done by right-clicking on the the project into the “Package explorer”, and then by choosing “Configure -> Associate with Sonar…”
You are now all done with configuration and can start using Sonar Eclipse. Change your eclipse perspective to “Sonar”.