How to make Sonar ignore some classes for codeCoverage metric?

JavaMavenSonarqube

Java Problem Overview


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>

Java Solutions


Solution 1 - Java

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").

Solution 2 - Java

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

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

According to: http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-Patterns

> It appears you can either end it with ".java" or possibly "*" > > to get the java classes you're interested in.

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

For jacoco: use this properties:

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

Solution 6 - Java

I think you 're looking for the solution described in this answer https://stackoverflow.com/questions/951569/exclude-code-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

Solution 7 - Java

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> 

Solution 8 - Java

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/*.*

Solution 9 - Java

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDmitrii BorovoiView Question on Stackoverflow
Solution 1 - JavabarfuinView Answer on Stackoverflow
Solution 2 - JavarogerdpackView Answer on Stackoverflow
Solution 3 - JavametatronView Answer on Stackoverflow
Solution 4 - JavaDeepikaView Answer on Stackoverflow
Solution 5 - JavaKhaouiniView Answer on Stackoverflow
Solution 6 - JavappapapetrouView Answer on Stackoverflow
Solution 7 - JavaCutberto OcampoView Answer on Stackoverflow
Solution 8 - JavaSalvatore Pannozzo CapodiferroView Answer on Stackoverflow
Solution 9 - JavaCraceInfotech-NatarajanView Answer on Stackoverflow