首页 > 解决方案 > 如何使用 Gradle Kotlin DSL 设置 codecov?

问题描述

我已阅读codecov/example-gradle,但不确定如何将其转换为 Kotlin DSL。

我的.travis.yml

language: java
jdk:
- openjdk11
before_install:
- chmod +x gradlew
- chmod +x gradle/wrapper/gradle-wrapper.jar
script:
- ./gradlew test
- ./gradlew codeCoverageReport
after_success:
- bash <(curl -s https://codecov.io/bash)

我的build.gradle.kts

import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.2.71"
    jacoco
    maven
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-engine:5.3.1")
}

tasks {
    "test"(Test::class) {
        useJUnitPlatform()
    }

    // Task with name 'codeCoverageReport' not found in root project ''.
    "codeCoverageReport"(JacocoReport::class) {
        executionData(fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec"))

        subprojects.onEach {
            sourceSets(it.sourceSets.main)
        }

        reports {
            xml.isEnabled = true
            xml.destination = File("$buildDir/reports/jacoco/report.xml")
            html.isEnabled = false
            csv.isEnabled = false
        }

        dependsOn("test")
    }
}

标签: gradlekotlincode-coveragegradle-kotlin-dslcodecov

解决方案


这是带有 gradle kotlin dsl 的 codecov 的最小工作示例:

.travis.yml:

language: java
jdk:
- openjdk11
before_install:
- chmod +x gradlew
- chmod +x gradle/wrapper/gradle-wrapper.jar
script:
- ./gradlew test build
- ./gradlew codeCoverageReport
after_success:
- bash <(curl -s https://codecov.io/bash)

build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.2.71"
    jacoco
    java
}

val junit5Version = "5.3.1"
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion

// This might not be needed in the future, but as of present the default version bundled with the latest version of gradle does not work with Java 11
jacoco {
    toolVersion = "0.8.2"
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
    testImplementation("org.junit.jupiter:junit-jupiter-engine:$junit5Version")
}

tasks {
    "test"(Test::class) {
        useJUnitPlatform()
    }

    val codeCoverageReport by creating(JacocoReport::class) {
        executionData(fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec"))

        subprojects.onEach {
            sourceSets(it.sourceSets["main"])
        }

        reports {
            sourceDirectories =  files(sourceSets["main"].allSource.srcDirs)
            classDirectories =  files(sourceSets["main"].output)
            xml.isEnabled = true
            xml.destination = File("$buildDir/reports/jacoco/report.xml")
            html.isEnabled = false
            csv.isEnabled = false
        }

        dependsOn("test")
    }
}

推荐阅读