简体   繁体   English

Gradle在Jenkins中构建JaCoCo jacocoTestReport时排除预构建的jar和类

[英]Gradle Exclude pre-built jars and classes while building JaCoCo jacocoTestReport in Jenkins

I have a project build based on Gradle. 我有一个基于Gradle的项目构建。 Now I added a lot of Unit Tests to it, and trying to integrate it into Jenkins to run them while building the project, and generate Code Coverage report (this is where JaCoCo comes into play). 现在,我向其中添加了许多单元测试,并尝试将其集成到Jenkins中以在构建项目时运行它们,并生成代码覆盖率报告(这是JaCoCo发挥作用的地方)。

I have the following config in build.gradle: 我在build.gradle中有以下配置:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: "jacoco"

buildDir "out"

repositories {
    maven {
        url "https://internal.corporate.repo.com/"
    }

    flatDir{
        dirs 'resources/lib'
    }

}

sourceSets {
    main {
        java {
            srcDir 'src/'
        }
        resources {
            srcDir 'resources/'
        }
    }
}

jacoco {
    toolVersion = "0.7.5.201505241946"
}

dependencies {
    compile 'org.apache.ant:ant-launcher:1.9.7'
    compile 'org.jdom:jdom:2.0.2'
    compile 'joda-time:joda-time:2.9.3'
    compile 'com.sun.mail:javax.mail:1.5.5'
    compile 'org.json:json:20160212'
    compile 'org.apache.wink:wink-client:1.4'
    compile 'org.apache.wink:wink-common:1.4'
    compile 'org.apache.wink:wink-json-provider:1.4'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0.1' 
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'org.apache.ant:ant:1.9.7'
    compile 'org.slf4j:slf4j-api:1.7.25'
    compile 'org.apache.logging.log4j:log4j-api:2.9.0'
    compile 'org.apache.logging.log4j:log4j-core:2.9.0'
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.9.1'
    compile 'javax.activation:activation:1.1.1'
    compile 'com.opencsv:opencsv:3.7'
    compile 'junit:junit:4+'
    compile name:'ant-soatest'  
    compile name:'ant-junit'
    compile 'commons-cli:commons-cli:1.3.1'
    compile 'org.apache.ant:ant-junit4:1.9.7'
    compile 'commons-io:commons-io:2.5'
    compile 'com.oracle.jdbc:ojdbc7:12.1.0.2'

    compile 'org.apache.commons:commons-configuration2:2.1.1'
    compile 'commons-beanutils:commons-beanutils:1.9.3'
    compile 'com.sun.jersey:jersey-client:1.8'
    compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.8.5'
    compile 'jaxen:jaxen:1.1.6'

}

 jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled true
        html.destination file("${buildDir}/jacocoHtml")
    }
}

test {
    jacoco {
        excludes = ['**/*.class']
    }
    testLogging.showStandardStreams = true 
 }

So I have two problems: 所以我有两个问题:

  1. When running the tests task, the outputted test.exec shows 0% coverage while I import it to Coverage tab in Eclipse. 运行测试任务时,输出的test.exec在将其导入Eclipse中的Coverage选项卡时显示0%的覆盖率。 Even though I know tests were run, the Gradle testing result shows 3 successful tests, this is the reason I moved on to the second attempt to actually get coverage results 尽管我知道测试已经运行,但Gradle测试结果显示3个成功的测试,这就是我继续进行第二次实际获取覆盖率结果的原因
  2. jacocoTestReport task - this one ends up with the following errors currently: jacocoTestReport任务-当前此任务最终出现以下错误:

14:18:03.517 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) 14:18:03.517 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: : Error while creating report 14:18:03.517 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.concurrent.StoppableExecutorImpl $ 1.run(StoppableExecutorImpl.java:46)14:18:03.517 [ERROR] [org。 gradle.internal.buildevents.BuildExceptionReporter]原因:创建报告时出错

followed by: 其次是:

14:18:03.520 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: java.io.IOException: Error while analyzing class /bld/workspace/some/DEV/really/long/obscure/pathname/appname/out/resources/main/testscripts/Cucumber/target/RenamedCommonDependencies.jar@oracle/jdbc/proxy/oracle$1jdbc$1replay$1driver$1NonTxnReplayableArray$2java$1sql$1Array$$$Proxy.class. 14:18:03.520 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]原因:java.io.IOException:分析类/ bld / workspace / some / DEV / really / long / obscure / pathname / appname时出错/out/resources/main/testscripts/Cucumber/target/RenamedCommonDependencies.jar@oracle/jdbc/proxy/oracle$1jdbc$1replay$1driver$1NonTxnReplayableArray$2java$1sql$1Array$$$Proxy.class。

and finally followed by: 最后是:

Caused by: java.lang.IllegalStateException: Can't add different class with same name: oracle/jdbc/proxy/oracle$1jdbc$1replay$1driver$1NonTxnReplayableArray$2java$1sql$1Array$$$Proxy 14:18:03.521 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106) 造成原因:java.lang.IllegalStateException:无法添加具有相同名称的其他类:oracle / jdbc / proxy / oracle $ 1jdbc $ 1replay $ 1driver $ 1NonTxnReplayableArray $ 2java $ 1sql $ 1Array $$$ Proxy 14:18:03.521 [错误] [org.gradle.internal.buildevents.BuildExceptionReporter]在org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106)

I have tried everything I could think of, last looking at Exclude package from jacoco code coverage for Android and JaCoCo gradle plugin exclude I tried to change the excludes path to absolute using /bld/workspace/some/DEV/really/long/obscure/pathname/appname/out/resources/main/testscripts/Cucumber/target/RenamedCommonDependencies.jar but same error 我已经尝试了所有我能想到的一切,最后查看了针对AndroidJaCoCo gradle插件的 jacoco代码覆盖中的Exclude包,我尝试使用/bld/workspace/some/DEV/really/long/obscure/pathname/appname/out/resources/main/testscripts/Cucumber/target/RenamedCommonDependencies.jar但存在相同错误

PLEASE help.. I am wasting 99% of my development time dealing with configuration, only 1% writing code... :( 请帮助..我浪费了我99%的开发时间来处理配置,只浪费了1%的代码... :(

I think that the task setup is not correct shold be something like 我认为任务设置不正确,应该是这样的

apply plugin: "application"
apply plugin: "jacoco"

mainClassName = "org.gradle.MyMain"

jacoco {
    applyTo run
}

task applicationCodeCoverageReport(type:JacocoReport){
    executionData run
    sourceSets sourceSets.main
}

Where in main class you put your class name, cause you don't need code coverage for all the system libs.. You can see an Example here: https://github.com/codecov/example-gradle 在主类中的您放置类名的地方,因为您不需要覆盖所有系统库的代码。.您可以在此处查看示例: https : //github.com/codecov/example-gradle

Example and info can be found here: https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:configuring_the_jacoco_plugin 示例和信息可以在这里找到: https : //docs.gradle.org/current/userguide/jacoco_plugin.html#sec : configuring_the_jacoco_plugin

However, I think that you had bet on the wrong tool ... Check out http://pitest.org/ 但是,我认为您押错了工具……请访问http://pitest.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM