使用 Lombok、Gradle、Jacoco 和 Sonar 过滤覆盖范围
Filtering coverage with Lombok, Gradle, Jacoco and Sonar
我在我的 Java 项目中使用 Gradle 4.4 和 JaCoCo 0.8.0,并在 SonarJava 5.0.1 中使用 Sonar。
我有一个用 lombok 的 @Value
和 @Builder
注释的 class。
我在 build.gradle 中的 JaCoCo 配置如下所示:
jacoco {
toolVersion = "0.8.0"
reportsDir = file("$buildDir/reports/jacoco")
}
jacocoTestReport.doFirst{
classDirectories = files("buildDir/classes")
}
task jacocoReport(type: JacocoReport){
sourceSets sourceSets.main
executionData test, integrationTest
}
此外,我有 lombok.confi 文件,带有 属性 lombok.addLombokGeneratedAnnotation = true
,并且生成的内容实际上在 build/classes.[=16 中有 @lombok.Generated
注释=]
然而,Sonar 的覆盖率仍然很低。它报告了大量要涵盖的条件和行。
如announcement of release of JaCoCo version 0.8.0所述:
Please Note
Tools that directly read exec files (which is not a final report) and embed JaCoCo for generation of report will provide filtering functionality only after they updated to this version of JaCoCo.
So please follow/wait/etc respective vendors such as
SonarQube - https://jira.sonarsource.com/browse/SONARJAVA-2608
Reports generated by corresponding version (0.8.0) of integrations developed as part of JaCoCo project by us (Ant Tasks, Maven Plugin and Command Line Interface) provide filtering functionality.
With Gradle JaCoCo Plugin you can select both runtime and version for "JaCoCoReport" task using "toolVersion" - https://docs.gradle.org/current/userguide/jacoco_plugin.html
这在JaCoCo changelog中也有说明:
Note: Tools that directly read exec files and embed JaCoCo for this (such as SonarQube or Jenkins) will provide filtering functionality only after they updated to this version of JaCoCo.
截至今天(2018 年 1 月 29 日),https://jira.sonarsource.com/browse/SONARJAVA-2608 的修复应该在尚未发布的 SonarJava 插件版本 5.1 中。
综上所述:Gradle生成的报告应该已经被过滤,SonarQube生成的报告将在SonarJava升级后被过滤。
除了配置 lombok 之外,我还必须告诉声纳扫描仪在哪里可以找到依赖项。是摆脱 SonarQube 中 "Unused private fields should be removed" 消息的唯一方法。
我遵循了此站点的说明:https://netwolfuk.wordpress.com/2017/10/29/maven-sonarqube-jacoco-lombok-and-teamcity/
归结为:
告诉 maven 将其依赖项复制到 Sonar 可以找到它们的地方。这很简单,只需添加:
dependency:copy-dependencies
将以下内容添加到声纳构建步骤中:
-Dsonar.java.libraries=target/dependency/*.jar
我在我的 Java 项目中使用 Gradle 4.4 和 JaCoCo 0.8.0,并在 SonarJava 5.0.1 中使用 Sonar。
我有一个用 lombok 的 @Value
和 @Builder
注释的 class。
我在 build.gradle 中的 JaCoCo 配置如下所示:
jacoco {
toolVersion = "0.8.0"
reportsDir = file("$buildDir/reports/jacoco")
}
jacocoTestReport.doFirst{
classDirectories = files("buildDir/classes")
}
task jacocoReport(type: JacocoReport){
sourceSets sourceSets.main
executionData test, integrationTest
}
此外,我有 lombok.confi 文件,带有 属性 lombok.addLombokGeneratedAnnotation = true
,并且生成的内容实际上在 build/classes.[=16 中有 @lombok.Generated
注释=]
然而,Sonar 的覆盖率仍然很低。它报告了大量要涵盖的条件和行。
如announcement of release of JaCoCo version 0.8.0所述:
Please Note
Tools that directly read exec files (which is not a final report) and embed JaCoCo for generation of report will provide filtering functionality only after they updated to this version of JaCoCo.
So please follow/wait/etc respective vendors such as SonarQube - https://jira.sonarsource.com/browse/SONARJAVA-2608
Reports generated by corresponding version (0.8.0) of integrations developed as part of JaCoCo project by us (Ant Tasks, Maven Plugin and Command Line Interface) provide filtering functionality.
With Gradle JaCoCo Plugin you can select both runtime and version for "JaCoCoReport" task using "toolVersion" - https://docs.gradle.org/current/userguide/jacoco_plugin.html
这在JaCoCo changelog中也有说明:
Note: Tools that directly read exec files and embed JaCoCo for this (such as SonarQube or Jenkins) will provide filtering functionality only after they updated to this version of JaCoCo.
截至今天(2018 年 1 月 29 日),https://jira.sonarsource.com/browse/SONARJAVA-2608 的修复应该在尚未发布的 SonarJava 插件版本 5.1 中。
综上所述:Gradle生成的报告应该已经被过滤,SonarQube生成的报告将在SonarJava升级后被过滤。
除了配置 lombok 之外,我还必须告诉声纳扫描仪在哪里可以找到依赖项。是摆脱 SonarQube 中 "Unused private fields should be removed" 消息的唯一方法。
我遵循了此站点的说明:https://netwolfuk.wordpress.com/2017/10/29/maven-sonarqube-jacoco-lombok-and-teamcity/
归结为:
告诉 maven 将其依赖项复制到 Sonar 可以找到它们的地方。这很简单,只需添加:
dependency:copy-dependencies
将以下内容添加到声纳构建步骤中:
-Dsonar.java.libraries=target/dependency/*.jar