How to replace absolute file patterns like file:**/src-gen/** with relative ones?

sonarqube

(Michael Keppler) #1

Recent versions of Sonarqube have a warning, that absolute file patterns shall be replaced by relative ones: https://github.com/SonarSource/sonarqube/blob/master/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java#L54

We have the use case of excluding generated code from scans. Right now that is done via multiple patterns like

file:**/src-gen/**

Just removing the “file:” prefix totally does not work, since Sonarqube seems to treat the pattern

**/src-gen/**

relative to the source folder of the Maven project being analyzed (i.e. it seems to look only at the package path to match the pattern and does not exclude any file). In our case that “src-gen” is the Maven project source folder, but the files do not get excluded with the shortened pattern. What do I miss?

Michael


(G Ann Campbell) #2

Hi Michael,

I’m confused.

So… it sounds like you don’t want any of the source analyzed?

Ann


(Michael Keppler) #3

Sorry Ann, I was shortening that too much in an attempt to avoid irrelevant details. Our Maven projects generally can have multiple source folders. Some of the modules may have only “src”, some may additionally have “src-gen”, “xtend-gen” and so on. The Sonar configuration for all the modules exists only once in the parent pom. There we exclude “src-gen”, but since this is only one of multiple source paths, we don’t exclude all files by that action.

Generally my question boils down to: How can I exclude Java files from the analysis, when the relevant path part for matching is neither in the package name nor in the file name, but “above in the folder structure”?


(G Ann Campbell) #4

Hi,

First, in general, analysis is and should be relative to the project root. That’s why paths should be relative, not absolute.

Now to your specific problem which I’m going to attempt to sum up, partly to validate my understanding: You have a multi-module Maven project, and each module has src (which should be analyzed) and potentially *-gen in addition. You struggle with how to - from the root pom - write relative exclusion patterns to exclude those additional source directories altogether.

Assuming I’ve got it right, then the answer is to change the definition of sonar.sources, rather than to handle this with exclusions. You should be able to configure properties to override the Maven-default definition of sonar.sources.

HTH,
Ann