SonarRunner, duplicate projects and projects named 'sonar'

Sonar-Runner, the external tool relies on a the properties file sonar-project.properties which is generated by Gradle. SonarRunner plugin.

If a multi-project tree contains projects with duplicate names then sonar runner ends up analysing same module twice which ultimately leads to SonarRunner crashing with a “ERROR: Caused by: Can not add the same measure twice on org.sonar.api.resources.Project”

The reason for this is the property key is generated from the project.name which is not necessarily unique. Gradle does not provide any DSL to allow this to be changed, this should not be confused with the SonarRunner property keys “sonar.projectName” or “sonar.projectKey” which are used for different things in the generated properties file.

See https://github.com/gradle/gradle/blob/34b5e1c3702612a8f6cef7e07c342d5e80b77de3/subprojects/sonar/src/main/groovy/org/gradle/sonar/runner/plugins/SonarRunnerPlugin.java

            String modules = COMMA_JOINER.join(Iterables.transform(enabledChildProjects, new Function<Project, String>() {
                public String apply(Project input) {
                    return input.getName();
                }
            }));
            properties.put(convertKey("sonar.modules", projectPrefix), modules);
            for (Project childProject : enabledChildProjects) {
                computeSonarProperties(childProject, properties, sonarPropertiesActionBroadcastMap);
            }

I’ve seen this issue with sonar-runner-dist-2.4, Gradle 2.10 and SonarQube server 5.0.1 and 5.3

Expected outcomes, either

  • SonarRunner plugin to use fully qualified project.path instead of project.name
  • Extra property in sonarRunner { sonarProperties {}} to allow us to chose the custom keys where necessary
  • “Will not fix” - in which case I would expect documentation for SonarRunner plugin to be updated to warn of this issue

Example, in this case

Root project 'multi-project’
±-- Project ‘:base’
| — Project ‘:base:sonarOther’
| ±-- Project ‘:base:sonarOther:bar’
| — Project ‘:base:sonarOther:foo’
— Project ‘:horse’
— Project ‘:horse:base’
— Project ‘:horse:base:baz’

Output grepping on Scan

06:40:25.918 INFO - ------------- Scan bar
06:40:27.674 INFO - ------------- Scan foo
06:40:29.475 INFO - ------------- Scan sonarOther
06:40:29.777 INFO - ------------- Scan base
06:40:30.072 INFO - ------------- Scan bar

As you can see Sonar-Runner goes depth first search, and scans first half of tree, but fails when it gets to the second ‘base’ project, this is because base.sonar.modules can only exist in sonar-project.properties once …

This can be confirmed by looking at the keys in the generated sonar-project.properties file

sonar.modules=base,horse
base.sonar.modules=sonarOther
base.sonarOther.sonar.modules=bar,foo
horse.base.sonar.modules=baz
horse.sonar.modules=base

Also, very closely related to this issue is projects named ‘sonar’. I’ve raised this with SonarRunner team – https://groups.google.com/forum/#!searchin/sonarqube/squirrel/sonarqube/l20VX71KQ80/HIqJZhE6EAAJ and https://jira.sonarsource.com/browse/SONAR-6514, The fix here could be the same… If generated sonar-project.properties file used fully qualified name then this would fix all cases apart from root project being named ‘sonar’…

The ‘sonar-runner’ plugin bundled with Gradle core has been deprecated in favor of the plugin maintained by the sonarqube team. I’d suggest you try that plugin out and see if it solves your problem. The core plugin is effectively EOL and no further maintenance is being done on it.