使用Gradle和Kotlin DSL配置Jacoco [英] Configure Jacoco with gradle and kotlin DSL

查看:221
本文介绍了使用Gradle和Kotlin DSL配置Jacoco的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Jacoco配置为从分析中排除某些类,但是找不到任何有效的示例:(

I'm trying to configure Jacoco to exclude some classes from analysis but can't find any working example :(

我找到了一些afterEvaluate的样本,但没有成功

I found some samples with afterEvaluate but no success

推荐答案

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会产生以下报告

using Gradle 5.4.1 execution of gradle test jacocoTestReport produces following report

添加到build.gradle.kts

tasks.withType<JacocoReport> {
  classDirectories.setFrom(
    sourceSets.main.get().output.asFileTree.matching {
      exclude("org/example/B.class")
    }
  )
}

执行同一命令会产生以下报告

execution of the same command produces following report

这篇关于使用Gradle和Kotlin DSL配置Jacoco的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆