0
votes

I have a Java-Gradle project that has multiple modules, meaning multiple build.gradle files and multiple test folders. I have a main build.gralde and I succeeded to aggregate all the multi jacoco reports to one main report using the configuration below at my main build.gradle. but now I want to exclude some of packages / classes from the aggregated modules. How do I do that?

you can see also my tries below

apply plugin: 'jacoco'
apply plugin: 'java'

def otherProjects = [':module1', ':module2']

otherProjects.each {
    // ensure other projects evaluated first so sourceSets are configured
    evaluationDependsOn it
}
jacoco {
    toolVersion = "0.8.4"
    reportsDir = file("$buildDir/jacoco")
}
jacocoTestReport {
    FileTree sourceTree = files().asFileTree
    FileTree classTree = files().asFileTree
    otherProjects.each {
        sourceTree += project(it).sourceSets.main.allJava
        classTree += project(it).sourceSets.main.output.asFileTree
    }
    additionalSourceDirs = sourceTree
    additionalClassDirs = classTree
    reports {
        html.enabled true
        html.destination file("${buildDir}/jacocoHtml")
    }

// try 1
afterEvaluate {
    classDirectories = files(classDirectories.files.collect {
        fileTree(dir: it, exclude: '/com/packege_from_module1/**')
    })
}

// try 2
afterEvaluate {
    additionalSourceDirs = files(additionalSourceDirs.files.collect {
        sourceTree(dir: it, exclude: 'com/packege_from__module1/**')
    })
}

// try 3
afterEvaluate {
    additionalSourceDirs = files(additionalSourceDirs.files.collect {
        classTree(dir: it, exclude: 'com/packege_from__module1/**')
    })
}

}

1

1 Answers

0
votes

Given

a/src/main/java/A.java

class A {
}

a/src/test/java/ATest.java

import org.junit.Test;

public class ATest {
    @Test
    public void a() {
    }
}

b/src/main/java/B.java

class B {
}

b/src/test/java/BTest.java

import org.junit.Test;

public class BTest {
    @Test
    public void b() {
    }
}

settings.gradle

rootProject.name = 'example'

include 'a'
include 'b'

and build.gradle

allprojects {
    apply plugin: 'java'
    apply plugin: 'jacoco'

    jacoco {
        toolVersion = "0.8.3"
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        testCompile 'junit:junit:4.12'
    }
}

task jacocoAggregateReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
    dependsOn = subprojects.test
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(subprojects.sourceSets.main.output)
    executionData = files(subprojects.jacocoTestReport.executionData)
}

using Gradle 4.10.3 or 5.4.1 execution of gradle jacocoAggregateReport will produce following report in directory build/reports/jacoco/jacocoAggregateReport/html/index.html

before

Now let's exclude class file A.class from report by changing

    classDirectories = files(subprojects.sourceSets.main.output)

in build.gradle on

    classDirectories = files(subprojects.sourceSets.main.output).asFileTree.matching {
        exclude 'A.class'
    }

and execution of gradle jacocoAggregateReport will produce

after

Your attempts did not worked, because report is constructed for all class files from both classDirectories and additionalClassDirs - no exclusion from classDirectories and additionalClassDirs in attempts 2 and 3, no exclusion from additionalClassDirs in attempt 1.