如何让Sonar忽略一些代码覆盖度量的类?

2022-08-31 13:26:21

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

但它没有帮助

更新

我有一个正确的科贝图拉个人资料。我试图将其添加到声纳配置文件中,但我仍然有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>

答案 1

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

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

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

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


答案 2

对我来说,这是有效的(基本上是级别的全局属性):pom.xml

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

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

看起来你可以用“.java”或“*”结束它。

以获取您感兴趣的 Java 类。


推荐