Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Maven to run a SonarQube project analysis with two different quality profiles?

Tags:

We run SonarQube analyses for our Java projects via Maven. Maven somehow does this automagically; all we did was add the sonar-maven-plugin to our pom.xml:

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

This works fine.

But now we need to run the SonarQube analysis twice, with different quality profiles. Since you can't easily change the project key from Maven, we use SonarQube's branch property to differentiate the SonarQube projects, like this (again from pom.xml):

<properties>
    <sonar.profile>MyQualityProfile1</sonar.profile>
    <sonar.branch>Dev_${sonar.profile}</sonar.branch>
    ...
</properties>

This way, we end up with two project entries in the SonarQube UI, both of which contain the exact same code, but have different issues depending on their quality profile (one used quality profile 1, and the other used quality profile 2).

Problem: In order to achieve this, I must manually change the pom.xml properties and run the entire build twice.

Question: How can I configure maven to simply run the sonar:sonar goal twice with different properties?

This would save us a lot of time on our builds. I already found this similar question, but no answers so far. Thanks!

like image 748
barfuin Avatar asked Feb 27 '14 12:02

barfuin


People also ask

Which is the default profile used in SonarQube quality profiles?

Built-in and default profiles SonarQube comes with a built-in quality profile defined for each supported language, called the Sonar way profile (it is marked with the BUILT-IN tag in the interface).

Is it possible to copy a profile from one SonarQube instance to another?

Copy a profile from one SonarQube instance to another? Use the Back up feature on the source instance to export the profile to an XML file. Use the Restore Profile feature on the target instance to import the file.

What is the command to run an analysis in SonarQube?

Run the sonar-scanner command to start the analysis.


1 Answers

Expanding on the previous answer given by Eldad AK regarding profiles:

Create two maven profiles as follows:

<properties>
  <sonar.branch>Dev_${sonar.profile}</sonar.branch>
</properties>

<profiles>
  <profile>
    <id>QualityProfileOne</id>
    <properties>
      <sonar.profile>MyQualityProfile1</sonar.profile>
    </properties>
  </profile>
  <profile>
    <id>QualityProfileTwo</id>
    <properties>
      <sonar.profile>MyQualityProfile2</sonar.profile>
    </properties>
  </profile>
</profiles>

Then run the following:

    $ mvn clean install -DskipTests
    $ mvn sonar:sonar -PQualityProfileOne
    $ mvn sonar:sonar -PQualityProfileTwo

(you may need to perform a clean between running sonar, not sure)
like image 192
John Q Citizen Avatar answered Oct 06 '22 06:10

John Q Citizen