How to set up Sonar with Maven on localhost

This tutorial describes how to set up Sonar to work with Maven projects on a local development machine. Sonar is a platform for continously inspecting code quality. It is now officially called SonarQube, but a lot of people know it by the original name.

Downloading

The first step is to download Sonar form it’s offical site (http://www.sonarqube.org) and extract it to a directory of your choice. At the time of writing the latest version is: SonarQube 4.4.

Configure Sonar and Maven

For Sonar to able to collect data about the code quality of projects, it needs to have access to a database where it can store the generated data. Also, we have to define a Maven profile that will be used to run our Maven project through Sonar code analysis. For this tutorial we will be using a MySQL database.

In the Sonar installation directory open the conf/sonar.properties file and configure the following properties:

sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar_db?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

In the Maven installation directory open the conf/settings.xml file and add a new profile inside the <profiles> tag:

<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.jdbc.url>
          jdbc:mysql://localhost:3306/sonar_db?useUnicode=true&characterEncoding=utf8
        </sonar.jdbc.url>
        <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
        <sonar.jdbc.username>root</sonar.jdbc.username>
        <sonar.jdbc.password>root</sonar.jdbc.password>
        <sonar.host.url>http://localhost:9000</sonar.host.url>
    </properties>
</profile>

Of course, in both cases you need to configure you own username and password, and the schema name that you plan on using (in this case it is sonar_db). With this configuration the Sonar web application will be accessible at the http://localhost:9000 url.

Create the database schema

When we first start Sonar, it will automatically create the required database tables, but the DB schema that it uses must already exist. So create an empty database schema with the name you configured (sonar_db).

If you forget to create the schema you can see the following exceptions in the log file (logs/sonar.log)

org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'sonar_db')
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'sonar_db'
ActiveRecord::ConnectionNotEstablished: no connection available
org.jruby.rack.RackInitializationException: no connection available
org.jruby.exceptions.RaiseException: (ConnectionNotEstablished) no connection available

Start the Sonar server

For Maven to access the server, we need to launch the server first. Navigate to the bin directory of your installation and select the folder according to your environment. I have a Windows 7 64bit system, so I choose the windows-x86-64 directory and start the server by running the StartSonar.bat file.

In the console, you can see the following feedback if the server is started:

wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    |
jvm 1    | 2014.08.29 10:32:43 INFO  Web server is started

Try to access the web application by entering http://localhost:9000/ in your browser. You should see something similar to this if it managed to start properly:

Sonar webapp

Run the analysis

Navigate to the directory of your Maven project and issue the following command to the perform code analysis:

mvn sonar:sonar

After this completes, you can go back to the web application and view the results there.