Code coverage come after test and integration test, but integration test may not exist

I am trying to write 1 parent build.gradle to be included in all our individual projects to apply code coverage.
This is my problem only about 25% of our projects have integration test but where we do we obviously want them included in our code coverage metrics. We are using jacoco as it comes built in.

I currently have

    jacocoTestReport {
      dependsOn integrationTest // tests are required to run before generating the report
      executionData test, integrationTest
      sourceSets sourceSets.main
      reports {

    xml.enabled false
    csv.enabled false
    html.destination layout.buildDirectory.dir('jacocoHtml').get().asFile

but that fails if integration test doesn’t exist.

but if I make it depend upon just test I get this build error:

    * What went wrong:
    Execution failed for task ':jacocoTestReport'.
    > Unable to read execution data file ...\build\jacoco\integrationTest.exec


If you plan to use latest version of Gradle, then I’d advise to take a look at the samples. This use case should be covered here:

In addition, the idiomatic gradle repository can provide some nice patterns to structure build logic:

Unfortunatly we are on an old version 4.10.3


Then you may look here :

The magic happens in the parent.gradle with the whenTaskAdded listener.

Please note that while it is possible to scale this solution to multi-projects builds with Gradle 4.10, it requires few tricks here and there to achieve the desired output. It may be easier to aggregate test execution data before creating the report in some case. There are plugins handling that part you can leverage to keep your build scripts simpler.