Skip to content

Fix no source root found issue with Scala 3 for multi-module projects #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/it/test_Scala3_multimodule/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean verify site -e -ntp
17 changes: 17 additions & 0 deletions src/it/test_Scala3_multimodule/module01/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>test_Scala3_multimodule</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>module01</artifactId>
<name>Test Scala 3 Multi-Module: Module 1</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg01

class HelloService1
{
def hello: String =
{
"Hello from module 1"
}

}

object HelloService1 extends HelloService1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pkg01

import org.junit.Test;
import org.junit.Assert.assertEquals

class HelloServiceTest
{
@Test
def test1(): Unit =
{
assertEquals("Hello from module 1", HelloService1.hello)
}

}
66 changes: 66 additions & 0 deletions src/it/test_Scala3_multimodule/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>integration_tests_parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../integration_tests_parent/pom.xml</relativePath>
</parent>

<artifactId>test_Scala3_multimodule</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Test Scala 3 Multi-Module</name>
<description>Test Scala 3 Multi-Module</description>

<properties>
<scala.compat.version>3</scala.compat.version>
<scala.version>3.3.1</scala.version>
<scala.library.artifact.id>scala3-library_3</scala.library.artifact.id>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<module>module01</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<configuration>
<aggregate>true</aggregate> <!-- for aggregated report -->
</configuration>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<configuration>
<aggregate>true</aggregate> <!-- for aggregated report -->
</configuration>
</plugin>
</plugins>
</reporting>
</project>
20 changes: 20 additions & 0 deletions src/it/test_Scala3_multimodule/validate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
try {

def module1ScoverageFile = new File(basedir, "module01/target/scoverage.xml")
assert module1ScoverageFile.exists()

def module1ReportFile = new File(basedir, "module01/target/site/scoverage/index.html")
assert module1ReportFile.exists()

def aggregatedScoverageFile = new File(basedir, "target/scoverage.xml")
assert aggregatedScoverageFile.exists()

def aggregatedReportFile = new File(basedir, "target/site/scoverage/index.html")
assert aggregatedReportFile.exists()

return true

} catch (Throwable e) {
e.printStackTrace()
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -155,6 +156,12 @@ public class SCoveragePreCompileMojo
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;

/**
* The current Maven session.
*/
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;

/**
* All Maven projects in the reactor.
*/
Expand Down Expand Up @@ -283,7 +290,7 @@ public void execute() throws MojoExecutionException
String _scalacOptions = quoteArgument( arg );
String addScalacArgs = arg;

arg = scala2 ? ( SOURCE_ROOT_OPTION + project.getBasedir().getAbsolutePath() ) : "";
arg = scala2 ? ( SOURCE_ROOT_OPTION + session.getExecutionRootDirectory() ) : "";
_scalacOptions = _scalacOptions + SPACE + quoteArgument( arg );
addScalacArgs = addScalacArgs + PIPE + arg;

Expand Down
69 changes: 24 additions & 45 deletions src/main/java/org/scoverage/plugin/SCoverageReportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.maven.doxia.siterenderer.RenderingContext;
import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
Expand All @@ -58,7 +57,6 @@
import scoverage.reporter.IOUtils;
import scoverage.serialize.Serializer;
import scoverage.reporter.CoberturaXmlWriter;
import scoverage.reporter.CoverageAggregator;
import scoverage.reporter.ScoverageHtmlWriter;
import scoverage.reporter.ScoverageXmlWriter;

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

/**
* The current Maven session.
*/
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

/**
* All Maven projects in the reactor.
*/
Expand Down Expand Up @@ -423,7 +427,7 @@ private void generateReports()

File coverageFile = Serializer.coverageFile( dataDirectory );
getLog().info( String.format( "Reading scoverage instrumentation [%s]...", coverageFile.getAbsolutePath() ) );
Coverage coverage = Serializer.deserialize( coverageFile, project.getBasedir() );
Coverage coverage = Serializer.deserialize( coverageFile, executionRootDirectory() );

getLog().info( String.format( "Reading scoverage measurements [%s*]...",
new File( dataDirectory, Constants.MeasurementsPrefix() ).getAbsolutePath() ) );
Expand All @@ -444,22 +448,17 @@ private void generateAggregatedReports()
AtomicInteger id = new AtomicInteger();
List<File> scoverageDataDirs = new ArrayList<File>();
List<File> sourceRoots = new ArrayList<File>();
MavenProject topLevelModule = null;
for ( MavenProject module : reactorProjects )
{
if ( module.isExecutionRoot() )
{
topLevelModule = module;
}
else if ( !module.getPackaging().equals( "pom" ) )
if ( !module.getPackaging().equals( "pom" ) )
{
File scoverageDataDir = rebase( dataDirectory, module );
File scoverageDataDir = rebase( dataDirectory, module.getBasedir() );
if ( scoverageDataDir.isDirectory() )
{
scoverageDataDirs.add( scoverageDataDir );
File coverageFile = Serializer.coverageFile(scoverageDataDir);
if (coverageFile.exists()) {
Coverage subCoverage = Serializer.deserialize(coverageFile, module.getBasedir());
Coverage subCoverage = Serializer.deserialize(coverageFile, executionRootDirectory() );
List<File> measurementFiles = Arrays.asList( IOUtils.findMeasurementFiles( scoverageDataDir ) );
scala.collection.Set<Tuple2<Object, String>> measurements =
IOUtils.invoked( CollectionConverters.asScala( measurementFiles ).toSeq(), encoding );
Expand Down Expand Up @@ -537,40 +536,15 @@ else if ( !module.getPackaging().equals( "pom" ) )
scoverageDataDirs.size() ) );
}

/* traverse up the module tree until a module isExecutionRoot */
if ( topLevelModule == null )
{
Queue<MavenProject> candidateForTopLevelModules = new ArrayDeque<>(reactorProjects);
while ( !candidateForTopLevelModules.isEmpty() )
{
MavenProject module = candidateForTopLevelModules.poll();
if ( module.isExecutionRoot() )
{
topLevelModule = module;
break;
}
if ( module.hasParent() )
{
candidateForTopLevelModules.add(module.getParent());
}
}
}
if ( topLevelModule == null )
{
// This exception should never be thrown.
throw new IllegalStateException("Cannot find the top level module to write the " +
"aggregated reports.");
}
File executionRootOutputDirectory = rebase( outputDirectory, executionRootDirectory() );
File executionRootXmlOutputDirectory = rebase( xmlOutputDirectory, executionRootDirectory() );

File topLevelModuleOutputDirectory = rebase( outputDirectory, topLevelModule );
File topLevelModuleXmlOutputDirectory = rebase( xmlOutputDirectory, topLevelModule );

mkdirs( topLevelModuleOutputDirectory );
mkdirs( topLevelModuleXmlOutputDirectory );
mkdirs( executionRootOutputDirectory );
mkdirs( executionRootXmlOutputDirectory );

getLog().info( "Generating coverage aggregated reports..." );
writeReports( coverage, sourceRoots, topLevelModuleXmlOutputDirectory, topLevelModuleXmlOutputDirectory,
topLevelModuleOutputDirectory );
writeReports( coverage, sourceRoots, executionRootXmlOutputDirectory, executionRootXmlOutputDirectory,
executionRootOutputDirectory );
getLog().info( "Coverage aggregated reports completed." );
}

Expand Down Expand Up @@ -610,10 +584,15 @@ else if ( directory.exists() && !directory.isDirectory() )
}
}

private File rebase( File file, MavenProject otherModule )
private File rebase( File file, File otherProjectDir )
{
return new File( file.getAbsolutePath().replace( project.getBasedir().getAbsolutePath(),
otherModule.getBasedir().getAbsolutePath() ) );
otherProjectDir.getAbsolutePath() ) );
}

private File executionRootDirectory()
{
return new File(session.getExecutionRootDirectory());
}

}