103

I have a Sonar profile in Maven. Everything works fine except the code coverage metric. I want to make Sonar ignore some classes only for the code coverage metric. I have the following profile:

<profile> <id>sonar</id> <properties> <sonar.exclusions>**/beans/jaxb/**</sonar.exclusions> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.plugin.version}</version> <configuration> <redirectTestOutputToFile>true</redirectTestOutputToFile> <excludes> <exclude>**/*Suite*.java</exclude> <exclude>**/*RemoteTest.java</exclude> <exclude>**/*SpringTest.java</exclude> <exclude>**/*CamelTest.java</exclude> <exclude>**/*FunctionalTest.java</exclude> <exclude>**/*IntegrationTest.java</exclude> <exclude>**/*DaoBeanTest.java</exclude> </excludes> </configuration> </plugin> </plugins> </build> </profile> 

Please help. I tried to add something like

<exclude>com/qwerty/dw/publisher/Main.class</exclude> 

but it didn't help

UPDATE

I have a correct Cobertura profile. I tried to add it to the Sonar profile, but still I have 53% instead about 95% like in the Cobertura profile

<profile> <id>sonar</id> <properties> <sonar.exclusions>**/beans/jaxb/**</sonar.exclusions> <sonar.core.codeCoveragePlugin>cobertura</sonar.core.codeCoveragePlugin> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.plugin.version}</version> <configuration> <redirectTestOutputToFile>true</redirectTestOutputToFile> <excludes> <exclude>**/*Suite*.java</exclude> <exclude>**/*RemoteTest.java</exclude> <exclude>**/*SpringTest.java</exclude> <exclude>**/*CamelTest.java</exclude> <exclude>**/*FunctionalTest.java</exclude> <exclude>**/*IntegrationTest.java</exclude> <exclude>**/*DaoBeanTest.java</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>${cobertura.maven.plugin.version}</version> <configuration> <instrumentation> <excludes> <exclude>com/qwerty/dw/dao/*</exclude> <exclude>com/qwerty/dw/domain/*</exclude> <exclude>com/qwerty/dw/beans/**/*</exclude> <exclude>com/qwerty/dw/daemon/exception/*</exclude> <exclude>com/qwerty/dw/daemon/Main.class</exclude> <exclude>com/qwerty/dw/sink/Main.class</exclude> <exclude>com/qwerty/dw/publisher/Main.class</exclude> <exclude>com/qwerty/dw/publisher/dao/*</exclude> <exclude>com/qwerty/dw/publisher/domain/*</exclude> </excludes> </instrumentation> <formats> <format>html</format> </formats> <aggregate>true</aggregate> <check> <haltOnFailure>true</haltOnFailure> <branchRate>60</branchRate> <lineRate>60</lineRate> <totalBranchRate>60</totalBranchRate> <totalLineRate>60</totalLineRate> </check> </configuration> <executions> <execution> <goals> <goal>clean</goal> <goal>check</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> 
2

11 Answers 11

133

At the time of this writing (which is with SonarQube 4.5.1), the correct property to set is sonar.coverage.exclusions, e.g.:

<properties> <sonar.coverage.exclusions>foo/**/*,**/bar/*</sonar.coverage.exclusions> </properties> 

This seems to be a change from just a few versions earlier. Note that this excludes the given classes from coverage calculation only. All other metrics and issues are calculated.

In order to find the property name for your version of SonarQube, you can try going to the General Settings section of your SonarQube instance and look for the Code Coverage item (in SonarQube 4.5.x, that's General Settings → Exclusions → Code Coverage). Below the input field, it gives the property name mentioned above ("Key: sonar.coverage.exclusions").

Sign up to request clarification or add additional context in comments.

10 Comments

For gradle it is sonarRunner { sonarProperties { property "sonar.coverage.exclusions", " '/domain/' , '/facade/' , '/stub/' , '/model/' , '/config/' " property "sonar.exclusions", "/stub/" } }
@MichaelTecourt - Its here SonarQube analysis exclusions & inclusions Extract from the documentation: sonar.coverage.exclusions - Comma-delimited list of file path patterns to be excluded from coverage calculations
The proper syntax to ignore foo package completely is: **/foo/**/*
@Andrew Roth The comment I added is 2 years old now and Sonar has seen a lot of changes by then :) The parameter to be used now is sonar.exclusions. Reference from the new documentation: docs.sonarqube.org/latest/project-administration/…
No, that's not correct. sonar.exclusions will exclude mentioned files or directories from analysis. sonar.coverage.exclusions still exists and will exclude mentioned files or directories from code coverage like in question asked. But it's not mentioned in current documentation. I'm using SonarQube 8.1 and I could see the configuration key sonar.coverage.exclusions under Administration > Analysis Scope
|
47

For me, this worked (basically pom.xml level global properties):

<properties> <sonar.exclusions>**/Name*.java</sonar.exclusions> </properties> 

According to: https://docs.sonarsource.com/sonarqube/latest/project-administration/analysis-scope/#file-exclusion-and-inclusion

It appears you can either end it with ".java" or possibly "*"

to get the java classes you're interested in.

4 Comments

I was also looking at this, but this excludes the files from every metric (documented API, rule compliance, ...) and not only test coverage.
Yeah that's what we wanted in our case. You might be able to add it to like pmd/findbugs or jacoco exclusions elsewise [?]
That may be what you wanted however the question was explicitly about coverage: " I want to make Sonar ignore some classes only for the code coverage metric"
Doesn't work for me. sonar.coverage.exclusions did the trick
22

Accordingly to this document for SonarQube 7.1

sonar.exclusions - Comma-delimited list of file path patterns to be excluded from analysis sonar.coverage.exclusions - Comma-delimited list of file path patterns to be excluded from coverage calculations

This document gives some examples on how to create path patterns

# Exclude all classes ending by "Bean" # Matches org/sonar.api/MyBean.java, org/sonar/util/MyOtherBean.java, org/sonar/util/MyDTO.java, etc. sonar.exclusions=**/*Bean.java,**/*DTO.java # Exclude all classes in the "src/main/java/org/sonar" directory # Matches src/main/java/org/sonar/MyClass.java, src/main/java/org/sonar/MyOtherClass.java # But does not match src/main/java/org/sonar/util/MyClassUtil.java sonar.exclusions=src/main/java/org/sonar/* # Exclude all COBOL programs in the "bank" directory and its sub-directories # Matches bank/ZTR00021.cbl, bank/data/CBR00354.cbl, bank/data/REM012345.cob sonar.exclusions=bank/**/* # Exclude all COBOL programs in the "bank" directory and its sub-directories whose extension is .cbl # Matches bank/ZTR00021.cbl, bank/data/CBR00354.cbl sonar.exclusions=bank/**/*.cbl 

If you are using Maven for your project, Maven command line parameters can be passed like this for example -Dsonar.coverage.exclusions=**/config/*,**/model/*

I was having problem with excluding single class explicitly. Below my observations:

**/*GlobalExceptionhandler.java - not working for some reason, I was expecting such syntax should work com/some/package/name/GlobalExceptionhandler.java - not working src/main/java/com/some/package/name/GlobalExceptionhandler.java - good, class excluded explicitly without using wildcards 

Comments

12

When using sonar-scanner for swift, use sonar.coverage.exclusions in your sonar-project.properties to exclude any file for only code coverage. If you want to exclude files from analysis as well, you can use sonar.exclusions. This has worked for me in swift

sonar.coverage.exclusions=**/*ViewController.swift,**/*Cell.swift,**/*View.swift 

Comments

6

For jacoco: use this properties:

-Dsonar.jacoco.excludes=**/*View.java 

1 Comment

sonar.jacoco.excludes is no longer supported (as of SonarQube 4.5.1). See here for the "new" configuration.
4

I had to struggle for a little bit but I found a solution, I added

-Dsonar.coverage.exclusions=**/*.* 

and the coverage metric was cancelled from the dashboard at all, so I realized that the problem was the path I was passing, not the property. In my case the path to exclude was java/org/acme/exceptions, so I used :

`-Dsonar.coverage.exclusions=**/exceptions/**/*.*` 

and it worked, but since I don't have sub-folders it also works with

-Dsonar.coverage.exclusions=**/exceptions/*.* 

Comments

3

I am able to achieve the necessary code coverage exclusions by updating jacoco-maven-plugin configuration in pom.xml

 <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> <executions> <execution> <id>pre-test</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <propertyName>jacocoArgLine</propertyName> <destFile>${project.test.result.directory}/jacoco/jacoco.exec</destFile> </configuration> </execution> <execution> <id>post-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${project.test.result.directory}/jacoco/jacoco.exec</dataFile> <outputDirectory>${project.test.result.directory}/jacoco</outputDirectory> </configuration> </execution> </executions> <configuration> <excludes> <exclude>**/GlobalExceptionHandler*.*</exclude> <exclude>**/ErrorResponse*.*</exclude> </excludes> </configuration> </plugin> 

this configuration excludes the GlobalExceptionHandler.java and ErrorResponse.java in the jacoco coverage.

And the following two lines does the same for sonar coverage .

 <sonar.exclusions> **/*GlobalExceptionHandler*.*, **/*ErrorResponse*.</sonar.exclusions> <sonar.coverage.exclusions> **/*GlobalExceptionHandler*.*, **/*ErrorResponse*.* </sonar.coverage.exclusions> 

1 Comment

Why is sonar.exclusions AND soar.coverage.exclusions both needed?
2

I think you 're looking for the solution described in this answer Exclude methods from code coverage with Cobertura Keep in mind that if you're using Sonar 3.2 you have specify that your coverage tool is cobertura and not jacoco ( default ), which doesn't support this kind of feature yet

1 Comment

I reformulate question. How to reuse reports generated by cobertura
2

Sometimes, Clover is configured to provide code coverage reports for all non-test code. If you wish to override these preferences, you may use configuration elements to exclude and include source files from being instrumented:

<plugin> <groupId>com.atlassian.maven.plugins</groupId> <artifactId>maven-clover2-plugin</artifactId> <version>${clover-version}</version> <configuration> <excludes> <exclude>**/*Dull.java</exclude> </excludes> </configuration> </plugin> 

Also, you can include the following Sonar configuration:

<properties> <sonar.exclusions> **/domain/*.java, **/transfer/*.java </sonar.exclusions> </properties> 

Comments

1

If you are looking for exclusion and using Gradle.

Add below lines in build.gradle file:

sonarqube { properties { property "sonar.exclusions", "'**/example/your/service/impl/**'," } } 

Note the inner single quote. Good luck.

Comments

0

You don't need an inner single quotes, just comma separated paths:

sonarqube { properties { property "sonar.exclusions", "**/example1/**, **/example2/**" } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.