I have the similar project sructure - main code is in one module and tests in anoter - and got similar problem with jacoco reporting.
My solution 1:
Just copy /classes
folder to test module by maven-resources-plugin
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-classes-to-test-module</id>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/../main-module/target/classes</directory>
</resource>
</resources>
<outputDirectory>
${project.build.outputDirectory}
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
But if your code contains lambdas, anonymous classes or so on, then the result report may not cover some classes, because it will be different classes due to different prepare-agent
runs.
My solution 2:
Use the report-aggregate
goal.
In parent POM I have a configuration like this:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
And the test-module's POM contanis these lines:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
Take in mind that the dependency scope is important for the report-aggregate
goal (jacoco docs). If your test-module has only test-scoped dependencies, you will got an empty report. Therefore, you should set the scope to compile, runtime or provided for those artifacts, which you want to see in the report. For example:
<dependency>
<groupId>my.project</groupId>
<artifactId>main-module-classes</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>