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.

Where Maven puts files during build

If you build a project with Maven, you can notice that it creates a target folder, and copies the resource files, the compiled source files, the test resource files and the compiled test source files into specific directories. For example the target/classes directory will contain the compiled source files and the resource files. The questions that could arise are:

  • How Maven know that it needs to create the directories with these names?
  • How does it know where to find the source files to compile?
  • How does it know where are the resource files?

They must be configured somewhere, otherwise Maven wouldn’t know what are the names of these directories. The important thing to know here is that contents of pom.xml files can be inherited. These default settings that we get are  inherited from another pom xml file called the Super POM, from which every pom gets it’s default settings.

The directories mentioned above are defined in the Super POM similar to this:

<build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${artifactId}-${version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
  </build>
</sxh>

Note the directories that are created can be different if you overwrite these configuration options in your project’s pom.xml file.

Download source

You can download the source files used in this project here.

If  you build this project using the mvn clean install command, you can check out the created target folder and the directories inside it.