Gradle jacoco coverage report with more than one submodule(s)?

后端 未结 5 1465
攒了一身酷
攒了一身酷 2021-01-02 11:35

Does anybody know how to configure a gradle file for java jacoco report that contain codecoverage of more than one gradle submodule?

my current approach only shows c

相关标签:
5条回答
  • 2021-01-02 12:14

    Finally I found this plugin: https://github.com/palantir/gradle-jacoco-coverage that did the job for me:

    root gradle.build

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            // see https://jcenter.bintray.com/com/android/tools/build/gradle/
            classpath 'com.android.tools.build:gradle:2.1.0'
            // classpath 'com.android.tools.build:gradle:2.2.0-alpha1'
    
            // https://github.com/palantir/gradle-jacoco-coverage
            classpath 'com.palantir:jacoco-coverage:0.4.0'      
        }
    }
    
    // https://github.com/palantir/gradle-jacoco-coverage
    apply plugin: 'com.palantir.jacoco-full-report'
    

    all subprojects that has

    apply plugin: 'jacoco'
    

    are included in the report.

    0 讨论(0)
  • 2021-01-02 12:14

    You can create a merged report without merged exec file. Create a new task to the root of build.gradle with following content.

    task jacocoReport(type: JacocoReport) {
        for (p in allprojects) {
            def testTask = p.tasks.findByName("test")
            if (testTask != null)
                dependsOn(testTask)
    
            executionData.setFrom(file("${p.buildDir}/jacoco/test.exec"))
            classDirectories.from(file("${p.buildDir}/classes/java/main"))
        }
    }
    
    0 讨论(0)
  • 2021-01-02 12:21

    One possible solution (with some sonar specific parts):

    def getJacocoMergeTask(Project proj){
        def jmClosure =  {
            doFirst {
                logger.info "${path} started"
                executionData.each { ed ->
                    logger.info "${path} data: ${ed}"
                }
            }
            onlyIf {
                executionData != null && !executionData.isEmpty()
            }
        }
    
        def jacocoMerge = null
        if(!proj.tasks.findByName('jacocoMerge')){
    
            jacocoMerge = proj.tasks.create('jacocoMerge', JacocoMerge.class)
            jacocoMerge.configure jmClosure
    
            // sonar specific part
            proj.rootProject.tasks["sonarqube"].mustRunAfter jacocoMerge
    
            proj.sonarqube {
                properties {
                    property "sonar.jacoco.reportPaths", jacocoMerge.destinationFile.absolutePath
                }
            }
            // end of sonar specific part
    
            logger.info "${jacocoMerge.path} created"
        } else {
            jacocoMerge = proj.tasks["jacocoMerge"]
        }
        jacocoMerge
    }
    
    
    afterEvaluate { project ->
        def jacocoMerge = getJacocoMergeTask(project)
    
        project.tasks.withType(Test) { task ->
            logger.info "${jacocoMerge.path} cfg: ${task.path}"
    
            task.finalizedBy jacocoMerge
            jacocoMerge.dependsOn task
    
            task.doLast {
                logger.info "${jacocoMerge.path} executionData ${task.path}"
                jacocoMerge.executionData task
            }
    
            def cfg = configurations.getByName("${task.name}Runtime")
            logger.info "${project.path} process config: ${cfg.name}"
    
            cfg.getAllDependencies().withType(ProjectDependency.class).each { pd ->
                def depProj = pd.dependencyProject
                logger.info "${task.path} dependsOn ${depProj.path}"
                def jm = getJacocoMergeTask(depProj)
    
                task.finalizedBy jm
                jm.dependsOn task
    
                task.doLast {
                    logger.info "${jm.path} executionData ${task.path}"
                    jm.executionData task
                }
            }
        }
    }
    

    This will merge all the executionData from all the projects, that used a certain project during testing as a dependency.

    0 讨论(0)
  • 2021-01-02 12:24

    This works for me

    plugins {
        id 'org.kordamp.gradle.jacoco' version '0.29.0'
    }
    
    config {
        jacoco {
            enabled
            mergeExecFile
            mergeReportHtmlFile
            mergeReportXmlFile
            additionalSourceDirs
            additionalClassDirs
        }
    }
    

    https://kordamp.org/kordamp-gradle-plugins/#_org_kordamp_gradle_jacoco

    0 讨论(0)
  • 2021-01-02 12:32

    You will need to create a new task of type JacocoMerge that aggregates the JaCoCo reports from all subprojects. See a similar discussion in this post.

    0 讨论(0)
提交回复
热议问题