如何使Sonar忽略代码Coverage指标的某些类?

时间:2012-08-27 03:31:18

标签: java maven sonarqube

我在Maven有一个Sonar个人资料。除代码覆盖率指标外,一切正常。我想让Sonar只为代码覆盖率指标忽略一些类。我有以下资料:

<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>

请帮忙。我试图添加像

这样的东西
<exclude>com/qwerty/dw/publisher/Main.class</exclude>

但它没有帮助

更新

我有一个正确的Cobertura个人资料。我试图将它添加到Sonar配置文件中,但我仍然有53%而不是像Cobertura配置文件中那样约95%

<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>

9 个答案:

答案 0 :(得分:53)

在撰写本文时(使用SonarQube 4.5.1),要设置的正确属性为sonar.coverage.exclusions,例如:

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

这似乎是之前几个版本的变化。请注意,这仅从覆盖率计算中排除给定的类。计算所有其他指标和问题。

要查找SonarQube版本的属性名称,您可以尝试转到SonarQube实例的常规设置部分,并查找代码覆盖率项(在SonarQube 4.5.x中,常规设置→排除→代码覆盖)。在输入字段下方,它给出了上面提到的属性名称(“Key:sonar.coverage.exclusions”)。

答案 1 :(得分:23)

对我们来说这很有用(基本上是pom.xml级别的属性):

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

根据http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-Patterns,你可以用“.java”或“*”来结束它,以获得你感兴趣的java类。

答案 2 :(得分:5)

对于jacoco:使用此属性:

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

答案 3 :(得分:2)

我认为您正在寻找此答案中描述的解决方案Exclude methods from code coverage with Cobertura 请记住,如果你使用的是Sonar 3.2,你已经指定你的覆盖工具是cobertura而不是jacoco(默认),它不支持这种功能

答案 4 :(得分:2)

有时,Clover配置为为所有非测试代码提供代码覆盖率报告。如果您希望覆盖这些首选项,则可以使用配置元素来排除和包含要检测的源文件:

<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>

此外,您还可以包含以下Sonar配置:

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

答案 5 :(得分:1)

使用声纳扫描仪进行快速扫描时,请在sonar-project.properties中使用sonar.coverage.exclusions排除任何文件以仅覆盖代码。如果您也想从分析中排除文件,则可以使用sonar.exclusions。这对我迅速起作用了

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

答案 6 :(得分:0)

根据SonarQube 7.1的this文档

  

sonar.exclusions -要以逗号分隔的文件路径模式列表   从分析中排除    sonar.coverage.exclusions -以逗号分隔    coverage 中要排除的文件路径模式列表   计算

document提供了一些有关如何创建路径模式的示例

# 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

如果您在项目中使用Maven,则可以像这样-Dsonar.coverage.exclusions=**/config/*,**/model/*

传递Maven命令行参数。

我在明确排除单个类时遇到问题。在我的观察之下:

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

答案 7 :(得分:0)

我不得不挣扎一点,但是我找到了解决方法,我补充了

/home/foo/bar.txt

,并且覆盖范围指标完全从仪表板上取消,所以我意识到问题出在我经过的路径,而不是属性。在我的情况下,排除的路径是java / org / acme / exceptions,所以我使用了:

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

它可以工作,但是由于我没有子文件夹,因此它也可以工作

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

答案 8 :(得分:0)

通过更新 pom.xml 中的 jacoco-maven-plugin 配置,我能够实现必要的代码覆盖排除

<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>

此配置排除了 jacoco 覆盖中的 GlobalExceptionHandler.java 和 ErrorResponse.java。

以下两行对声纳覆盖范围相同。

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