Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The configuration of the Maven SonarQube plug-in is described in the SonarQube™ Wiki. For an example project that uses Maven with SonarQube, see the official Maven example.

If you have an existing Maven-based build which you want to analyze, add the SonarQube Maven task (either version 2 or 3 depending on your build) to your plan configuration. Then, configure the settings in the task to your needs.

Maven Task Configuration Example

...

titleRemote Agents

Please note that if you want to use this task on a remote agent, you have to either add a Maven capability to your remote agent in the Bamboo UI or the following key/value pair to your bamboo-agent-home/bin/bamboo-capabilities.properties file, see Bamboo docs for more details:

For Maven 3.x:

system.builder.mvn3.Maven3=path/to/your/maven3/home

In this part, we show the necessary steps to execute a Maven build and the generation of SonarQube metrics including test coverage measurement with Jacoco (there are other ways to collect test coverage like Clover or Cobertura). SonarQube's runner do not by themselves measure the coverage of your code. They will use the generated coverage reports (e.g. by Jacoco) which must be configured (e.g., location of these reports). The Wiki describes the necessary changes in your Maven pom.xml like the addition of the Jacoco Maven plug-in. There are also sample Maven projects on GitHub which might help you to set this up.

Once you have setup your pom.xml, you have to configure the necessary Bamboo tasks. Since version 2.2 of the Maven Surefire plug-in (which is used to run tests and collect their results in Maven), coverage is no more collected during the analysis phase. We therefore have to configure the Maven build to collect the code coverage metrics first:

Image Removed

The next step is to use the Maven task to run the SonarQube analysis:

Image Removed

With these two tasks in place, the Maven SonarQube plug-in will generate the metrics ready to be viewed by SonarQube:

Image Removed

SonarQube server configuration

The Maven task depends on a SonarQube server configuration. SonarQube server settings with Maven can be either specified in your settings.xmlpom.xml or by using the global SonarQube server configuration in the Include Code Quality for Bamboo app settings.

a) Global SonarQube server configuration

If you just have one SonarQube server, we recommend that you specify the connection parameters in the app server configuration.

b) settings.xml

The settings.xml file contains elements used to define values which configure Maven execution in various ways, like the pom.xml, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information.

We can configure the connection settings in the settings.xml file like this:

Code Block
languagexml
titlesettings.xml for Sonar
<settings>
  <profiles>
    <profile>
      <id>sonar</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
		<sonar.host.url>http://localhost:9000</sonar.host.url>
		<sonar.login>admin</sonar.login>
		<sonar.password>admin</sonar.password>
	  </properties>
    </profile>
  </profiles>
</settings>  

c) pom.xml

It is also possible to define the SonarQube server settings in your project-specific pom.xml. A typical configuration might look like this:

Code Block
languagexml
titlepom.xml for Sonar
<properties>
  <sonar.host.url>http://localhost:9000</sonar.host.url>
  <sonar.login>admin</sonar.login>
  <sonar.password>admin</sonar.password>
</properties>

Jacoco troubleshooting:

...

If you experience the following error from Jacoco

JaCoCo SonarQube incompatible version 1007

...

Configuration

Configure the Maven project to use the Sonar™Scanner as shown in the SonarQube™ Wiki.

See the official Maven example for inspiration

  • Ensure the pom.xml of your project contains the sonar-maven-plugin as dependency

    Code Block
    languagexml
    <build>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.7.0.1746</version>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
  • [optional: code coverage] ensure your pom.xml integrates the Jacoco plugin, see https://docs.sonarqube.org/latest/analysis/test-coverage/java-test-coverage/ for more details.

[optional: code coverage] Add a Maven task to your Bamboo job:

Run the tests and generate code coverage reports:

Code Block
languagebash
clean org.jacoco:jacoco-maven-plugin

...

:prepare-agent 

...

verify

...

Add a Sonar™ Maven task to your Bamboo job:

...

Tips & Troubleshooting

Info

If you use

...

SonarQube GUI => Settings => Update Center => Plugins Updates => Java => Click "Upgrade to 2.2.1"

Furthermore, please make sure that you are using version 0.7.0.201403182114 or newer of the Jacoco Maven plug-in.

...

borderStylesolid
titleOn this page:

...

Sonar™ Maven task on a remote agent, set the Maven capability by

  • adding a Maven capability to your remote agent in the Bamboo UI

  • or add the following key/value pair to your bamboo-agent-home/bin/bamboo-capabilities.properties file, see Bamboo docs for more details:

    Code Block
    system.builder.mvn3.Maven3=path/to/your/maven3/home

The 'Maven3' part is the executable label.

Insert excerpt
Getting started
Getting started
namegit_info
nopaneltrue