Eric Le Merdy

Sonar - exclude files from coverage in maven pom.xml

Published by Éric Le Merdy

There always have some bad reasons to exclude files from Sonar coverage report.

But sometimes, there are some good ones. In my project, we have some classes mostly generated with lombok. Sonar is reporting code coverage is not sufficent in those files. Since we know we will never cover java bean generated code, it could be useful to ignore code coverage.

But we want to make the definition of this exclusion as close as possible from the source code. This is why we are not setting the Sonarqube general settings of the webapp. We rather use the maven pom.xml to define it. This way, this parameter is specific to this project analysis.

<project>
  <properties>
    <sonar.coverage.exclusions>
      **/bean/**/*.java,
      **/domain/**/*.java
    </sonar.coverage.exclusions>
  </properties>
</project>

Extract of a pom.xml to define where code coverage will be ignored in Sonar.

As you can see, this is a comma separated value list. Hopefully, all our beans are defined with a common package pattern. Patterns syntax can be found in the sonar documentation. There also have comprehensive exemple in the admin section of sonarqube.