如何让 Sonar 忽略 codeCoverage 指标的某些类?

新手上路,请多包涵

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

原文由 Dmitrii Borovoi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
1 个回答

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

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

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

为了找到您的 SonarQube 版本的属性名称,您可以尝试转到 SonarQube 实例的 General Settings 部分并查找 Code Coverage 项(在 SonarQube 4.5.x 中,即 General Settings → Exclusions → Code Coverage )。在输入字段下方,它给出了上面提到的属性名称(“Key:sonar.coverage.exclusions”)。

原文由 barfuin 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题