【问题标题】:How do I set up codecov with Gradle Kotlin DSL?如何使用 Gradle Kotlin DSL 设置 codecov?
【发布时间】:2018-10-01 07:52:30
【问题描述】:

我已阅读 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 code-coverage gradle-kotlin-dsl codecov


    【解决方案1】:

    这是带有 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")
        }
    }
    

    【讨论】:

      【解决方案2】:

      对我来说,与 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 2019-07-10
        • 2020-01-18
        • 2018-09-13
        相关资源
        最近更新 更多