Maven SonarQube multi module

2020-03-14 19:37发布

问题:

I have a project made up of several modules.

I'm trying to analyse these with SonarQube.

I've included the Sonar Maven plugin as a dependency in each module:

<dependency>
  <groupId>org.codehaus.sonar</groupId>
  <artifactId>sonar-maven-plugin</artifactId>
  <version>5.1</version>
</dependency>

Then I'm running Maven using:

mvn clean verify sonar:sonar

Maven completes successfully and I can see the Sonar analysis happening however when I open the Sonar UI, the modules aren't visible in projects.

However...

If I run the Maven command from an individual module directory, it is visible in projects.

Feel I'm missing something very simple, appreciate any help!

回答1:

Instead of as a dependency, put the sonar-maven-plugin in the <build> section of the root pom.xml, as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.0.1</version>
        </plugin>
    </plugins>
</build>

And since it's a multi-module project, you should first perform an install from the root project and then run the sonar:sonar goal as a dedicated step, as follows:

mvn clean install
mvn sonar:sonar

To configure the sonarqube server URL, specify a project property of sonar.host.url in your settings.xml or pom.xml as follows:

<properties>
    <!-- Optional URL to server. Default value is http://localhost:9000 -->
    <sonar.host.url>http://myserver:9000</sonar.host.url>
</properties>


回答2:

SonarQube supports multi-module projects just like Maven does. This means that a Maven project containing multiple modules maps to a SonarQube project containing multiple modules/components, not multiple projects.

Take SonarQube code for example (it's a multi-module Maven project): the parent project aggregates all info from sub-modules, then if you check its structure (or its code page) you can see the list of all sub-components (for example: SonarQube::Core)

The bottom line is that you're seeing expected behaviour. Sub-modules of a Maven project are not meant to be analysed as projects, you should always analyse the parent project and SonarQube will handle modules natively.