Skip to content

Commit 13c1974

Browse files
authored
Use jacoco-report-aggregation plugin (#3049)
1 parent e8843ec commit 13c1974

File tree

9 files changed

+96
-90
lines changed

9 files changed

+96
-90
lines changed

build.gradle.kts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
`base-conventions`
55
`build-metadata`
66
`dependency-update-check`
7-
`jacoco-conventions`
7+
`jacoco-aggregation-conventions`
88
`temp-maven-repo`
99
}
1010

@@ -47,6 +47,12 @@ val vintageProjects by extra(listOf(
4747
val mavenizedProjects by extra(platformProjects + jupiterProjects + vintageProjects)
4848
val modularProjects by extra(mavenizedProjects - listOf(projects.junitPlatformConsoleStandalone.dependencyProject))
4949

50+
dependencies {
51+
(modularProjects + listOf(projects.platformTests.dependencyProject)).forEach {
52+
jacocoAggregation(project(it.path))
53+
}
54+
}
55+
5056
nexusPublishing {
5157
packageGroup.set("org.junit")
5258
repositories {
@@ -57,36 +63,3 @@ nexusPublishing {
5763
nohttp {
5864
source.exclude("buildSrc/build/generated-sources/**")
5965
}
60-
61-
val jacocoTestProjects = listOf(
62-
projects.junitJupiterEngine,
63-
projects.junitJupiterMigrationsupport,
64-
projects.junitJupiterParams,
65-
projects.junitVintageEngine,
66-
projects.platformTests
67-
).map { it.dependencyProject }
68-
val jacocoClassesDir by extra(file("$buildDir/jacoco/classes"))
69-
70-
val jacocoRootReport by tasks.registering(JacocoReport::class) {
71-
modularProjects.forEach {
72-
dependsOn(it.tasks.named("extractJar"))
73-
it.pluginManager.withPlugin("java") {
74-
sourceDirectories.from(it.the<SourceSetContainer>()["main"].allSource.srcDirs)
75-
}
76-
}
77-
classDirectories.from(files(jacocoClassesDir))
78-
reports {
79-
html.required.set(true)
80-
xml.required.set(true)
81-
csv.required.set(false)
82-
}
83-
}
84-
85-
afterEvaluate {
86-
jacocoRootReport {
87-
jacocoTestProjects.forEach {
88-
executionData(it.tasks.withType<Test>().map { task -> task.the<JacocoTaskExtension>().destinationFile })
89-
dependsOn(it.tasks.withType<Test>())
90-
}
91-
}
92-
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import org.gradle.api.attributes.LibraryElements
3+
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
4+
import org.gradle.api.attributes.TestSuiteType
5+
import org.gradle.kotlin.dsl.invoke
6+
import org.gradle.kotlin.dsl.`jacoco-report-aggregation`
7+
import org.gradle.kotlin.dsl.named
8+
import org.gradle.testing.jacoco.plugins.JacocoCoverageReport
9+
import org.junit.gradle.jacoco.JacocoConventions.COVERAGE_CLASSES
10+
11+
plugins {
12+
id("jacoco-conventions")
13+
`jacoco-report-aggregation`
14+
}
15+
16+
configurations {
17+
allCodeCoverageReportClassDirectories {
18+
attributes {
19+
attribute(LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements::class, COVERAGE_CLASSES))
20+
}
21+
}
22+
}
23+
24+
reporting {
25+
reports {
26+
create<JacocoCoverageReport>("jacocoRootReport") {
27+
testType.set(TestSuiteType.UNIT_TEST)
28+
}
29+
}
30+
}
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2-
31
plugins {
42
jacoco
53
}
@@ -10,51 +8,6 @@ jacoco {
108
toolVersion = requiredVersionFromLibs("jacoco")
119
}
1210

13-
tasks {
14-
withType<Test>().configureEach {
15-
configure<JacocoTaskExtension> {
16-
isEnabled = enableJaCoCo
17-
}
18-
}
19-
withType<JacocoReport>().configureEach {
20-
enabled = enableJaCoCo
21-
}
22-
}
23-
24-
pluginManager.withPlugin("java") {
25-
26-
val jacocoClassesDir: File by rootProject.extra
27-
28-
val jar by tasks.existing(Jar::class)
29-
30-
val extractJar by tasks.registering(Copy::class) {
31-
from(zipTree(jar.map { it.archiveFile }))
32-
into(jacocoClassesDir)
33-
include("**/*.class")
34-
// don't version-specific classes of MR JARs
35-
exclude("META-INF/versions/**")
36-
includeEmptyDirs = false
37-
onlyIf { jar.get().enabled }
38-
}
39-
40-
jar {
41-
finalizedBy(extractJar)
42-
}
43-
44-
tasks.named<JacocoReport>("jacocoTestReport") {
45-
enabled = false
46-
}
47-
48-
pluginManager.withPlugin("com.github.johnrengelman.shadow") {
49-
50-
val shadowJar by tasks.existing(ShadowJar::class) {
51-
finalizedBy(extractJar)
52-
}
53-
extractJar {
54-
from(zipTree(shadowJar.map { it.archiveFile }))
55-
// don't report coverage for shadowed classes
56-
exclude("**/shadow/**")
57-
onlyIf { shadowJar.get().enabled }
58-
}
59-
}
11+
tasks.withType<JacocoReport>().configureEach {
12+
enabled = enableJaCoCo
6013
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
3+
import org.junit.gradle.jacoco.JacocoConventions.COVERAGE_CLASSES
4+
5+
plugins {
6+
java
7+
id("jacoco-conventions")
8+
}
9+
10+
val mavenizedProjects: List<Project> by rootProject.extra
11+
val enableJaCoCo = project.hasProperty("enableJaCoCo")
12+
13+
tasks.withType<Test>().configureEach {
14+
configure<JacocoTaskExtension> {
15+
isEnabled = enableJaCoCo
16+
}
17+
}
18+
19+
val codeCoverageClassesJar by tasks.registering(Jar::class) {
20+
from(tasks.jar.map { zipTree(it.archiveFile) })
21+
archiveClassifier.set("jacoco")
22+
enabled = project in mavenizedProjects
23+
duplicatesStrategy = DuplicatesStrategy.INCLUDE
24+
}
25+
26+
configurations.create("codeCoverageReportClasses") {
27+
isCanBeResolved = false
28+
isCanBeConsumed = true
29+
isTransitive = false
30+
attributes.attribute(LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements::class, COVERAGE_CLASSES))
31+
outgoing.artifact(codeCoverageClassesJar)
32+
}

buildSrc/src/main/kotlin/java-library-conventions.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
idea
99
checkstyle
1010
id("base-conventions")
11-
id("jacoco-conventions")
11+
id("jacoco-java-conventions")
1212
}
1313

1414
val mavenizedProjects: List<Project> by rootProject.extra
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.junit.gradle.jacoco
2+
3+
object JacocoConventions {
4+
const val COVERAGE_CLASSES = "coverage-classes"
5+
}

buildSrc/src/main/kotlin/shadow-conventions.gradle.kts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ plugins {
55

66
val shadowed by configurations.creating
77

8-
configurations.forEach { configuration ->
9-
configuration.outgoing.apply {
10-
val removed = artifacts.removeIf { it.classifier.isNullOrEmpty() }
11-
if (removed) {
12-
artifact(tasks.shadowJar) {
13-
classifier = ""
8+
configurations {
9+
listOf(apiElements, runtimeElements).forEach {
10+
it.configure {
11+
outgoing {
12+
artifacts.clear()
13+
artifact(tasks.shadowJar) {
14+
classifier = ""
15+
}
1416
}
1517
}
1618
}
@@ -54,6 +56,10 @@ tasks {
5456
dependsOn(shadowJar)
5557
enabled = false
5658
}
59+
named<Jar>("codeCoverageClassesJar") {
60+
from(shadowJar.map { zipTree(it.archiveFile) })
61+
exclude("**/shadow/**")
62+
}
5763
test {
5864
dependsOn(shadowJar)
5965
// in order to run the test against the shadowJar

junit-platform-commons/junit-platform-commons.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ tasks.jar {
2929
})
3030
}
3131

32+
tasks.codeCoverageClassesJar {
33+
exclude("org/junit/platform/commons/util/ModuleUtils.class")
34+
}
35+
3236
eclipse {
3337
classpath {
3438
sourceSets -= project.sourceSets.mainRelease9.get()

junit-platform-console/junit-platform-console.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ tasks {
4949
)
5050
})
5151
}
52+
codeCoverageClassesJar {
53+
exclude("org/junit/platform/console/options/ConsoleUtils.class")
54+
}
5255
jar {
5356
manifest {
5457
attributes("Main-Class" to "org.junit.platform.console.ConsoleLauncher")

0 commit comments

Comments
 (0)