Search code examples
gradlekotlincode-coveragegradle-kotlin-dslcodecov

How do I set up codecov with Gradle Kotlin DSL?


I have read codecov/example-gradle and I am uncertain how to convert it to the Kotlin DSL.

My .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)

My 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")
    }
}

Solution

  • This is a minimum working example of codecov with gradle kotlin dsl:

    .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")
        }
    }