Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying Jacoco in Gradle Multiproject

Tags:

gradle

jacoco

I use Jacoco in some of my Gradle implementations and decide improve its usage in multiprojects.

I created an structure similar to

parent-project
  - model-project
  - service-project

In my build.gradle parent-project I defined:

allprojects {
  apply plugin: 'jacoco'
  //apply other plugins

  repositories {...}

  test.finalizedBy(project.tasks.jacocoTestReport)

  jacocoTestReport {

    reports {

      csv.enabled false

      html{
        enabled true
        destination "${buildDir}/reports/jacoco"
      }

      xml.enabled false
    }

    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
  }

  //other configurations

  buildscript {

    repositories {

      jcenter()
      mavenCentral()
      mavenLocal()
    }

    dependencies {
      classpath 'org.ajoberstar:gradle-jacoco:0.2.0'
    }
  }
}

When I executed this code all the time I received:

jacocoTestReport SKIPPED

So I found this discussion where I realized that

jacocoRootReport doesn't work if some subprojects don't have any tests at all because this causes the onlyIf of JacocoReport to be false

I tried insert the following code in the end however still doesn't work

task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
  dependsOn = subprojects.test
  additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
  sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
  classDirectories = files(subprojects.sourceSets.main.output)
  executionData = files(subprojects.jacocoTestReport.executionData)
  reports {
    html.enabled = true
    xml.enabled = true
    csv.enabled = false
  }
  onlyIf = {
    true
  }
  doFirst {
    executionData = files(executionData.findAll {
        it.exists()
    })
  }
}

I also tried create an test in service-project... still doesn't work and the message of skipping remains

How can I solve this?

like image 514
arthurfnsc Avatar asked Oct 11 '25 15:10

arthurfnsc


2 Answers

It was an incorrect configuration. As far as I am using TestNG instead JUnit I needed specify it in my build.gradle

So my parent configuration was change to:

subprojects {

  apply plugin: 'jacoco'
  //apply other plugins

  def coverageSourceDirs = [
    'src/main/java'
  ]

  repositories {...}

  jacoco {
    toolVersion = '0.7.5+'
  }

  jacocoTestReport {

    additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)
    classDirectories =  files(sourceSets.main.output)
    sourceDirectories = files(sourceSets.main.allSource.srcDirs)

    reports {

      csv.enabled false

      html{
        enabled true
        destination "${buildDir}/reports/jacoco"
      }

      xml.enabled false
    }
  }

  test {
    useTestNG()
  }

  test.finalizedBy(project.tasks.jacocoTestReport)

  dependencies {

    testCompile "org.testng:testng:$testNGVersion"
    testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
    testCompile "org.springframework:spring-test:$springVersion"
  }

  task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {

    additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(subprojects.sourceSets.main.output)
    dependsOn = subprojects.test
    executionData = files(subprojects.jacocoTestReport.executionData)
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)

    reports {

      csv.enabled false

      html{
        enabled true
        destination "${buildDir}/reports/jacoco"
      }

      xml.enabled false
    }

    onlyIf = {
      true
    }

    doFirst {
      executionData = files(executionData.findAll {
        it.exists()
      })
    }
  }
}
like image 156
arthurfnsc Avatar answered Oct 15 '25 07:10

arthurfnsc


JaCoCo Report Aggregation plugin was introduced in gradle 7.4.2.

It aggregates the results of multiple JaCoCo code coverage reports into a single one.

Use testCodeCoverageReport task and don't forget to declare jacocoAggregation dependencies.

like image 25
Nolequen Avatar answered Oct 15 '25 09:10

Nolequen