2323import java .io .File ;
2424import java .io .FileInputStream ;
2525import java .util .ArrayList ;
26- import java .util .ArrayDeque ;
2726import java .util .Arrays ;
2827import java .util .List ;
2928import java .util .Locale ;
30- import java .util .Queue ;
3129import java .util .ResourceBundle ;
3230import java .util .concurrent .atomic .AtomicInteger ;
3331
3432import org .apache .maven .doxia .siterenderer .RenderingContext ;
3533import org .apache .maven .doxia .siterenderer .sink .SiteRendererSink ;
3634
35+ import org .apache .maven .execution .MavenSession ;
3736import org .apache .maven .plugin .AbstractMojo ;
3837import org .apache .maven .plugin .MojoExecutionException ;
3938import org .apache .maven .plugins .annotations .Execute ;
5857import scoverage .reporter .IOUtils ;
5958import scoverage .serialize .Serializer ;
6059import scoverage .reporter .CoberturaXmlWriter ;
61- import scoverage .reporter .CoverageAggregator ;
6260import scoverage .reporter .ScoverageHtmlWriter ;
6361import 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 , executionRootDirectory () );
427431
428432 getLog ().info ( String .format ( "Reading scoverage measurements [%s*]..." ,
429433 new File ( dataDirectory , Constants .MeasurementsPrefix () ).getAbsolutePath () ) );
@@ -444,22 +448,17 @@ 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 ;
448451 for ( MavenProject module : reactorProjects )
449452 {
450- if ( module .isExecutionRoot () )
451- {
452- topLevelModule = module ;
453- }
454- else if ( !module .getPackaging ().equals ( "pom" ) )
453+ if ( !module .getPackaging ().equals ( "pom" ) )
455454 {
456- File scoverageDataDir = rebase ( dataDirectory , module );
455+ File scoverageDataDir = rebase ( dataDirectory , module . getBasedir () );
457456 if ( scoverageDataDir .isDirectory () )
458457 {
459458 scoverageDataDirs .add ( scoverageDataDir );
460459 File coverageFile = Serializer .coverageFile (scoverageDataDir );
461460 if (coverageFile .exists ()) {
462- Coverage subCoverage = Serializer .deserialize (coverageFile , module . getBasedir () );
461+ Coverage subCoverage = Serializer .deserialize (coverageFile , executionRootDirectory () );
463462 List <File > measurementFiles = Arrays .asList ( IOUtils .findMeasurementFiles ( scoverageDataDir ) );
464463 scala .collection .Set <Tuple2 <Object , String >> measurements =
465464 IOUtils .invoked ( CollectionConverters .asScala ( measurementFiles ).toSeq (), encoding );
@@ -537,40 +536,15 @@ else if ( !module.getPackaging().equals( "pom" ) )
537536 scoverageDataDirs .size () ) );
538537 }
539538
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- }
539+ File executionRootOutputDirectory = rebase ( outputDirectory , executionRootDirectory () );
540+ File executionRootXmlOutputDirectory = rebase ( xmlOutputDirectory , executionRootDirectory () );
564541
565- File topLevelModuleOutputDirectory = rebase ( outputDirectory , topLevelModule );
566- File topLevelModuleXmlOutputDirectory = rebase ( xmlOutputDirectory , topLevelModule );
567-
568- mkdirs ( topLevelModuleOutputDirectory );
569- mkdirs ( topLevelModuleXmlOutputDirectory );
542+ mkdirs ( executionRootOutputDirectory );
543+ mkdirs ( executionRootXmlOutputDirectory );
570544
571545 getLog ().info ( "Generating coverage aggregated reports..." );
572- writeReports ( coverage , sourceRoots , topLevelModuleXmlOutputDirectory , topLevelModuleXmlOutputDirectory ,
573- topLevelModuleOutputDirectory );
546+ writeReports ( coverage , sourceRoots , executionRootXmlOutputDirectory , executionRootXmlOutputDirectory ,
547+ executionRootOutputDirectory );
574548 getLog ().info ( "Coverage aggregated reports completed." );
575549 }
576550
@@ -610,10 +584,15 @@ else if ( directory.exists() && !directory.isDirectory() )
610584 }
611585 }
612586
613- private File rebase ( File file , MavenProject otherModule )
587+ private File rebase ( File file , File otherProjectDir )
614588 {
615589 return new File ( file .getAbsolutePath ().replace ( project .getBasedir ().getAbsolutePath (),
616- otherModule .getBasedir ().getAbsolutePath () ) );
590+ otherProjectDir .getAbsolutePath () ) );
591+ }
592+
593+ private File executionRootDirectory ()
594+ {
595+ return new File (session .getExecutionRootDirectory ());
617596 }
618597
619598}
0 commit comments