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

偶尔善良 提交于 2019-12-10 12:33:40

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/37544410/sonar-maven-plugin-extending-sonar-sources-in-multi-module-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!