84

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>
barfuin
  • 16,865
  • 10
  • 85
  • 132
Dmitrii Borovoi
  • 2,814
  • 9
  • 32
  • 50

10 Answers10

116

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

barfuin
  • 16,865
  • 10
  • 85
  • 132
  • 1
    For gradle it is sonarRunner { sonarProperties { property "sonar.coverage.exclusions", " '**/domain/**' , '**/facade/**' , '**/stub/**' , '**/model/**' , '**/config/**' " property "sonar.exclusions", "**/stub/**" } } – shane lee Apr 23 '15 at 04:06
  • @Thomas But it seem this does not fork for integration tests. – Jigar Shah Nov 04 '15 at 13:02
  • 1
    I can't find `sonar.coverage.exclusions` anywhere in the docs, only `sonar.exclusions` is mentioned. – Michael Técourt Feb 11 '16 at 16:45
  • 2
    @MichaelTecourt - Its here [SonarQube analysis exclusions & inclusions](https://docs.sonarqube.org/display/SONAR/Analysis+Parameters#AnalysisParameters-Exclusions/Inclusions) **Extract from the documentation:** sonar.coverage.exclusions - Comma-delimited list of file path patterns to be excluded from coverage calculations – Venkat Feb 03 '17 at 07:27
  • 3
    The proper syntax to ignore foo package completely is: `**/foo/**/*` – Heezer Feb 28 '18 at 16:41
  • How to set the same in gradle? @Thomas Jensen – Ankita Apr 13 '18 at 03:14
  • 1
    @Venkat it looks like that property is no longer part of the documentation. Can you confirm that the `sonar.exclusions` property is now the correct one to use for this, and that it only excludes the file(s) from coverage, and not Issues or Duplications? – Andrew Roth Mar 10 '19 at 16:34
  • 4
    @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: https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/ – Venkat Mar 12 '19 at 02:02
  • 4
    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` – ChW Feb 18 '20 at 20:53
  • 1
    It's worth noting that this path should start from the root of your project, not your sources root, if they are different. For example, `src/main/java/com/company/test/App.java` rather than `com/company/test/App.java` – Trenton Telge Feb 18 '22 at 21:53
42

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.

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • 10
    I was also looking at this, but this excludes the files from every metric (documented API, rule compliance, ...) and not only test coverage. – g00glen00b Jun 11 '14 at 09:39
  • Yeah that's what we wanted in our case. You might be able to add it to like pmd/findbugs or jacoco exclusions elsewise [?] – rogerdpack May 28 '15 at 15:19
  • 7
    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" – Lonzak Jun 21 '17 at 11:18
  • 1
    Doesn't work for me. sonar.coverage.exclusions did the trick – denu Dec 15 '22 at 14:47
15

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
metatron
  • 551
  • 6
  • 7
9

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
Deepika
  • 438
  • 3
  • 6
6

For jacoco: use this properties:

-Dsonar.jacoco.excludes=**/*View.java
Khaouini
  • 69
  • 1
  • 2
  • 5
    `sonar.jacoco.excludes` is no longer supported (as of SonarQube 4.5.1). See [here](http://stackoverflow.com/a/27133765/1005481) for the "new" configuration. – barfuin Nov 25 '14 at 18:11
3

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/*.*
Dharman
  • 30,962
  • 25
  • 85
  • 135
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

Community
  • 1
  • 1
ppapapetrou
  • 1,653
  • 9
  • 13
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> 
2

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

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.

Shirish Singh
  • 797
  • 7
  • 17