SONAR Setup

Installing Sonar

Overview

Sonar is made of 3 components:

  • A Database that stores the configuration and results of quality analysis
  • A Web Server that is used to navigate the results of the analyzes and make configuration
  • A Client that will run source code analyzers to compute data on projects

Sonar Eclipse plugin is not compatible with WCS 6.0. We need to run the sonar as a stand alone application in developer machines. We can configure Sonar in the build servers identified with the same steps.

Installing Database

Sonar supports several database engines we can choose amongst.

Find the list of supported Database at

http://docs.codehaus.org/display/SONAR/Requirements#Requirements-SupportedPlatforms

If we simply want to make a test or a demo, we can use H2 (Apache Derby prior to Sonar 3.2) as a database. H2 (Derby prior to Sonar 3.2) is shipped with Sonar and does not need any installation. This database must not be used for production.

To use one of the other databases, we simply need to create a schema and provide permissions to create, update and delete objects. Tables and indexes will then be automatically created when launching Sonar for the first time.

A sample scripts to create the schema in MySQL can be found at

https://github.com/SonarSource/sonar/tree/master/sonar-application/src/main/assembly/extras/database/mysql

Character Set

When creating a database, the recommended character set is UTF-8.

Installing Sonar Server

Download and unzip the distribution from

http://www.sonarsource.org/downloads/

Configuring Database

If we do not use the default embedded database, edit conf/sonar.properties to configure the database access. Templates are available for every supported database. Just uncomment them and comment the lines dedicated to H2 (Derby prior to version 3.2):

    sonar.jdbc.url: the URL of the database
    sonar.jdbc.driver: the class of the driver
    sonar.jdbc.user: the username
    sonar.jdbc.password: the password

For Oracle, copy the JDBC driver to /extensions/jdbc-driver/[YOUR DATABASE]/.

Other drivers for supported database are already provided (do not replace provided drivers as they are the only ones supported).

Starting Sonar Server

The default listen port is 9000, the default context path is / and Sonar listens by default to all network interfaces: '0.0.0.0'. Once launched, the Sonar web server is available on http://localhost:9000.

Parameters can be changed into the file conf/sonar.properties:

sonar.web.host : 192.0.0.1
sonar.web.port: 80
sonar.web.context: /sonar

Execute the following script to start the server:

    On Linux/Mac OS: bin/<YOUR OS>/sonar.sh start
    On Windows: bin/windows-x86-32/StartSonar.bat

We can now browse to http://localhost:9000.

Sonar Runner Installation and Configuration:

Features

The Sonar Runner is recommended as the default launcher to analyze a project with Sonar.

Prerequisites

Java 6 or higher

Sonar installed

Installation

Download Sonar Runner from

http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.2/sonar-runner-dist-2.2.zip

Un-compress the downloaded file into a directory named $SONAR_RUNNER_HOME in the next steps.

Update the global settings (database connection, server URL) by editing the file $SONAR_RUNNER_HOME/conf/sonar-runner.properties

    #----- Default Sonar server
    #sonar.host.url=http://localhost:9000
    #----- PostgreSQL
    #sonar.jdbc.url=jdbc:postgresql://localhost/sonar
    #sonar.jdbc.driver=org.postgresql.Driver
    #----- MySQL
    #sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
    #sonar.jdbc.driver=com.mysql.jdbc.Driver
    #----- Oracle
    #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
    #sonar.jdbc.driver=oracle.jdbc.driver.OracleDriver
    #----- Global database settings
    #sonar.jdbc.username=sonar
    #sonar.jdbc.password=sonar

Create a new SONAR_RUNNER_HOME environment variable set to $SONAR_RUNNER_HOME.

Add the $SONAR_RUNNER_HOME/bin directory to your path.

We can check the basic installation by opening a new shell and executing the command 'sonar-runner -h' (on windows platform the command is 'sonar-runner.bat -h') .

We should get a message like this :

usage: sonar-runner [options]    
    Options:
     -h,--help             Display help information
     -X,--debug            Produce execution debug output
     -D,--define <arg>     Define property

If we need more debug information we can add the property sonar.verbose by adding the command line parameter -Dsonar.verbose=true.

We are now ready to analyze a project with the Sonar Runner.

Analyzing a project with Sonar Runner:

For full instructions go to

http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Runner

Below are the steps to analyze the logic project of WCS.

Create a sonar-runner.properties with the following lines for Java logic folder

# required metadata
sonar.projectKey=test:sonar
sonar.projectName=Sonar Test
sonar.projectVersion=1.0
 
# optional description
sonar.projectDescription=Analyzing Logic JAR
 
# path to source directories (required)
sonar.sources=src
 
# path to project binaries (optional), for example directory of Java bytecode
sonar.binaries=bin
 
# The value of the property must be the key of the language.
sonar.language=java
 
# Additional parameters
sonar.my.property=1

Place the above file at WCToolkitEE60\workspace\WebSphereCommerceServerExtensionsLogic. Run sonar-runner from the command prompt with in the above said directory.

Sonar runner will create all review outputs. We can access the output by accessing the URL http://localhost:9000

Please note that for subsequent anlaysis we can change only the line

sonar.projectVersion=1.0
to
sonar.projectVersion=2.0

The above method maintains all versions of the project intact. We need to create separate property files for different projects that needs analysis

Below are the sample screen shots

Sonar Dashboard for the code analyzed

Hotspots

Most violated class WHRConfigParamConstants

Violation Drill down