Introduction to Sonar
It is an open source platform to continuously analyse and measure code quality. It helps decide the necessary measures that should be taken for the improvement of code quality by focusing the following seven areas that are crucial in deciding the quality of code.
For more details please visit
http://www.sonarsource.org/
Installing Sonar
Download the latest version of sonar zip file from
http://www.sonarsource.org/downloads/ and unzip this file. As currently the latest version is 3.1.1 therefore we will download zip file for the version is 3.1.1. On unzipping this zip file a
sonar-3.1.3 directory is created.
In this directory, go to the
\conf sub-directory and edit the
sonar.properties file. Here, the credentials for a
sonar user are already defined:
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar
Note: We need to change the above username and password properties if we have different database credentials.
We will look at the section dedicated to MySQL database to define the access to our MySQL database:
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
sonar.jdbc.validationQuery: select 1
It is noticeable here that the sonar database should already be created before deploying the sonar web-application to the Tomcat server.
And that’s all for the Sonar configuration.
Now, we need to make sure that mysql-connector-java-5.1.18.jar exists in
\extensions\jdbc-driver\mysql and
if it does not exist then we need to copy it to that location.
Finally build the sonar.war file that we will use to deploy Sonar in Tomcat. But first, it will be better to download the desired Sonar plugins from
http://docs.codehaus.org/display/SONAR/Plugin+Library and put them into the Sonar
\extensions\plugins directory to avoid restarting sonar server
for loading the plugins.
It is very important to know that for sonar version 3.1.1the SonarJ plugin prevents the sonar server from starting therefore we should use Sonargraph plugin instead. Please see comments on
SONARPLUGINS-1434
Now run the
build-war.bat file for windows or
build-war.sh file for linux or Mac located in the
\war directory and soon you will have the sonar.war file created.
Now deploy the generated war file into the Tomcat server. The sonar server should be up and running as depicted under.
Note that the default username and password for sonar administrator user is admin/admin.
Analysing Projects with Sonar Runner
First download Sonar Runner zip file from
http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner. Unzip the file and open sonar-runner.properties file in the
/conf directory. Now set the global database and sonar server properties for the sonar runner.
#----- Default Sonar server
sonar.host.url=http://localhost:8080/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driver=com.mysql.jdbc.Driver
#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
Note: We need to change the above username and password properties if we have different database credentials.
Next add an environment variable SONAR_RUNNER_HOME to point to the root directory of the sonar runner and also add SONAR_RUNNER_HOME/bin to PATH variable.
Now go to the root directory of the project that should be analysed with sonar and add a sonar-project.properties file there that should contain the following properties.
# required metadata
sonar.projectKey=h:TTSProject
sonar.projectName=TTSProject
sonar.projectVersion=1.0
# path to source directories (required)
sources=src
# path to test source directories (optional)
# tests=testDir1,testDir2
# path to project binaries (optional), for example directory of Java bytecode
binaries=build
# optional comma-separated list of paths to libraries. Only path to JAR file and path to directory of classes are supported.
# libraries=path/to/library.jar,path/to/classes/dir
# Uncomment this line to analyse a project which is not a java project.
# The value of the property must be the key of the language.
# sonar.language=cobol
# Additional parameters
sonar.scm.url=scm:svn:file://localhost:8080/sonar
Note that the sonar.scm.url property is needed when sonar scm activity plugin is added to the sonar application. The format for the sonar.scm.url is predefined. Please visit for details
http://maven.apache.org/scm/scm-url-format.html.
scm:<scm_provider><delimiter><provider_specific_part>
Finally run
sonar-runner command from the root directory of the project where sonar-project.properties file was added. That's it. Following snapshot depicts the results of our first sonar analysis.