How to get from a source file path to a SonarQube component key for a Gradle project?

599 views
Skip to first unread message

Michael Rüegg

unread,
Jun 18, 2016, 8:35:47 AM6/18/16
to SonarQube
Hi,

I'm interested to know how I can translate file paths to SonarQube component keys for Gradle projects. Here's an example:

source path:
com.yyy.xxx.core/src/main/java/com/yyy/xxx/core/engine/processor/DebugEventProcessor.java


component key:

com.yyy.xxx:com.yyy.xxx.core:feature/Fix-Sonar-Issues:src/main/java/com/yyy/xxx/core/engine/processor/DebugEventProcessor.java


How does the SonarQube Gradle project translate file paths to component keys? Is this documented somewhere or can you point me to the source code of the SonarQube Gradle plug-in where this path->component_key translation is coded?


Thanks in advance,

Michael

Julien HENRY

unread,
Jun 20, 2016, 2:48:39 AM6/20/16
to SonarQube
Hi Michael,

Component keys for a file is <moduleKey>:<relative path from module baseDir>.

Then for Gradle the latest strategy to generate unique module key is described here:

Basically it is:
  • [<group>:]<name> for project / root module
  • <root module key><module path> for other modules

Michael Rüegg

unread,
Jun 20, 2016, 1:18:29 PM6/20/16
to Julien HENRY, SonarQube
Hi Julien,

Thanks for your answer.

If all I have is a repository with a Gradle project, then I guess I’m stuck with parsing the Gradle build files to deduce component IDs for given file paths in this repository. Are you aware of a SonarQube plug-in that does this build file parsing already?

I guess the SonarQube Gradle plug-in can easily access the SonarQube related build data in the Gradle build files when it is run as part of the Gradle build and has access to its runtime data. But I have not and I need to do this statically without executing a build. Any ideas on how to accomplish this? I think this can get quite complicated for multi-module Gradle builds and because Gradle files are just ordinary Groovy files….

Best regards,
Michael 

-- 
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/phZ8f94mKdQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/cf1b5672-b3de-478a-9885-b4c39b2aeb15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julien HENRY

unread,
Jun 20, 2016, 1:23:58 PM6/20/16
to Michael Rüegg, SonarQube
Are you interested only by the root module/project key? Or also by all submodules keys?

Michael Rüegg

unread,
Jun 20, 2016, 3:33:38 PM6/20/16
to Julien HENRY, SonarQube
I’m interested in translating file paths to fully qualified component IDs by solely analyzing the Gradle build files. Example:

source path:
com.yyy.xxx.core/src/main/java/com/yyy/xxx/core/engine/processor/DebugEventProcessor.java

component key:
com.yyy.xxx:com.yyy.xxx.core:feature/Fix-Sonar-Issues:src/main/java/com/yyy/xxx/core/engine/processor/DebugEventProcessor.java


Any ideas?

Thanks,
Michael

Julien HENRY

unread,
Jun 21, 2016, 2:24:41 AM6/21/16
to Michael Rüegg, SonarQube
For a non modular project that's pretty simple:

com.yyy.xxx = Gradle project group. Usually defined in gradle.properties or build.gradle
com.yyy.xxx.core = Gradle project name. Usually defined in gradle.properties. Default to folder name.
feature/Fix-Sonar-Issues = sonar.branch
src/main/java/com/yyy/xxx/core/engine/processor/DebugEventProcessor.java = relative file path

Michael Rüegg

unread,
Jun 21, 2016, 5:04:19 AM6/21/16
to Julien HENRY, SonarQube
Hi Julien,

Yes, for a single-module project it is relatively straightforward, but even then stuff like the project key can be defined / overridden in several places:

- gradle.properties in project dir
- gradle.properties in sub-project dir
- gradle.properties in user home

But I can also configure the project key (beside other things) in the build.gradle file:

sonarqube {
  properties {
    property "sonar.projectName", "My Project Name"
    property "sonar.projectKey", "org.sonarqube:java-gradle-simple"
  }
}

So even in this case it gets quite complicated to deduce the project key. And for multi-module projects it gets really complicated...

Could you think of another solution to deduce component IDs for file paths which doesn’t force me to parse all these files?

Thanks,
Michael

Julien HENRY

unread,
Jun 21, 2016, 5:20:20 AM6/21/16
to Michael Rüegg, SonarQube
One option could be to write a custom plugin with a Sensor that will produce an output file with mapping file absolute path <-> component key.
Reply all
Reply to author
Forward
0 new messages