Share

Integrating Sonar with Liferay in windows

Sonar is an open source Platform used by development teams to manage source code quality. Sonar has been developed with a main objective, To make code quality management accessible to everyone with minimal effort.

And it aggregates many tools
  • Static analysis(PMD, Find bugs, Check Style)
  • Duplicate code(Squid,CPD)
  • Code coverage(Cobertura,JaCoCo)
The main purpose of sonar is to test Code Quality

   What is code quality?
         It is an indicator about how quickly developers can add business value to a software system.


A well written program is a program  where cost of implementing a feature is constant throughout the program's lifetime

Prerequisites

  • Windows(xp/vista or 7)
  • Ant 1.8.3
  • Java 1.6
  • Mysql 5.0
  • Sonar 4.0
  • Sonar Ant Task 1.3 jar
Creating the Database


Create the database by executing following command in Sql prompt

  create database sonar;

Refresh the database if you are using MySql workbench or Sqlyog
Otherwise execute following command and as a output you can see the list of databases,in those sonar is one of the databases.

  show databases;

Installing and Running Sonar server

1. Download sonar zip file from sonar Downloads page.
2. Extract it at your choice of location.(D:\sonarqube-4.4)
3. Open sonar config properties file in config folder of sonar home directory(D:\sonarqube-4.4\conf)
4. Provide your database username and password in sonar.properties file.

sonar.properties
   sonar.jdbc.username=root
   sonar.jdbc.password=         

    For myself i didn't give password.
5. By default it come with derby database so comment the following line in sonar.properties by
    adding   # before the line

#sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar

6. Activate the Mysql database by uncomment following properties

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

7. Start the sonar by running StartSonar batch file located in bin
    (D:\sonarqube-4.4\bin\windows-x86-64\StartSonar).
8. In console "The web server is started" message will be displayed.
9. To test the sonar is installed correct or not access the sonar server in browser
     http://localhost:9000(Default port).
10. If you can see the following screen then you successfully completed sonar installation.
11.Login using admin/admin
12. Goto configuration by clicking configuration link in top menu.
13. Click on Restore Profile link

Installing the Sonar Ant Task

1. To run Ant task we need to download the Sonar Ant task jar file from Sonar repository.Download
     only “sonar-ant-task-1.3.jar” file, and save it in D:\sonarJar\sonar-ant-task-1.3.jar“.

Integration with Liferay 6.2

The following steps tells about integration of Sonar with Liferay6.2 

Making Sonar library available to Liferay

1. Open the eclipse Liferay6.2 Environment
2. Goto Window → Preferences Ant → Runtime
3. Click on AntHomeLibraries
4. Click on Add External Jars
5. Give the path of sonar-ant-task-1.3.jar
6. Click on Apply and then Ok
7. Now the jar is available to Liferay Environment

Sonar Ant Target

1. Open plugins folder of Liferay6.2
2. We can find build.xml file,Open it
3. Add the Sonar Ant target as follows
<!--This is the sonar target for testing the code quality-->
<!-- The properties file to give your projectname -->
<property file="sonardeploy.properties"></property>
<target name="sonar">
                <property name="sonar.host.url" value="http://localhost:9000" />   
                <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306   
                                                      /sonar?useUnicode=true&amp;characterEncoding=utf8" />
                <property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
               
                <!-- the username and password of your database -->
                <property name="sonar.jdbc.username" value="root" />
                <property name="sonar.jdbc.password" value="" />
               
                <!-- list of mandatories Sonar properties -->
                <property name="sonar.sources" value="portlets/${projectName}/docroot
                                                                                                                    /WEB-INF/src" />
                <property name="sonar.projectName" value="${projectNameInSonarPortal}" />       
                <property name="sonar.binaries" value="portlets/${projectName}/docroot
                                                                                                             /WEB-INF/classes" />
                <property name="sonar.libraries" value="portlets/${projectName}/docroot
                                                                                                                  /WEB-INF/lib" />
                                               
                <!-- The console info -->
                <echo>The given project name ${projectName}</echo>
                <echo>The project name in sonar portal ${projectNameInSonarPortal}</echo>
                <echo>The sonar sources directory ${sonar.sources}</echo>

                 <!-- Execute Sonar -->
                <sonar:sonar key="encore-portal" version="0.1-SNAPSHOT"
                                                                                    xmlns:sonar="antlib:org.sonar.ant"/
</target>

4. Now save the build.xml.
5. In eclipse import plugins project as "Existing project from workspace" under general.
6. Goto the Antview of the LIferay6.2 and add the plugins build.xml to the ant view.
7. If you observe Now we have Sonar Ant task as follows

8. Instead of every time updating the build.xml for giving the project name i have created one
    .properties file and reading those properties from properties file to the pligins build.xml by using
     following tag.

<!-- The properties file to give your projectname -->
<property file="sonardeploy.properties"></property>

9. Create sonardeploy.properties file in the sam location of liferay plugins build.xml(E:\Liferay
    \Liferay 6.2\workspace\plugins) And add the following properties

sonardeploy.properties
 #This is the name of the portlet or name of the one of plugins(Ext or Hook)
 projectName=CreateMeeting-portlet
 #This is the name of the project which will be shown in Sonar portal
 projectNameInSonarPortal=CreateMeeting

10. Save it and execute Sonar ant ask in ant view.Before that we have to deploy our portlet in
      liferay server.
           Deploy the portlet in liferay server by executing ant deploy of that particular portlet build.xml
           Execute the  Sonar ant ask through plugins build.xml

11. Now refresh the sonar server in web browser( http://localhost:9000)
12. You can see your project with the name(CreateMeeting) given for the property
      projectNameInSonarPortal in sonardeploy.properties as like below.


13. If we click on project name, we can see all the violations, boilerplate code and etc regarding
      project.

Congratualtions...!!!. You have completed successfully.

Note:Please observe the ant sonar target code in build.xml.It is applicable only for to test the code quality of portlets.If you want to test other plugins like ext,hook then we have to change the path of the following properties.
  • sonar.sources
  • sonar.binaries
  • sonar.libraries

No comments:

Post a Comment