I am able to get code coverage working fine if I have a single project but now I have a multi module project.
My application is built in the api
project and all my integration tests are running in a separate project which uses the artefact built as previous module.
The build runs but I dont get a code coverage report, instead I get the info message:
Skipping JaCoCo execution due to missing classes directory
My coverage report file jacoco-it.exec
is created but it seems like the jacoco
plugin requires the classes in the project the tests are running in.
Can somebody tell me what I need to do to be able to create a coverage report when the classes are in another module?
Even though jacoco:report-aggregate
was meant for aggregation report on the dependencies, it did not work in my case - I use project structure like:
project
└─ module-api ⇦ API definition
└─ module-spec ⇦ module with black box tests through the API
└─ module-impl1 ⇦ implementation 1 of the API
└─ module-impl2 ⇦ another implementation of the API
└─ module-test1 ⇦ some dependencies + impl1, apply the tests from spec
└─ module-test2 ⇦ some dependencies + impl2, apply the tests from spec
In this case report-aggregate
generated an empty report. The module-test1 and module-test2 projects are with POM packaging type, which is not suitable to run report
on them too, as it requires classes in the target/classes directory.
My solution
I configured the jacoco plugin to dump the classes it instruments in the in the project's target/classes directory by adding:
<classDumpDir>${project.build.outputDirectory}</classDumpDir>
This allowed the regular report
goal to work. The complete code is:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<configuration>
<destFile>${project.build.directory}/coverage-reports/code-coverage.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/code-coverage.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/code-coverage</outputDirectory>
<propertyName>jacocoArgLine</propertyName>
<classDumpDir>${project.build.outputDirectory}</classDumpDir>
</configuration>
<executions>
<execution>
<id>prepare-jacoco-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Please note that the instrumented classes are from all dependencies, so some filtering would be needed to narrow the report scope. See: https://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With