我在 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 个人资料。我尝试将其添加到声纳配置文件中,但我仍然有 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>
在撰写本文时(使用 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 →排除 → 代码覆盖率)。在输入字段下方,它给出了上面提到的属性名称(“Key:sonar.coverage.exclusions”)。
对我来说这很有效(基本上是
pom.xml
级别的全局属性):
<properties>
<sonar.exclusions>**/Name*.java</sonar.exclusions>
</properties>
根据:http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-Patterns
看来你可以用“.java”或“*”结束
获取您感兴趣的java类。
根据 this SonarQube 7.1 文档
sonar.exclusions - 以逗号分隔的文件路径模式列表 排除在分析之外 sonar.coverage.exclusions - 逗号分隔 要从 coverage 排除的文件路径模式列表 计算
这个文档给出了一些关于如何创建路径模式的示例
# 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,则可以像这样传递 Maven 命令行参数
-Dsonar.coverage.exclusions=**/config/*,**/model/*
我在明确排除单个类别时遇到问题。以下是我的观察:
**/*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
当使用 sonar-scanner 进行 swift 时,请在 sonar-project.properties 中使用 sonar.coverage.exclusions 来排除任何文件,以仅实现代码覆盖。如果您还想从分析中排除文件,可以使用 sonar.exclusions。这对我来说很快就有用了
sonar.coverage.exclusions=**/*ViewController.swift,**/*Cell.swift,**/*View.swift
对于 jacoco:使用此属性:
-Dsonar.jacoco.excludes=**/*View.java
我不得不挣扎一点,但我找到了解决方案,我补充说
-Dsonar.coverage.exclusions=**/*.*
并且覆盖率指标完全从仪表板中取消,所以我意识到问题在于我经过的路径,而不是属性。就我而言,排除的路径是 java/org/acme/exceptions,所以我使用了:
`-Dsonar.coverage.exclusions=**/exceptions/**/*.*`
它有效,但由于我没有子文件夹,它也适用于
-Dsonar.coverage.exclusions=**/exceptions/*.*
我认为您正在寻找此答案中描述的解决方案使用 Cobertura 从代码覆盖率中排除方法 请记住,如果您使用 Sonar 3.2,您已指定您的覆盖工具是 cobertura 而不是 jacoco(默认),后者还不支持此类功能
有时,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>
此外,您还可以包括以下声纳配置:
<properties>
<sonar.exclusions>
**/domain/*.java,
**/transfer/*.java
</sonar.exclusions>
</properties>
我能够通过更新 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>
如果您正在寻找排除并使用 Gradle。
在 build.gradle 文件中添加以下行:
sonarqube {
properties {
property "sonar.exclusions", "'**/example/your/service/impl/**',"
} }
注意内部单引号。祝你好运。