Вопрос

I want to group 25 modules under a single project key so I can get a consolidated view of code duplication. However the sonar maven plugin uses the <groupId>:<artifactId> so each project is separate.

I've tried overriding the sonar.projectKey but the maven plugin doesn't consider it.

Is there a way of grouping modules together under a single name so that you can have an aggregate view? Or is there some other in the sonarqube server to get that aggregate view?

Это было полезно?

Решение

As far as I can tell, and contrary to some other forum posts, with at least v3.2 of the maven-sonar-plugin (maybe earlier) the sonar.projectKey property is respected and overrides the default of ${project.groupId}:${project.artifactId}.

Checking the source code also confirms it first looks for the property before defaulting it.

org.sonarsource.scanner.maven.bootstrap.MavenSonarRunner.java

private static void defineProjectKey(MavenProject pom, Properties props) {
  String key;
  if (pom.getModel().getProperties().containsKey(ScanProperties.PROJECT_KEY)) {
    key = pom.getModel().getProperties().getProperty(ScanProperties.PROJECT_KEY);
  } else {
    key = getSonarKey(pom);
  }
  props.setProperty(MODULE_KEY, key);
}

private static String getSonarKey(MavenProject pom) {
  return new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString();
}

org.sonarsource.scanner.api.ScanProperties

String PROJECT_KEY = "sonar.projectKey";

So by setting the following in the POM, for example, the projectKey can be overriden:

<properties>
    <sonar.projectKey>myprefix:${project.groupId}:${project.artifactId}</sonar.projectKey>
</properties>

Tested on Maven 3.3.9. In case this helps anyone!

Link to original GitHub Source: https://github.com/SonarSource/sonar-scanner-maven/blob/master/src/main/java/org/sonarsource/scanner/maven/bootstrap/MavenProjectConverter.java

Другие советы

I have a similar setup and use a multi modules / aggregator project for that http://maven.apache.org/pom.html#Aggregation.

Just pass the aggregator pom that contains the modules to the sonar build and it should group all modules together. The grouping name is taken from the aggregator artifact-property (and can be overwritten by its name-property).

<groupId>...</groupId>
<artifactId>aggregator-project</artifactId>
<version>...</version>
<packaging>pom</packaging>
<name>Group name overrites artifactId</name>

<modules>
 <module>my-project</module>
 <module>another-project</module>
</modules>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top