Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override sonar projectKey when using maven

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?

like image 771
Will Avatar asked Apr 26 '14 15:04

Will


People also ask

What is Sonar projectKey?

projectKey is simply the unique identifier of your project inside SonarQube. You are free to choose whatever you want, as long as it is unique. Analysis Parameters is the official documentation page from Sonar, where you can find additional information about all the properties.

What does Mvn sonar sonar do?

The SonarScanner for Maven is a Maven plugin that allows you to execute SonarCloud code analysis via a regular Maven goal. As a Maven goal, the scanner is available anywhere Maven is available (locally, in CI services, etc.), without the need to manually download, set up, and maintain a separate installation.

How do I scan a Maven project in SonarQube?

Analyzing a Maven project consists of running a Maven goal: sonar:sonar from the directory that holds the main project pom. xml . You need to pass an authentication token using the sonar. login property in your command line.

How do I set sonar project properties?

SonarQube Properties and Parameters Global analysis parameters, defined in the UI, apply to all the projects (From the top bar, go to Settings > General Settings) Project analysis parameters, defined in the UI, override global parameters (At a project level, go to Configuration > Settings)


1 Answers

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

like image 131
NaanProphet Avatar answered Oct 22 '22 01:10

NaanProphet