23
23
import java .io .File ;
24
24
import java .io .FileInputStream ;
25
25
import java .util .ArrayList ;
26
- import java .util .ArrayDeque ;
27
26
import java .util .Arrays ;
28
27
import java .util .List ;
29
28
import java .util .Locale ;
30
- import java .util .Queue ;
31
29
import java .util .ResourceBundle ;
32
30
import java .util .concurrent .atomic .AtomicInteger ;
33
31
34
32
import org .apache .maven .doxia .siterenderer .RenderingContext ;
35
33
import org .apache .maven .doxia .siterenderer .sink .SiteRendererSink ;
36
34
35
+ import org .apache .maven .execution .MavenSession ;
37
36
import org .apache .maven .plugin .AbstractMojo ;
38
37
import org .apache .maven .plugin .MojoExecutionException ;
39
38
import org .apache .maven .plugins .annotations .Execute ;
58
57
import scoverage .reporter .IOUtils ;
59
58
import scoverage .serialize .Serializer ;
60
59
import scoverage .reporter .CoberturaXmlWriter ;
61
- import scoverage .reporter .CoverageAggregator ;
62
60
import scoverage .reporter .ScoverageHtmlWriter ;
63
61
import scoverage .reporter .ScoverageXmlWriter ;
64
62
@@ -131,6 +129,12 @@ public class SCoverageReportMojo
131
129
@ Parameter ( defaultValue = "${project}" , readonly = true , required = true )
132
130
private MavenProject project ;
133
131
132
+ /**
133
+ * The current Maven session.
134
+ */
135
+ @ Parameter (defaultValue = "${session}" , readonly = true , required = true )
136
+ private MavenSession session ;
137
+
134
138
/**
135
139
* All Maven projects in the reactor.
136
140
*/
@@ -423,7 +427,7 @@ private void generateReports()
423
427
424
428
File coverageFile = Serializer .coverageFile ( dataDirectory );
425
429
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 () );
427
431
428
432
getLog ().info ( String .format ( "Reading scoverage measurements [%s*]..." ,
429
433
new File ( dataDirectory , Constants .MeasurementsPrefix () ).getAbsolutePath () ) );
@@ -444,22 +448,17 @@ private void generateAggregatedReports()
444
448
AtomicInteger id = new AtomicInteger ();
445
449
List <File > scoverageDataDirs = new ArrayList <File >();
446
450
List <File > sourceRoots = new ArrayList <File >();
447
- MavenProject topLevelModule = null ;
448
451
for ( MavenProject module : reactorProjects )
449
452
{
450
- if ( module .isExecutionRoot () )
451
- {
452
- topLevelModule = module ;
453
- }
454
- else if ( !module .getPackaging ().equals ( "pom" ) )
453
+ if ( !module .getPackaging ().equals ( "pom" ) )
455
454
{
456
- File scoverageDataDir = rebase ( dataDirectory , module );
455
+ File scoverageDataDir = rebase ( dataDirectory , module . getBasedir () );
457
456
if ( scoverageDataDir .isDirectory () )
458
457
{
459
458
scoverageDataDirs .add ( scoverageDataDir );
460
459
File coverageFile = Serializer .coverageFile (scoverageDataDir );
461
460
if (coverageFile .exists ()) {
462
- Coverage subCoverage = Serializer .deserialize (coverageFile , module . getBasedir () );
461
+ Coverage subCoverage = Serializer .deserialize (coverageFile , executionRootDirectory () );
463
462
List <File > measurementFiles = Arrays .asList ( IOUtils .findMeasurementFiles ( scoverageDataDir ) );
464
463
scala .collection .Set <Tuple2 <Object , String >> measurements =
465
464
IOUtils .invoked ( CollectionConverters .asScala ( measurementFiles ).toSeq (), encoding );
@@ -537,40 +536,15 @@ else if ( !module.getPackaging().equals( "pom" ) )
537
536
scoverageDataDirs .size () ) );
538
537
}
539
538
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 () );
564
541
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 );
570
544
571
545
getLog ().info ( "Generating coverage aggregated reports..." );
572
- writeReports ( coverage , sourceRoots , topLevelModuleXmlOutputDirectory , topLevelModuleXmlOutputDirectory ,
573
- topLevelModuleOutputDirectory );
546
+ writeReports ( coverage , sourceRoots , executionRootXmlOutputDirectory , executionRootXmlOutputDirectory ,
547
+ executionRootOutputDirectory );
574
548
getLog ().info ( "Coverage aggregated reports completed." );
575
549
}
576
550
@@ -610,10 +584,15 @@ else if ( directory.exists() && !directory.isDirectory() )
610
584
}
611
585
}
612
586
613
- private File rebase ( File file , MavenProject otherModule )
587
+ private File rebase ( File file , File otherProjectDir )
614
588
{
615
589
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 ());
617
596
}
618
597
619
598
}
0 commit comments