使用 gradle 和 kotlin DSL 配置 Jacoco
Configure Jacoco with gradle and kotlin DSL
我正在尝试配置 Jacoco 以从分析中排除一些 类 但找不到任何工作示例:(
我找到了一些样本 afterEvaluate
但没有成功
src/main/java/org/example/A.java
:
package org.example;
class A {
}
src/main/java/org/example/B.java
:
package org.example;
class B {
}
src/test/java/org/example/ExampleTest.java
:
package org.example;
public class ExampleTest {
@org.junit.Test
public void test() {
new A();
new B();
}
}
build.gradle.kts
:
plugins {
java
jacoco
}
repositories {
mavenCentral()
}
dependencies {
testCompile("junit:junit:4.12")
}
使用 Gradle 5.4.1 执行 gradle test jacocoTestReport
产生以下报告
添加后 build.gradle.kts
tasks.withType<JacocoReport> {
classDirectories.setFrom(
sourceSets.main.get().output.asFileTree.matching {
exclude("org/example/B.class")
}
)
}
执行同一命令会生成以下报告
只是添加到@Godin 的精彩回答:
@Godin 解释的方式,你必须 运行 gradle test jacocoTestReport
这还不错,但是如果你想让 jacoco 运行 当你 运行 只是使用 gradle test
将此添加到您的 build.gralde.kts:
tasks.test {
finalizedBy("jacocoTestReport")
doLast {
println("View code coverage at:")
println("file://$buildDir/reports/jacoco/test/html/index.html")
}
}
我已经通过这种方式排除了:
tasks.jacocoTestReport {
classDirectories.setFrom(
files(classDirectories.files.map {
fileTree(it) {
exclude(
"com/example/integration/**",
"com/example/application/*Ext*"
)
}
})
)
}
已拍摄 from here
我正在尝试配置 Jacoco 以从分析中排除一些 类 但找不到任何工作示例:(
我找到了一些样本 afterEvaluate
但没有成功
src/main/java/org/example/A.java
:
package org.example;
class A {
}
src/main/java/org/example/B.java
:
package org.example;
class B {
}
src/test/java/org/example/ExampleTest.java
:
package org.example;
public class ExampleTest {
@org.junit.Test
public void test() {
new A();
new B();
}
}
build.gradle.kts
:
plugins {
java
jacoco
}
repositories {
mavenCentral()
}
dependencies {
testCompile("junit:junit:4.12")
}
使用 Gradle 5.4.1 执行 gradle test jacocoTestReport
产生以下报告
添加后 build.gradle.kts
tasks.withType<JacocoReport> {
classDirectories.setFrom(
sourceSets.main.get().output.asFileTree.matching {
exclude("org/example/B.class")
}
)
}
执行同一命令会生成以下报告
只是添加到@Godin 的精彩回答:
@Godin 解释的方式,你必须 运行 gradle test jacocoTestReport
这还不错,但是如果你想让 jacoco 运行 当你 运行 只是使用 gradle test
将此添加到您的 build.gralde.kts:
tasks.test {
finalizedBy("jacocoTestReport")
doLast {
println("View code coverage at:")
println("file://$buildDir/reports/jacoco/test/html/index.html")
}
}
我已经通过这种方式排除了:
tasks.jacocoTestReport {
classDirectories.setFrom(
files(classDirectories.files.map {
fileTree(it) {
exclude(
"com/example/integration/**",
"com/example/application/*Ext*"
)
}
})
)
}
已拍摄 from here