Joan Rieu
Joan Rieu

Reputation: 1019

How to add source code to Jacoco report in Gradle

In Gradle, when you have multiple projects, you might want to generate a Jacoco test coverage report from one project and have the classes from the other projects show up in the report.

To do that, the JacocoReport documentation from Gradle 2.14 lists two pairs of directives called:

However, both expect a single FileCollection and some glue for all project source sets and output files, with calls to files() on someJavaProjectSourceSet.srcDirs required to get the line-of-code-level audit with actual source code embedded in the report.

Is there a better way ?

Upvotes: 1

Views: 3210

Answers (1)

Joan Rieu
Joan Rieu

Reputation: 1019

The sourceSets directive adds other source sets to the report, with both the source code and the class files.

Although it doesn't appear in the plugin documentation for some reason, that's actually how the plugin itself adds the files for the current project in the default jacocoTestReport task.

/**
 * Adds a source set to the list to be reported on.
 * The output of this source set will be used as classes to include in the report.
 * The source for this source set will be used for any classes included in the report.
 *
 * @param sourceSets one or more source sets to report on
 */
public void sourceSets(final SourceSet... sourceSets)

To include source sets from other projects, you can do:

jacocoTestReport {
    sourceSets project(':myAlphaProject').sourceSets.main
    sourceSets project(':myBetaProject').sourceSets.main
}

Simple !

Upvotes: 4

Related Questions