Gradle jacoco plugin

Hi

Since I was asked, if I read the documentation - yes I do. But this leaves me with a couple of questions. I am still struggeling with the how and when does gradle setup the dependencies. :smiley:

For instance, when applying the gradle jacoco plugin it directly tells the reader, that the jacocoTestReport task does not depend on the test task. As solution, the usage of finalizedBy and dependsOn is suggested.

I am particularly confused, that jacocoTestReport is market UP-TO-DATE after re-building the solution. Isn’t that only possible if the task’s input- and output directory stays the same? Thus, the task implicitely does check that?

So, didn’t we discuss previously to wire inputs and outpus? Thus, wouldn’t it be more appropriate to simply state?

tasks.jacocoTestReport {
    inputs.dir(tasks.test)
}

And in that context, I naively thought that

tasks.jacocoTestReport {}

is just a short-hand notation for

tasks { jacocoTestReport { } }

But in the latter case, gradle complains about

inputs.dir(tasks.test)
                  ^ None of the following candidates is applicable:
                    
                      val SourceSetContainer.test: NamedDomainObjectProvider<SourceSet>:
                        Unresolved reference. None of the following candidates is applicable because of a receiver type mismatch:
                      val SourceSetContainer.test: NamedDomainObjectProvider<SourceSet>
                    
                      val TaskContainer.test: TaskProvider<Test>:
                        Unresolved reference. None of the following candidates is applicable because of a receiver type mismatch:
                      val TaskContainer.test: TaskProvider<Test>

And for the sake of completeness, there also exists sourceSets, which is only applicable for Java source and resource files, as the documentation states - and has nothing to do with generally wiring inputs and outputs of tasks?

Additionally, there exists source which is only available for SourceTask? And since jacocoTestReport does not implement / is derived from that type, source is not available?

And last but not least, I want the jacocoTestReport configuration to propagate down to my sub-projects of my multi-project. The recommended way is the convention plugin ? Do I really need the jacoco report aggregation plugin ? It might be fine to aggregate the report into a single report, but as long as sonar gets along with it, no need?

Since I was asked, if I read the documentation - yes I do.

Sorry if this came over harsher than it was meant, I wouldn’t have asked this question here, as the JaCoCo topic with task dependencies is indeed a bit awkward. :smiley:

I am particularly confused, that jacocoTestReport is market UP-TO-DATE after re-building the solution.

Why should it not be up-to-date, if its inputs - which is the JaCoCo execution data file produced by the test task and some other files like the compiled classes and JaCoCo itself - did not change?

If any of the inputs changed and the task is considered up-to-date, then that is a bug you should report. But if nothing changed, Gradle does what it is best at and avoids unnecessary work.

Thus, the task implicitely does check that?

Yes, of course, that is one reason why tasks should always cleanly declare their inputs and outputs.

So, didn’t we discuss previously to wire inputs and outpus? Thus, wouldn’t it be more appropriate to simply state?

Actually, no.
Besides that dir probably doesn’t work, it would state that all outputs of the test task are inputs of the jacocoTestReport task.
But you only want the JaCoCo execution data file output to be the input for the other task.
That would then more correctly be

tasks.jacocoTestReport {
    inputs.file(tasks.test.map { it.the<JacocoTaskExtension>().destinationFile })
}

But whether this is intended here is the question and a question of what you want.
The JaCoCo execution data file already is an output of the test task.
And it already is an input of the jacocoTestReport task.
And there already is a mustRunAfter ordering constraint between jacocoTestReport and test.

For whatever reason the Gradle folks decided that this is all there is for default bonding between those tasks.
About the motivation I can just guess.

But a maybe good guess could be, that you might want to get the coverage report even if the test task is failing. If jacocoTestReport depends on test, then jacocoTestReport only runs if test was successful. And adding the explicit dependency - either through an explicit dependsOn or the snippet I just wrote and would prefer for cleanliness - is easier to add than to remove if that dependency would be built-in.

And in that context, I naively thought that

tasks.jacocoTestReport {}

is just a short-hand notation for

tasks { jacocoTestReport { } }

It is, kind of.

But in the latter case, gradle complains about

Hover in your IDE over tasks and you see, that tasks is not TaskContainer, but TaskProvider<TaskReportTask>.

So by writing

tasks {
    jacocoTestReport {
        inputs.dir(tasks.test)
    }
}

you effectively wrote the equivalent of

tasks.jacocoTestReport {
    inputs.dir(tasks.tasks.test)
}

which then fails the same way.

What you would have wanted to write was

tasks {
    jacocoTestReport {
        inputs.dir(test)
    }
}

And for the sake of completeness, there also exists sourceSets which is only applicable for Java source and resource files, as the documentation states

I’d say that is an error in the documentation.
A source set is a collection of source directory sets,
depending on what plugins you apply.
For example the java source directory set, or the resources source directory set, but also the groovy source directory set or the antlr source directory set.

It has special support and direct methods for java and resources and other source directory sets like groovy or antlr are added via the extension mechanism. So the documentation there is maybe something between a bit over-possessive and wrong.

and has nothing to do with generally wiring inputs and outputs of tasks?

A source set - or actuatlly the source directory sets in it - indeed have a role in wiring task inputs and outputs together, in that it is an in-between piece.

For example tasks that need all source code, like a sources jar task, get the sources of the source set and thus the sources of all the source directory sets.

And tasks that generate some of these sources like Java sources, some resources, and so on, are registered as source directories in those source directory sets.

That way all source-consuming tasks automatically get a complete set of sources and and automatically have all the necessary implicit task dependencies that generate those sources.

Additionally, there exists source which is only available for SourceTask? And since jacocoTestReport does not implement / is derived from that type, source is not available?

Correct, just like in any class hierarchy in any programming language. It also does not make much sense for the JacocoReport task type I guess. A SourceTask is something where you also include and exclude files and that you usually wouldn’t want to there.

And last but not least, I want the jacocoTestReport configuration to propagate down to my sub-projects of my multi-project. The recommended way is the convention plugin?

The answer is always the same - short of the few things you can configure in the settings script. Yes, if you want to have some convention applied to multiple places, you put it to a convention plugin and apply that plugin where you want the convention to be effective. I personally wouldn’t use buildSrc though, but an included build with the build logic.

Do I really need the jacoco report aggregation plugin?

My crystal ball is in the repair shop, sorry.
If you want to generate a combined report over multiple projects using Gradle, you should use it, but even then you don’t need it, but could do the same things it does manually, just doesn’t make much sense. :smiley:
If you don’t want to have an aggregated report produced by Gradle, you don’t need that plugin.