Skip to content

Commit 4ca33b3

Browse files
committed
Fix no source root found issue with Scala 3 for multi-module projects
1 parent c8a3a37 commit 4ca33b3

File tree

8 files changed

+155
-43
lines changed

8 files changed

+155
-43
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals=clean verify site -e -ntp
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>it.scoverage-maven-plugin</groupId>
8+
<artifactId>test_Scala3_multimodule</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<modelVersion>4.0.0</modelVersion>
14+
<artifactId>module01</artifactId>
15+
<name>Test Scala 3 Multi-Module: Module 1</name>
16+
17+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pkg01
2+
3+
class HelloService1
4+
{
5+
def hello: String =
6+
{
7+
"Hello from module 1"
8+
}
9+
10+
}
11+
12+
object HelloService1 extends HelloService1
Lines changed: 14 additions & 0 deletions
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(): Unit =
10+
{
11+
assertEquals("Hello from module 1", HelloService1.hello)
12+
}
13+
14+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_Scala3_multimodule</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<packaging>pom</packaging>
17+
<name>Test Scala 3 Multi-Module</name>
18+
<description>Test Scala 3 Multi-Module</description>
19+
20+
<properties>
21+
<scala.compat.version>3</scala.compat.version>
22+
<scala.version>3.3.1</scala.version>
23+
<scala.library.artifact.id>scala3-library_3</scala.library.artifact.id>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
</properties>
26+
27+
<modules>
28+
<module>module01</module>
29+
</modules>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
</plugin>
37+
<plugin>
38+
<groupId>net.alchim31.maven</groupId>
39+
<artifactId>scala-maven-plugin</artifactId>
40+
</plugin>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-surefire-plugin</artifactId>
44+
</plugin>
45+
<plugin>
46+
<groupId>@project.groupId@</groupId>
47+
<artifactId>@project.artifactId@</artifactId>
48+
<configuration>
49+
<aggregate>true</aggregate> <!-- for aggregated report -->
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<reporting>
56+
<plugins>
57+
<plugin>
58+
<groupId>@project.groupId@</groupId>
59+
<artifactId>@project.artifactId@</artifactId>
60+
<configuration>
61+
<aggregate>true</aggregate> <!-- for aggregated report -->
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</reporting>
66+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
try {
2+
3+
def module1ScoverageFile = new File(basedir, "module01/target/scoverage.xml")
4+
assert module1ScoverageFile.exists()
5+
6+
def module1ReportFile = new File(basedir, "module01/target/site/scoverage/index.html")
7+
assert module1ReportFile.exists()
8+
9+
def aggregatedScoverageFile = new File(basedir, "target/scoverage.xml")
10+
assert aggregatedScoverageFile.exists()
11+
12+
def aggregatedReportFile = new File(basedir, "target/site/scoverage/index.html")
13+
assert aggregatedReportFile.exists()
14+
15+
return true
16+
17+
} catch (Throwable e) {
18+
e.printStackTrace()
19+
return false
20+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.maven.artifact.resolver.ArtifactResolver;
3434
import org.apache.maven.artifact.versioning.ArtifactVersion;
3535
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
36+
import org.apache.maven.execution.MavenSession;
3637
import org.apache.maven.model.Dependency;
3738
import org.apache.maven.plugin.AbstractMojo;
3839
import org.apache.maven.plugin.MojoExecutionException;
@@ -155,6 +156,12 @@ public class SCoveragePreCompileMojo
155156
@Parameter( defaultValue = "${project}", readonly = true, required = true )
156157
private MavenProject project;
157158

159+
/**
160+
* The current Maven session.
161+
*/
162+
@Parameter( defaultValue = "${session}", readonly = true, required = true )
163+
private MavenSession session;
164+
158165
/**
159166
* All Maven projects in the reactor.
160167
*/
@@ -283,7 +290,7 @@ public void execute() throws MojoExecutionException
283290
String _scalacOptions = quoteArgument( arg );
284291
String addScalacArgs = arg;
285292

286-
arg = scala2 ? ( SOURCE_ROOT_OPTION + project.getBasedir().getAbsolutePath() ) : "";
293+
arg = scala2 ? ( SOURCE_ROOT_OPTION + session.getTopLevelProject().getBasedir().getAbsolutePath() ) : "";
287294
_scalacOptions = _scalacOptions + SPACE + quoteArgument( arg );
288295
addScalacArgs = addScalacArgs + PIPE + arg;
289296

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

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
import java.io.File;
2424
import java.io.FileInputStream;
2525
import java.util.ArrayList;
26-
import java.util.ArrayDeque;
2726
import java.util.Arrays;
2827
import java.util.List;
2928
import java.util.Locale;
30-
import java.util.Queue;
3129
import java.util.ResourceBundle;
3230
import java.util.concurrent.atomic.AtomicInteger;
3331

3432
import org.apache.maven.doxia.siterenderer.RenderingContext;
3533
import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
3634

35+
import org.apache.maven.execution.MavenSession;
3736
import org.apache.maven.plugin.AbstractMojo;
3837
import org.apache.maven.plugin.MojoExecutionException;
3938
import org.apache.maven.plugins.annotations.Execute;
@@ -58,7 +57,6 @@
5857
import scoverage.reporter.IOUtils;
5958
import scoverage.serialize.Serializer;
6059
import scoverage.reporter.CoberturaXmlWriter;
61-
import scoverage.reporter.CoverageAggregator;
6260
import scoverage.reporter.ScoverageHtmlWriter;
6361
import scoverage.reporter.ScoverageXmlWriter;
6462

@@ -131,6 +129,12 @@ public class SCoverageReportMojo
131129
@Parameter( defaultValue = "${project}", readonly = true, required = true )
132130
private MavenProject project;
133131

132+
/**
133+
* The current Maven session.
134+
*/
135+
@Parameter(defaultValue = "${session}", readonly = true, required = true)
136+
private MavenSession session;
137+
134138
/**
135139
* All Maven projects in the reactor.
136140
*/
@@ -423,7 +427,7 @@ private void generateReports()
423427

424428
File coverageFile = Serializer.coverageFile( dataDirectory );
425429
getLog().info( String.format( "Reading scoverage instrumentation [%s]...", coverageFile.getAbsolutePath() ) );
426-
Coverage coverage = Serializer.deserialize( coverageFile, project.getBasedir() );
430+
Coverage coverage = Serializer.deserialize( coverageFile, session.getTopLevelProject().getBasedir() );
427431

428432
getLog().info( String.format( "Reading scoverage measurements [%s*]...",
429433
new File( dataDirectory, Constants.MeasurementsPrefix() ).getAbsolutePath() ) );
@@ -444,22 +448,18 @@ private void generateAggregatedReports()
444448
AtomicInteger id = new AtomicInteger();
445449
List<File> scoverageDataDirs = new ArrayList<File>();
446450
List<File> sourceRoots = new ArrayList<File>();
447-
MavenProject topLevelModule = null;
451+
MavenProject topLevelProject = session.getTopLevelProject();
448452
for ( MavenProject module : reactorProjects )
449453
{
450-
if ( module.isExecutionRoot() )
451-
{
452-
topLevelModule = module;
453-
}
454-
else if ( !module.getPackaging().equals( "pom" ) )
454+
if ( !module.getPackaging().equals( "pom" ) )
455455
{
456456
File scoverageDataDir = rebase( dataDirectory, module );
457457
if ( scoverageDataDir.isDirectory() )
458458
{
459459
scoverageDataDirs.add( scoverageDataDir );
460460
File coverageFile = Serializer.coverageFile(scoverageDataDir);
461461
if (coverageFile.exists()) {
462-
Coverage subCoverage = Serializer.deserialize(coverageFile, module.getBasedir());
462+
Coverage subCoverage = Serializer.deserialize(coverageFile, topLevelProject.getBasedir());
463463
List<File> measurementFiles = Arrays.asList( IOUtils.findMeasurementFiles( scoverageDataDir ) );
464464
scala.collection.Set<Tuple2<Object, String>> measurements =
465465
IOUtils.invoked( CollectionConverters.asScala( measurementFiles ).toSeq(), encoding );
@@ -537,40 +537,15 @@ else if ( !module.getPackaging().equals( "pom" ) )
537537
scoverageDataDirs.size() ) );
538538
}
539539

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-
565-
File topLevelModuleOutputDirectory = rebase( outputDirectory, topLevelModule );
566-
File topLevelModuleXmlOutputDirectory = rebase( xmlOutputDirectory, topLevelModule );
540+
File topLevelProjectOutputDirectory = rebase( outputDirectory, topLevelProject );
541+
File topLevelProjectXmlOutputDirectory = rebase( xmlOutputDirectory, topLevelProject );
567542

568-
mkdirs( topLevelModuleOutputDirectory );
569-
mkdirs( topLevelModuleXmlOutputDirectory );
543+
mkdirs( topLevelProjectOutputDirectory );
544+
mkdirs( topLevelProjectXmlOutputDirectory );
570545

571546
getLog().info( "Generating coverage aggregated reports..." );
572-
writeReports( coverage, sourceRoots, topLevelModuleXmlOutputDirectory, topLevelModuleXmlOutputDirectory,
573-
topLevelModuleOutputDirectory );
547+
writeReports( coverage, sourceRoots, topLevelProjectXmlOutputDirectory, topLevelProjectXmlOutputDirectory,
548+
topLevelProjectOutputDirectory );
574549
getLog().info( "Coverage aggregated reports completed." );
575550
}
576551

0 commit comments

Comments
 (0)