This analyzer is recommended to launch analysis on Java Maven project.

Prerequisites

You must have previously installed and configured Maven for SonarQube and read Analyzing Code Source.

Analyzing a Maven Project

Analyzing a Maven project consists of running a Maven goal: sonar:sonar in the directory where the pom.xml file sits.

# The sonar:sonar goal must be executed in a dedicated mvn command
mvn clean install
mvn sonar:sonar
 
# The following command may lead to unexpected issues
mvn clean install sonar:sonar

Since Java ecosystem 2.2, to get coverage information you have to generate the coverage report. If you are not generating it during your build you can use the following command:

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true

mvn sonar:sonar

Please check the JaCoCo plugin page for more advanced information

 

Configuring the SonarQube Analysis

A pom.xml file sample is available here.

Additional analysis parameters are listed on the Analysis Parameters page.

Excluding a module from SonarQube analysis

You can either:

Sample Projects

To help you get started, a simple project sample is available on github that can be browsed or downloadedprojects/languages/java/maven/java-maven-simple

How to Fix Version of Maven Plugin

It is recommended to lock down versions of Maven plugins. Two versions of the Sonar maven plugin exist, one for Maven 2 and one for Maven 3. Fixing its version depends on the Maven versions used to analyse the project:

Project analyzed with Maven 2 only

Add the following code to the pom.xml file:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>1.0</version>
      </plugin>
    <plugins>
  </pluginManagement>
</build>

The version 1.0-beta-1 is not supported anymore.

Project analyzed with Maven 3 only

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>2.1</version>
      </plugin>
    <plugins>
  </pluginManagement>
</build>

Project analyzed with both Maven 2 and Maven 3

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>${sonarVersion}</version>
      </plugin>
    <plugins>
  </pluginManagement>
</build>
<profile>
  <id>maven-2</id>
  <activation>
    <file>
      <!-- basedir expression is only recognized by Maven 3.x (see MNG-2363) -->
      <missing>${basedir}</missing>
    </file>
  </activation>
  <properties>
    <sonarVersion>1.0</sonarVersion>
  </properties>
</profile>
<profile>
  <id>maven-3</id>
  <activation>
    <file>
      <!-- basedir expression is only recognized by Maven 3.x (see MNG-2363) -->
      <exists>${basedir}</exists>
    </file>
  </activation>
  <properties>
    <sonarVersion>2.1</sonarVersion>
  </properties>
</profile>

Analyzing a Multi-language Project

Since SonarQube 4.2, it is possible to run an analysis on a multi-language project. To do so, the sonar.language property just has to be removed. Conversely, if for some reason you want to perform a single language-only analysis, make sure sonar.language is specified. By default the sonar.sources property is set to the value of the Maven sourceDirectory property (by default it is src/main/java). Therefore, for a multi-language project, the property usually has to be overridden to: sonar.sources=src. Note that this property can only be set in the pom file. It's not possible to set it via the command line.

To help you get started, a multi-language project sample can be browsed or downloaded from github: projects/languages/multi-language/multi-language-java-javascript-maven