如何使用 Gradle Kotlin DSL 设置 codecov?

How do I set up codecov with Gradle Kotlin DSL?

我已阅读 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")
    }
}

这是使用 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")
    }
}

对我来说,与 Codecov 的集成是这样的:

.travis.yml

language: java
sudo: false
before_cache:
  - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/
install:
  - gem install pdd -v 0.20.5
  - gem install xcop -v 0.6
script:
  - set -e
  - pdd --file=/dev/null
  - ./gradlew buildPlugin
  - ./gradlew check
env:
  global:
    - JAVA_OPTS="-Xmx256m"
jdk:
  - oraclejdk8
dist: trusty
after_success: bash <(curl -s https://codecov.io/bash)

build.gradle.kts:

import org.jetbrains.intellij.tasks.PatchPluginXmlTask
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper

group = "com.aivinog1"
version = "1.0-SNAPSHOT"

buildscript {
    repositories {
        mavenCentral()
        jcenter() }
    // @todo #26:30m For now, this version and Kotlin plugin version are duplicate itself's. Needs to move it in a variable.
    dependencies { classpath(kotlin("gradle-plugin", "1.2.30")) }
}

plugins {
    id("org.jetbrains.intellij") version ("0.4.9")
    kotlin("jvm") version ("1.2.30")
    id("io.gitlab.arturbosch.detekt") version("1.0.0-RC16")
    jacoco
}

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

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

repositories {
    jcenter()
}

tasks.jacocoTestReport {
    reports {
        xml.isEnabled = true
        xml.destination  = File("$buildDir/reports/jacoco/report.xml")
        csv.isEnabled = false
        html.isEnabled = false
    }
    executionData(File("build/jacoco/test.exec"))
}

tasks.test {
    useJUnitPlatform()
    finalizedBy(tasks.jacocoTestReport)
}

intellij {
    version = "2019.1.3"

    tasks {
        withType<PatchPluginXmlTask> {
            changeNotes("")
        }
    }
}

detekt {
    toolVersion = "1.0.0-RC16"
    input = files("src/main/kotlin")
    filters = ".*/resources/.*,.*/build/.*"
}

你可以在这个 PR 上验证:https://github.com/aivinog1/0pdd-idea-plugin/pull/33