Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Sonar to exclude files from Maven pom.xml

I have a project configured in maven and the code analysis is done by SonarQube.

I am trying to configure SonarQube in the pom.xml file to exclude a few files from the code analysis. Those files can be identified by their class names, they contain the underscore character before the extension (they are metamodel classes). Below I give the part of the pom.xml file where I try to exclude them:

<plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>sonar-maven-plugin</artifactId>     <version>2.2</version>     <configuration>         <sonar.sources>src/main/java</sonar.sources>         <sonar.exclusions>file:**/src/main/java/**/*_.*</sonar.exclusions>     </configuration> </plugin> 

However, the above code does not work. Is there a way to configure SonarQube from my pom.xml file to ignore those files when analysing the source code?

Thank you in advance.

like image 856
pappus Avatar asked Jan 29 '14 07:01

pappus


2 Answers

Sonar exclusions (like other sonar properties) have to be added to the <properties> section of the POM file. Like so (example from excluding jOOQ autogenerated code from current project):

<properties>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     <sonar.host.url>http://www.example.com/</sonar.host.url>     <sonar.jdbc.url>jdbc:postgresql://www.example.com/sonar</sonar.jdbc.url>     <sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>     <sonar.jdbc.username>sonar</sonar.jdbc.username>     <sonar.jdbc.password>sonar</sonar.jdbc.password>     <sonar.exclusions>org/binarytherapy/generated/**/*, **/GuiceBindComposer.java</sonar.exclusions>     <sonar.dynamic>reuseReports</sonar.dynamic> </properties> 
like image 169
Mikkel Løkke Avatar answered Sep 19 '22 06:09

Mikkel Løkke


classes/packages mentioned in <sonar.exclusions> excludes the given classes from all static analysis by Sonar, however <sonar.coverage.exclusions> excludes given classes/packages only from coverage, and still be analyzed for other parameters.

<properties>     <sonar.coverage.exclusions>         **/domain/**/*,         **/pojos/*     </sonar.coverage.exclusions> </properties> 

Reference:

  • https://docs.sonarqube.org/display/SONAR/Analysis+Parameters#AnalysisParameters-Exclusions/Inclusions

Source:

  • https://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreCodeCoverage
  • https://docs.sonarqube.org/display/SONAR/Analysis+Parameters#AnalysisParameters-Exclusions/Inclusions
  • https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven#AnalyzingwithSonarQubeScannerforMaven-ExcludingamodulefromSonarQubeanalysis
like image 44
Amit Kaneria Avatar answered Sep 21 '22 06:09

Amit Kaneria