@@ -6,81 +6,52 @@ extensions.configure(JacocoPluginExtension) {
66 it. toolVersion = jacocoVersion
77}
88
9- // Configuration for declaring which projects contribute coverage data.
10- def coverageDataProjects = configurations. register(' coverageDataProjects' ) {
11- canBeConsumed = false
12- canBeResolved = true
13- }
9+ pluginManager. withPlugin(' groovy' ) {
10+ // The jacoco plugin automatically creates 'jacocoTestReport' for the 'test' task.
11+ // Configure it to produce XML (for CI tools) and HTML reports.
12+ tasks. named(' jacocoTestReport' , JacocoReport ) {
13+ reports {
14+ xml. required = true
15+ html. required = true
16+ csv. required = false
17+ }
1418
15- // Lazily collect source directories and class files from all coverageDataProjects dependencies.
16- def covProjectList = coverageDataProjects. map {
17- it. dependencies. withType(ProjectDependency ). collect {
18- project. project(it. path)
19+ dependsOn(tasks. named(' test' ))
1920 }
20- }
21-
22- def allSourceDirs = covProjectList. map {
23- it. findAll { it. plugins. hasPlugin(' java' ) }
24- .collectMany {
25- it. extensions. getByType(SourceSetContainer ). named(' main' ). get()
26- .allSource. sourceDirectories. files
27- }
28- }
29-
30- def allClassDirs = covProjectList. map {
31- it. findAll { it. plugins. hasPlugin(' java' ) }
32- .collectMany {
33- it. extensions. getByType(SourceSetContainer ). named(' main' ). get()
34- .output. files
35- }
36- }
3721
38- def allExecFiles = covProjectList. map {
39- it. collectMany {
40- it. fileTree(it. layout. buildDirectory. dir(' jacoco' )) {
41- include(' **/*.exec' )
42- }. files
22+ // Ensure coverage report runs after tests
23+ tasks. named(' test' ) {
24+ finalizedBy(tasks. named(' jacocoTestReport' ))
4325 }
4426}
4527
46- // Register the aggregated coverage report task.
47- // This merges JaCoCo execution data from all coverageDataProjects into a single report.
48- // Task dependencies on all Test tasks (test, integrationTest, etc.) in the declared
49- // projects are derived automatically — no hard-coded project paths needed.
50- tasks. register(' jacocoAggregatedReport' , JacocoReport ) {
51- description = ' Generates aggregated JaCoCo coverage report across all subprojects.'
52- group = ' verification'
28+ // When an integrationTest task is registered (e.g., via the Grails web plugin in example apps),
29+ // register a JaCoCo report task for it. Unlike the 'test' task, the JaCoCo plugin does not
30+ // auto-create report tasks for custom Test tasks.
31+ afterEvaluate { proj ->
5332
54- classDirectories. from(allClassDirs)
55- executionData. from(allExecFiles)
56- sourceDirectories. from(allSourceDirs)
33+ def integrationTestTasks = tasks. withType(Test ). matching { it. name == ' integrationTest' }
34+ if (! integrationTestTasks. isEmpty()) {
5735
58- reports {
59- xml . required = true
60- html . required = true
61- csv . required = false
62- }
63- }
36+ def integrationTest = integrationTestTasks . first()
37+ def execFile = integrationTest . extensions . getByType( JacocoTaskExtension ) . destinationFile
38+
39+ def reportTask = tasks . register( ' jacocoIntegrationTestReport ' , JacocoReport ) {
40+ description = ' Generates code coverage report for the integrationTest task. '
41+ group = ' verification '
6442
65- // After evaluation, wire dependsOn for every Test task in every coverage project.
66- // This ensures all .exec files exist before the aggregated report collects them.
67- afterEvaluate {
68- def projects = coverageDataProjects. get(). dependencies
69- .withType(ProjectDependency )
70- .collect { project. project(it. path) }
43+ executionData. from(execFile)
44+ sourceSets(proj. extensions. getByType(SourceSetContainer ). named(' main' ). get())
7145
72- tasks . named( ' jacocoAggregatedReport ' ) { reportTask ->
73- projects . each {
74- it . tasks . withType( Test ) . configureEach { testTask ->
75- reportTask . dependsOn(testTask)
46+ reports {
47+ xml . required = true
48+ html . required = true
49+ csv . required = false
7650 }
51+
52+ dependsOn(integrationTest)
7753 }
78- }
79- }
8054
81- pluginManager. withPlugin(' base' ) {
82- tasks. named(' check' ) {
83- dependsOn(' jacocoAggregatedReport' )
55+ integrationTest. finalizedBy(reportTask)
8456 }
8557}
86-
0 commit comments