Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sonar-maven-plugin: extending sonar.sources in multi-module project

I want to extend sonar.sources, which by default is pom.xml,src/main/java, by src/main/resources in order to check XML files that are located there.

This seemingly simple task turned out to be difficult since I have a multi-module maven project (> 100 modules, nested) with a lot of them don't have a src/main/resources folder and most of them not even a src folder (e.g. for packaging=pom). This leads to a build error if I set sonar.sources to pom.xml,src/main/java,src/main/resources or pom.xml,src/main:

[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.5:sonar (default-cli) on project xyz-parent: The directory 'C:\...\submodule\src\main\resources' does not exist for Maven module ... Please check the property sonar.sources -> [Help 1]

The sonar-maven-plugin itself uses ${project.build.sourceDirectory} to do it right in every module.

I tried to use regular expressions to set sonar.sources to src/main by cutting off /java (or \java on Windows) via

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <id>regex-property</id>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>sonar.sources</name>
        <value>${project.build.sourceDirectory}</value>
        <regex>[/\\]+java$</regex>
        <replacement></replacement>
        <failIfNoMatch>false</failIfNoMatch>
      </configuration>
    </execution>
  </executions>
</plugin>

But this does nothing and I have no idea to which lifecycle phase I should bind it to have it executed.

Or is there a way to avoid the error due to missing directory in sonar.sources? According to current sonar-maven source missing directories are only ignored if the POM has pom packaging but not for ear or jar modules without resources.

Or should I ensure src/main exists before sonar start? What hook may I use for that?

like image 219
vboerchers Avatar asked May 31 '16 11:05

vboerchers


1 Answers

One of the things you can do it:

  • In your root POM, define the following properties:
    • sonar.sources to be .
    • sonar.inclusions to be src/main/**
    • => this will include all the known files that SQ finds in your modules in the src/main folder if it exists
  • In your modules, if you want to control better what gets in, you just have to use/override sonar.exclusions or sonar.inclusions
like image 56
Fabrice - SonarSource Team Avatar answered Nov 03 '22 11:11

Fabrice - SonarSource Team