java和travis-ci的问题,代码覆盖率和gradle

时间:2018-10-24 16:50:03

标签: gradle code-coverage travis-ci codacy

我曾在多个地方尝试过如何使用gradle配置travis-ci中Codacy的覆盖范围。

我发现如何在没有travis-CI的情况下配置Gradle。我想自动做些事情。 有人可以帮忙吗?

添加:

我的build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}


plugins {
    id "org.sonarqube" version "2.6"
    id 'com.gradle.build-scan' version '1.16'
}
configurations { codacy }
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.rjdesenvolvimento'
version = '0.0.1'
sourceCompatibility = 1.11

repositories {
    mavenCentral()
}

tasks.withType(Test) { enabled = false }

dependencies {
    testImplementation 'junit:junit:4.12'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.6.Final'
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-security')
    implementation('org.springframework.boot:spring-boot-starter-web')
    runtimeOnly('org.springframework.boot:spring-boot-devtools')
    runtimeOnly('org.postgresql:postgresql')
    annotationProcessor("org.projectlombok:lombok:1.18.2")
    compileOnly('org.projectlombok:lombok')
    compileOnly('org.sonarsource.sonarqube:sonar-plugin-api:7.3')
    compileOnly('org.jacoco:org.jacoco.core:0.8.2')
    compileOnly('org.jacoco:org.jacoco.agent:0.8.2')
    compileOnly('com.codacy:codacy-coverage-reporter:5.0.310')
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    testImplementation('org.springframework.security:spring-security-test')
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
    codacy 'com.github.codacy:codacy-coverage-reporter:-SNAPSHOT'
}

buildScan {
    termsOfServiceUrl = 'https://gradle.com/terms-of-service'
    termsOfServiceAgree = 'yes'
}

task sendCoverageToCodacy(type: JavaExec, dependsOn: jacocoTestReport) {
    main = "com.codacy.CodacyCoverageReporter"
    classpath = configurations.codacy
    args = [
            "report",
            "-l",
            "Java",
            "-r",
            "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
    ]
}

jacocoTestReport{
    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/reports/jacoco/html"
    }
    executionData = files('build/jacoco/test.exec')
}

test {
    finalizedBy jacocoTestReport
}

和travis.yml

env:
- CODACY_PROJECT_TOKEN=c8f9b1f6aab444018dd4e3...
language: java
install: true

jdk:
- oraclejdk11
before_install:
- chmod +x gradlew
- sudo apt-get install jq
- wget -O ~/codacy-coverage-reporter-assembly-latest.jar $(curl https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r '.assets[0].browser_download_url')

dist: trusty
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/
addons:
sonarcloud:
  organization: "rjdesenvolvimento-github"
  token:
secure: 5150fa5cbf1f86006a140e0b8a96e...
script:
- ./gradlew build --scan -s sonarqube -Dsonar.projectKey=rjdesenvolvimento_apipessoas -Dsonar.organization=rjdesenvolvimento-github -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=5150fa5cbf1f86006a140e0b8a96e...

after_success:
- bash <(curl -s https://codecov.io/bash)>
  • java -jar〜/ codacy-coverage-reporter-assembly-latest.jar报告-l Java -r build / reports / jacoco / test / jacocoTestReport.xml

还有travis-ci

> Task :compileJava
> Task :processResources
> Task :classes
> Task :bootJar
> Task :jar SKIPPED
> Task :assemble
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test SKIPPED
> Task :jacocoTestReport SKIPPED
> Task :check UP-TO-DATE
> Task :build
> Task :sonarqube
$ java -jar ~/codacy-coverage-reporter-assembly-latest.jar report -l Java -r build/reports/jacoco/test/jacocoTestReport.xml
10/24 23:51:49 INFO  c.c.rules.ConfigurationRules:101 - Using API base URL: https://api.codacy.com 
10/24 23:51:49 ERROR c.c.CodacyCoverageReporter$:28 - File /home/travis/build/rjdesenvolvimento/apipessoas/build/reports/jacoco/test/jacocoTestReport.xml does not exist. 

到目前为止,尚无报道

2 个答案:

答案 0 :(得分:1)

您是否尝试过按照此处所述创建Gradle任务? https://github.com/codacy/codacy-coverage-reporter/blob/master/README.md#gradle-task

答案 1 :(得分:1)

该解决方案似乎存在于文档中。

首先,您需要执行gradle任务。 https://github.com/codacy/codacy-coverage-reporter/blob/master/README.md#gradle-task

似乎有两个示例

然后,您应该更新travis-ci步骤,以在每次运行时自动发送覆盖率报告。与https://github.com/codacy/codacy-coverage-reporter/blob/master/README.md#travis-ci一起使用,但是使用了您刚定义的gradle任务