JaCoCo plugin with multi-project builds


(Hoobajoob) #1

I have an inherited source tree that I’ve tried to restructure as a multi-project build. There are 4 subprojects: A, B, C, D. The lib dependency graph is D -> C -> B -> A.

There are no test source sets except in D, which includes tests for all the classes from A, B, C, and D. This is because the tests were written in such a way that they can’t be split out such that tests for A depend only on classes from A.

So, assuming that this is a constraint: that the tests cannot be partitioned out into test source sets under A, B, C, and D; I am trying to get a JaCoCo report that includes coverage for all of the classes from A, B, and C as well as those from D when I run D’s tests.

However, I am only seeing coverage reports from classes in the main source set for project D.

How can I get a coverage report for classes from all projects when the test task only runs in project D?


(uklance) #2

TLDR; You’ll need to set additionalClassDirs and additionalSourceDirs properties on the jacocoTestReport task

eg:

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

def otherProjects = [':A', ':B', ':C']

otherProjects.each {
   // ensure other projects evaluated first so sourceSets are configured
   evaluationDependsOn it
}

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
}

(Hoobajoob) #3

Fantastic! Thank you @Lance! This worked exactly as I was hoping. Especially welcome after hours of fruitless project and config restructuring.