Skip to content

Commit 2644e30

Browse files
sumeetgajjarjozic
andauthored
Fix NPE during report aggregation when multiple modules are specified using -pl arg (#104)
Co-authored-by: Eugene Platonov <jozic@live.com>
1 parent dd052eb commit 2644e30

File tree

15 files changed

+258
-1
lines changed

15 files changed

+258
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ or
158158
There is no separate mojo for aggregated reports, there is `aggregate` parameter.
159159
To additionally generate aggregated SCoverage report for root module, when generating regular reports,
160160
set `aggregate` parameter value to `true`.
161-
It works only in multimodule projects.
161+
It works only in multimodule projects, the aggregated report will be generated in the current
162+
execution root.
162163

163164
It can be configured by defining `aggregate` plugin configuration parameter or `scoverage.aggregate` project property.
164165

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals=clean scoverage:report -e -ntp -pl module01 -pl module02/submodule02_02
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<groupId>it.scoverage-maven-plugin</groupId>
7+
<artifactId>test_report_for_some_submodules</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>module01</artifactId>
14+
<name>Test Scoverage report works for specified submodules: Module 1</name>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pkg01
2+
3+
class HelloService1
4+
{
5+
def hello =
6+
{
7+
"Hello from module 1"
8+
}
9+
10+
}
11+
12+
object HelloService1 extends HelloService1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pkg01
2+
3+
import org.junit.Test;
4+
import org.junit.Assert.assertEquals
5+
6+
class HelloServiceTest
7+
{
8+
@Test
9+
def test1()
10+
{
11+
assertEquals("Hello from module 1", HelloService1.hello)
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<groupId>it.scoverage-maven-plugin</groupId>
7+
<artifactId>test_report_for_some_submodules</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>module02</artifactId>
14+
<packaging>pom</packaging>
15+
<name>Test Scoverage report works for specified submodules: Module 2</name>
16+
17+
<modules>
18+
<module>submodule02_01</module>
19+
<module>submodule02_02</module>
20+
</modules>
21+
22+
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<groupId>it.scoverage-maven-plugin</groupId>
7+
<artifactId>module02</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>submodule02_01</artifactId>
14+
<name>Test Scoverage report works for specified submodules: SubModule 2_1</name>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pkg02_01
2+
3+
class HelloService2
4+
{
5+
def hello =
6+
{
7+
"Hello from submodule02_01"
8+
}
9+
10+
}
11+
12+
object HelloService2 extends HelloService2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pkg02_01
2+
3+
import org.junit.Test;
4+
import org.junit.Assert.assertEquals
5+
6+
class HelloServiceTest
7+
{
8+
@Test
9+
def test2()
10+
{
11+
assertEquals("Hello from submodule02_01", HelloService2.hello)
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<groupId>it.scoverage-maven-plugin</groupId>
7+
<artifactId>module02</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>submodule02_02</artifactId>
14+
<name>Test Scoverage report works for specified submodules: SubModule 2_2</name>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pkg02_02
2+
3+
class HelloService2
4+
{
5+
def hello =
6+
{
7+
"Hello from submodule02_02"
8+
}
9+
10+
}
11+
12+
object HelloService2 extends HelloService2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pkg02_02
2+
3+
import org.junit.Test;
4+
import org.junit.Assert.assertEquals
5+
6+
class HelloServiceTest
7+
{
8+
@Test
9+
def test2()
10+
{
11+
assertEquals("Hello from submodule02_02", HelloService2.hello)
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>it.scoverage-maven-plugin</groupId>
9+
<artifactId>integration_tests_parent</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<relativePath>../integration_tests_parent/pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>test_report_for_some_submodules</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<packaging>pom</packaging>
17+
<name>Test Scoverage report works for specified submodules</name>
18+
<description>Test Scoverage report works for specified submodules</description>
19+
20+
<properties>
21+
<scala.compat.version>2.13</scala.compat.version>
22+
<scala.minor.version>12</scala.minor.version>
23+
</properties>
24+
25+
<modules>
26+
<module>module01</module>
27+
<module>module02</module>
28+
</modules>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
</plugin>
36+
<plugin>
37+
<groupId>net.alchim31.maven</groupId>
38+
<artifactId>scala-maven-plugin</artifactId>
39+
</plugin>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-surefire-plugin</artifactId>
43+
</plugin>
44+
<plugin>
45+
<groupId>@project.groupId@</groupId>
46+
<artifactId>@project.artifactId@</artifactId>
47+
<configuration>
48+
<aggregate>true</aggregate> <!-- for aggregated report -->
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
<reporting>
55+
<plugins>
56+
<plugin>
57+
<groupId>@project.groupId@</groupId>
58+
<artifactId>@project.artifactId@</artifactId>
59+
<configuration>
60+
<aggregate>true</aggregate> <!-- for aggregated report -->
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</reporting>
65+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
try {
2+
3+
def aggregatedScoverageFile = new File(basedir, "target/scoverage.xml")
4+
assert aggregatedScoverageFile.exists()
5+
6+
def aggregatedReportFile = new File(basedir, "target/site/scoverage/index.html")
7+
assert aggregatedReportFile.exists()
8+
9+
return true
10+
11+
} catch (Throwable e) {
12+
e.printStackTrace()
13+
return false
14+
}

src/main/java/org/scoverage/plugin/SCoverageReportMojo.java

+27
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import java.io.File;
2424
import java.io.FileInputStream;
2525
import java.util.ArrayList;
26+
import java.util.ArrayDeque;
2627
import java.util.Arrays;
2728
import java.util.List;
2829
import java.util.Locale;
30+
import java.util.Queue;
2931
import java.util.ResourceBundle;
3032
import java.util.concurrent.atomic.AtomicInteger;
3133

@@ -535,6 +537,31 @@ else if ( !module.getPackaging().equals( "pom" ) )
535537
scoverageDataDirs.size() ) );
536538
}
537539

540+
/* traverse up the module tree until a module isExecutionRoot */
541+
if ( topLevelModule == null )
542+
{
543+
Queue<MavenProject> candidateForTopLevelModules = new ArrayDeque<>(reactorProjects);
544+
while ( !candidateForTopLevelModules.isEmpty() )
545+
{
546+
MavenProject module = candidateForTopLevelModules.poll();
547+
if ( module.isExecutionRoot() )
548+
{
549+
topLevelModule = module;
550+
break;
551+
}
552+
if ( module.hasParent() )
553+
{
554+
candidateForTopLevelModules.add(module.getParent());
555+
}
556+
}
557+
}
558+
if ( topLevelModule == null )
559+
{
560+
// This exception should never be thrown.
561+
throw new IllegalStateException("Cannot find the top level module to write the " +
562+
"aggregated reports.");
563+
}
564+
538565
File topLevelModuleOutputDirectory = rebase( outputDirectory, topLevelModule );
539566
File topLevelModuleXmlOutputDirectory = rebase( xmlOutputDirectory, topLevelModule );
540567

0 commit comments

Comments
 (0)