Sunday, April 17, 2016

Generate Sonar Report for Maven and Ant Project

Below Post will describe about how to generate Sonar Report for maven and ant project.

Prerequisites : 


  • JDK 1.7
  • Mysql
  • sonarqube-5.4
  • Maven
  • Ant

Download sonarqube-5.4 from here sonar 5.4 and Extract the Sonar zip file.

Create database called "sonar" in mysql. You can choose oracle also in my case i have use mysql.
Open <sonar-directory>\conf\sonar.properties
Provide your database username and password as shown below                              

sonar.jdbc.username: root
sonar.jdbc.password: root



Uncomment below Lines of code by removing "#" for mysql jdbc connection

sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

if your using oracle then Uncomment above Lines of code by removing "#" for Oracle jdbc connection.

Now, Edited the \sonarqube-5.4\sonarqube-5.4\conf\wrapper.conf file and added the JDK path,
                wrapper.java.command=C:\Program Files\Java\jdk1.7.0_79\bin\java.exe as show below :

# Path to JVM executable. By default it must be available in PATH.
# Can be an absolute path, for example:
#wrapper.java.command=/path/to/my/jdk/bin/java
wrapper.java.command=C:\Program Files\Java\jdk1.7.0_79\bin\java.exe

Now we are all set for starting sonar server so start the sonar by clicking on
\sonarqube-5.4\bin\windows-x86-64\StartSonar.bat. 

If every thing goes fine then you should see below screen :


In the above screen in highlighted area you must be seeing as "Process[web] is up". It means your server has started. Now go to your browser and type "http://localhost:9000/" you should see below screen:


sonar default user name:  admin and password: admin. You can login and can see above screen.
So far have discuss about setting and starting sonar.

Now we will test our project with sonar.

Sonar Report For Maven Project -: 

Go to your project pom,xml file and add below line into your pom.xml file inside properties tag.

<sonar.host.url>http://localhost:9000/</sonar.host.url>

below is the properties tag for my project

<properties>
<jdk.version>1.7</jdk.version>
<spring.version>2.5.6</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>2.5</servletapi.version>
<sonar.host.url>http://localhost:9000/</sonar.host.url>
</properties>
I assume you already have installed maven in your system if not please installed it.
Now first clean and then install your maven project and then go to project path and open command prompt and type command mvn sonar:sonar it will generate sonar report as below :


After successfully generation sonar report you refresh your "http://localhost:9000/dashboard/"
you will be able to see your project name as shown in below screen : 


This is it for generating sonar report for maven project.
Note : 
If you don't want some file to include in sonar report then use exclusion property of sonar as shown below in your pom.xml file

<sonar.exclusions>
**/test/folderOne/**,**/test/folderTwo/**
</sonar.exclusions>

if you have multiple project and you want to skip a project as a whole then keep below property in that project pom.xml file.

<sonar.skip>true</sonar.skip>

Sonar Report For Ant Project -:

Open your build.xml file of your ant project and paste below line inside <project> tag as shown below: 

<!-- Define the SonarQube global properties (the most usual way is to pass these properties via the command line) -->
<property name="sonar.host.url" value="http://localhost:9000" />

<property name="build.dir" value="docroot/WEB-INF" />
<property name="src.dir" value="${build.dir}/src" />
<property name="classes.dir" value="${build.dir}/classes" />

<property name="sonar.projectKey" value="org.codehaus.sonar:yourprojectname" />
<property name="sonar.projectName" value="yourprojectname" />
<property name="sonar.projectVersion" value="1.0" />

<property name="sonar.sources" value="${src.dir}" />
<property name="sonar.binaries" value="${build.dir}" />


<!-- ========= Define SonarQube target ========= -->
<target name="sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
</taskdef>

<sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>

</target>

below is screen of my project build.xml file


I Assume you already have installed ant in your system if not please install it.
Download from here sonar-ant-task/2.2 put sonar-ant-task/2.2 into your ant lib folder.
now first clean and then deploy your ant project and then go to project path and open command prompt and type command ant sonar it will generate sonar report as below :


After successfully generation sonar report you refresh your "http://localhost:9000/dashboard/"
you will be able to see your project name as shown in below screen : 


Note : 
For skipping any file or any project same property will be used as i have mentioned for maven, But in ant it will be under sonar target tag as shown below : 

for skipping file :

<target name="sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
</taskdef>
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>
<sonar.exclusions>**/test/folderOne/**,**/test/folderTwo/**</sonar.exclusions>
</target>

for skipping project :

<target name="sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
</taskdef>
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>
<sonar.skip>true</sonar.skip>
</target>

This is it for generating sonar report with ant.

To see blocker/critical/major issue of your project go through the screen according to highlighted area :