When using the Gradle JaCoCo plugin, it would be nice if the coverage reports for all subprojects were merged together, to make them easier to access. Could the next version handle multiproject setups better?
In the meantime, is there a manual configuration that makes gradle test jacoco perform this merging when run at the parent project level?
That integration test doesnât actually appear to be merging subprojects and I couldnât figure out a variation that seemed to work to merge my subprojects. I am admittedly mostly just a monkey pressing keys at random when it comes to Gradle, but a working example of this for a project with subprojects would be really useful.
The JacocoMerge is probably more correct, but a common alternative is the jacocoRootReport style task. The example below is how you can create an aggregate report and publish to Coveralls. You can dig into the usage here if you run into problems.
I have been searching for the answer to this question several times over the last year, each time giving up after a couple of hours of testing a zillion different suggestions - in vain. I am not deeply into Gradle, so much of it seems like black magic. I finally managed to adapt Benjaminâs suggestion into something that works for me (a project with subprojects common, client, server, and integration). And it works on Gradle 5.6.
The one thing I could not figure out is how to automatically avoid subprojects that do not produce a jacoco.exec (which is the case of âcommonâ), so I have to have a manual test in the merge task, which is a bit âhandheldâ. Anyone who knows the trick to make gradle ignore in case there is no file?
Embed the above in the root build.gradle, and gradle test jacocoRootReport to get coverage for all subprojects in the build/reports/⊠folder.
I wouldnât recommend this approach. You are looking for the files in Gradleâs âconfigurationâ phase but the files are created in Gradleâs âexecutionâ phase. See build phases
TLDR; this will only work in a âdirtyâ build scenario where a previous build has run jacoco and was not âcleanedâ
I suggest you determine the projects by inspecting the Gradle model (ie tasks/plugins in the model)
@ciprian-radu where would you put the task jacocoMergeAll(type: JacocoMerge) script you have written? Iâve tried to put it in the root gradle file of my android project, but as soon as I sync I get the following error message
Could not get unknown property 'test' for project ':app' of type org.gradle.api.Project.
Is there something that I am missing? Do you have a full working sample somewhere that I could use a reference?
Seriously, I have spent hours trying to fix jacoco after upgrading 5.3 â 5.6. The first comment on this github gist USED to work in 5.3 but not in 5.6âŠ(perfect example of how to help people with a full working solution)âŠ
I am trying to get 5.6 to work without the âcanât find test.execâ error and it seems people keep giving âpartialâ solutions. The gist above was a complete amazing example that âjust workedâ. Does someone have a merge task that âjust worksâ? I have tried way way too many things and spent way tooo much time on this due to âpartialâ solutions of try this. or if I find it, I will post my complete solution as that would help everyone here.
I really also donât get why this would work as âeveryâ subproject has the plugin BUT itâs just they donât generate a test.exec file since they have no testsâŠ
Instead, the other solutions of gathering up all test.exec files seems more valid to me(and what was done in the past versions like 5.3). but then @Lance said âI wouldnât recommend this approach.â so I am very very confused. A complete jacoco merge to use in gradle 5.6 that works in gradle 6 would rock if someone has it. If I get to it first, I will post it as well, but I am getting to the point of slamming into brick wall and stopping for a while to cycle back fresh at some point.
@Lance I tried combining your idea with the merge report but alas I get test.exec not found in the empty projects since the jacoco plugin is in my subprojects section, even empty projects get the plugin so that doesnât seem to work. Here is my latest try and the errorâŠ
Unable to read /Users/dean/workspace/webpieces/core/output/jacoco/test.exec
NOTE: webpieces/core directory is ONLY a directory containing about 5 subprojects. It has no src directory and no tests, but because the jacoco plugin is in the subprojects {} section, it ends up getting the plugin.
We need a way like @ciprian-radu 's method that only gathers existing test.exec files and skips projects without that file.
ALSO of note is that the test task in webpieces/core directory does not fail saying there is no tests so why does jacoco fail just because there is no test.exec file. This seems like a plugin bug that they should skip merging in certain projects that have no test.exec file automatically.
In a multi-project build, you can use the gradle-jacocolog plugin to aggregate coverage information across subprojects. The plugin is really for logging the coverage data, but it can also perform aggregations.
Configure your Jacoco like you would normally, then apply the plugin to the root project:
plugins {
id 'org.barfuin.gradle.jacocolog' version '1.2.3'
}
After that, you can:
gradle jacocoAggregatedReport
to display the aggregated coverage. There is also a jacocoMergeSubprojects task which will just create a merge coverage file, if you want to process it in some other way.
Hope this helps!
(Full dislosure: I am the author of gradle-jacocolog.)
I have the same question. I donât know where should I put the task.
There is one setting.gradle file in the root directory which should be parsed during initialization phase, and can not define task in it.
I didnât read the whole thread here, but it is seldomly a good idea to use a JacocoMerge task.
This is only necessary if you want to feed JaCoCo execution data of multiple tasks to some tool that does not support handling multiple such files which most should do. The JacocoReport task for example can handle multiple execution data files without problem.
But anyway, if you want to generate an overall JaCoCo report over test from a multi-project build, nowadays there are cleaner and more idiomatic ways anway. I recommend having a look at the sample that exactly demonstrates that: Reporting code coverage with JaCoCo Sample