Search code examples
javagradlepluginsjacocomulti-module

Gradle and Plugin shenanigans, for multi-module builds


So I have a multi module project which builds a lot of stuff using gradle, including some AWS Lambda's, a Java (Spring Boot) microservice, and a ReactJS webapp.

To this end I would like to generate some Jacoco code coverage reports to keep on top of things. So currently I have this:

subprojects {
    apply plugin: 'jacoco'

    plugins.withType(JavaPlugin) {
        jacoco {
            toolVersion = "0.8.5"
        }
    }
}

Which "works". However if I change it to this:

subprojects {
    plugins.withType(JavaPlugin) {
        apply plugin: 'jacoco'

        jacoco {
            toolVersion = "0.8.5"
        }
    }
}

Gradle fails hard when it tries to build the first Java project. I mean I don't really mind, it's just really weird that you have to apply the jacoco plugin to subprojects that don't have any Java code in them. Anyone?


Solution

  • See Example 3 (Applying plugins only on certain subprojects) in the documentation https://docs.gradle.org/current/userguide/plugins.html

    i.e. in your case:

    plugins {
      id 'jacoco' version '1.0.0' apply false
    }
    
    subprojects {
      if (name.startsWith('myjacostuff')) {
        apply plugin: 'jacoco'
      }
    
      plugins.withType(JavaPlugin) {
        jacoco {
          toolVersion = "0.8.5"
        }
      }
    }