...
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
...
title | Remote 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:
The next step is to use the Maven task to run the SonarQube™ analysis:
With these two tasks in place, the Maven SonarQube™ plug-in will generate the metrics ready to be viewed by SonarQube™:
SonarQube™ server configuration
The Maven task depends on a SonarQube™ server configuration. SonarQube™ server settings with Maven can be either specified in your settings.xml
, pom.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 | ||||
---|---|---|---|---|
| ||||
<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 | ||||
---|---|---|---|---|
| ||||
<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 thesonar-maven-plugin
as dependencyCode Block language xml <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 | ||
---|---|---|
| ||
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.
...
borderStyle | solid |
---|---|
title | On this page: |
...
Sonar™ Maven task on a remote agent, set the Maven capability by
The 'Maven3' part is the executable label. |
Insert excerpt | ||||||||
---|---|---|---|---|---|---|---|---|
|