Liferay Sonar Reports integration

In this tutorial, we will see how Liferay Plugin SDK can be integrated with Sonar Qube to get code quality report.

What is SonarQube?

Sonarqube is open source tool to manage code quality at  project level and it  mainly used to get the code quality report and check static code analysis.

Sonar Server Setup:

  • Download the SonarQube Community Edition (V5.1.2) from http://www.sonarqube.org/downloads/
  • Download Sonar Ant Task jar file (v2.4) also from above page
  • Extract SonarQube into your local machine and edit sonar.properties under in conf folder ( D:\work\sonarqube-5.1.2\conf)
  • Create database in mysql “create database sonar character set utf8”
  • Add MySQL database properties  in  sonar.properties file
    • jdbc.username=root
      jdbc.password=root
      jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
      &rewriteBatchedStatements=true&useConfigs=maxPerformance
  • Now execute StartSonar.bat file in bin\windows-x86-64 to start the server
  • verify sonar process is up message in and  open localhost:9000  in browser and login with  credentials:
    • URL : http://localhost:9000
    • UserName: admin
    • Password: admin .status

Liferay IDE Setup with Sonar ANT Task:

  • Download sonar-ant-task.jar (v2.3) from sonarqube downloads
  • In eclipse IDE, navigate to windows->preferences->ant->runtime->ANT HOME libraries
  • Click on “Add External Jar” and add sonar-ant-task2.3.jar as shown in below:

EclipseConfig

 

Liferay portlet Plugin Integration with Sonar:

  • In this step, we will add sonar ant target task in portlet build.xml file
  • Suppose for the portlet: test-portlet  which  created in eclipse.
  • The build.xml file of test-portlet is:intial_build
  • Edit  build.xml with target name: sonar
  • Liferay Service Builder auto generates lot of code for you. you can add service builder package path in exclusions property of sonar, so  add service build package  to ignore code quality generation report
    • <?xml version="1.0"?>
      <!DOCTYPE project><project name="test-portlet" basedir="." default="deploy" xmlns:sonar="antlib:org.sonar.ant">
      <import file="../build-common-portlet.xml" />
      <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" />
      <property name="sonar.jdbc.username" value="root" />
      <property name="sonar.jdbc.password" value="root" />
      <!-- list of mandatories Sonar properties -->
      <property name="sonar.language" value="java" />
      <property name="sonar.sources" value="docroot/WEB-INF/src" />
      <property name="sonar.exclusions" value="**/com/test/product/service/base/**/*,**/com/test/product/service/persistence/**/*,**/com/mb/order/model/impl/**/*" />
      <property name="sonar.projectName" value="orders-portlet" />
      <property name="sonar.projectVersion" value="1.3" />
      <property name="sonar.projectKey" value="org.codehaus.sonar:sonar-orders-portlet" />
      <echo>Sonar Report is Executing for test Portlet</echo>
      <echo>Sonar Report is Executing on Source Directory docroot/WEB-INF/src,docroot/html</echo>
      <sonar:sonar />
      </target>
      </project>
  • you can see sonar ant target in IDE under ant view  ant_after_config
  • Run the sonar target and you can find below Login into sonar site and you can see dashboard with project info
  • Refresh the sonar site and you can see the test-portlet in your dashboard and click on that Total Report
  •  This page will complete details and you can find critical,Major issues under “issues” tab. Note that, you can configure severity of the defects under “Rules” tab.

Hope this helps!!!

5 Comments

  1. Srinivasan Swaminathan

    I am facing the ERROR: org.sonar.runner.impl.RunnerException: Unable to execute Sonar

    Reply
  2. jayaram pokuri

    are you trying to run ant from Liferay? before that, make sure you have configure necessary configuration at build.xml file.

    Reply
  3. Srinivasan Swaminathan

    Yes, I given you my configured code. For this property I dont what values I need to give sonar.projectkey, sonar.projectVersion, sonar.exclusions. See the below code and I using Postgres.

    Sonar Report is Executing for test Portlet
    Sonar Report is Executing on Source Directory docroot/WEB-INF/src,docroot/html

    Reply
  4. Srinivasan Swaminathan

    <

    Sonar Report is Executing for test Portlet
    Sonar Report is Executing on Source Directory docroot/WEB-INF/src,docroot/html

    >

    Reply
  5. sanjay borana

    hello,

    How to do this for liferay sdk level.

    thanx

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *