22
22
import java .io .FileOutputStream ;
23
23
import java .io .IOException ;
24
24
import java .io .OutputStreamWriter ;
25
- import java .util .HashMap ;
26
- import java .util .LinkedHashSet ;
27
- import java .util .List ;
28
- import java .util .Map ;
29
- import java .util .Properties ;
30
- import java .util .Set ;
25
+ import java .util .*;
26
+ import java .util .stream .Collectors ;
27
+ import java .util .stream .Stream ;
31
28
29
+ import edu .emory .mathcs .backport .java .util .Arrays ;
32
30
import org .apache .maven .artifact .Artifact ;
33
31
import org .apache .maven .artifact .factory .ArtifactFactory ;
34
32
import org .apache .maven .artifact .repository .ArtifactRepository ;
35
33
import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
36
34
import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
37
35
import org .apache .maven .artifact .resolver .ArtifactResolver ;
36
+ import org .apache .maven .artifact .versioning .ArtifactVersion ;
37
+ import org .apache .maven .artifact .versioning .DefaultArtifactVersion ;
38
38
import org .apache .maven .model .Dependency ;
39
39
import org .apache .maven .plugin .AbstractMojo ;
40
40
import org .apache .maven .plugin .MojoExecutionException ;
@@ -286,20 +286,19 @@ else if ( "2.13".equals( resolvedScalaVersion ) || resolvedScalaVersion.startsWi
286
286
287
287
try
288
288
{
289
- Artifact pluginArtifact = getScalaScoveragePluginArtifact ( resolvedScalaVersion , scalaBinaryVersion );
289
+ List < Artifact > pluginArtifacts = getScalaScoveragePluginArtifacts ( resolvedScalaVersion , scalaBinaryVersion );
290
290
Artifact runtimeArtifact = getScalaScoverageRuntimeArtifact ( scalaBinaryVersion );
291
291
292
- if ( pluginArtifact == null )
293
- {
294
- return ; // scoverage plugin will not be configured
295
- }
296
-
297
292
addScoverageDependenciesToClasspath ( runtimeArtifact );
298
293
299
294
String arg = DATA_DIR_OPTION + dataDirectory .getAbsolutePath ();
300
295
String _scalacOptions = quoteArgument ( arg );
301
296
String addScalacArgs = arg ;
302
297
298
+ arg = SOURCE_ROOT_OPTION + project .getBasedir ().getAbsolutePath ();
299
+ _scalacOptions = _scalacOptions + SPACE + quoteArgument ( arg );
300
+ addScalacArgs = addScalacArgs + PIPE + arg ;
301
+
303
302
if ( !StringUtils .isEmpty ( excludedPackages ) )
304
303
{
305
304
arg = EXCLUDED_PACKAGES_OPTION + excludedPackages .replace ( "(empty)" , "<empty>" );
@@ -320,11 +319,10 @@ else if ( "2.13".equals( resolvedScalaVersion ) || resolvedScalaVersion.startsWi
320
319
addScalacArgs = addScalacArgs + PIPE + "-Yrangepos" ;
321
320
}
322
321
323
- String _scalacPlugins =
324
- String .format ( "%s:%s:%s" , pluginArtifact .getGroupId (), pluginArtifact .getArtifactId (),
325
- pluginArtifact .getVersion () );
322
+ String _scalacPlugins = pluginArtifacts .stream ()
323
+ .map (x -> String .format ("%s:%s:%s" , x .getGroupId (), x .getArtifactId (),x .getVersion ())).collect (Collectors .joining (" " ));
326
324
327
- arg = PLUGIN_OPTION + pluginArtifact . getFile ().getAbsolutePath ();
325
+ arg = PLUGIN_OPTION + pluginArtifacts . stream (). map ( x -> x . getFile ().getAbsolutePath ()). collect ( Collectors . joining ( String . valueOf ( java . io . File . pathSeparatorChar )) );
328
326
addScalacArgs = addScalacArgs + PIPE + arg ;
329
327
330
328
Properties projectProperties = project .getProperties ();
@@ -371,6 +369,7 @@ else if ( "2.13".equals( resolvedScalaVersion ) || resolvedScalaVersion.startsWi
371
369
private static final String SCALA_LIBRARY_ARTIFACT_ID = "scala-library" ;
372
370
373
371
private static final String DATA_DIR_OPTION = "-P:scoverage:dataDir:" ;
372
+ private static final String SOURCE_ROOT_OPTION = "-P:scoverage:sourceRoot:" ;
374
373
private static final String EXCLUDED_PACKAGES_OPTION = "-P:scoverage:excludedPackages:" ;
375
374
private static final String EXCLUDED_FILES_OPTION = "-P:scoverage:excludedFiles:" ;
376
375
private static final String PLUGIN_OPTION = "-Xplugin:" ;
@@ -427,7 +426,7 @@ private void setProperty( Properties projectProperties, String propertyName, Str
427
426
}
428
427
}
429
428
430
- private Artifact getScalaScoveragePluginArtifact ( String resolvedScalaVersion , String scalaMainVersion )
429
+ private List < Artifact > getScalaScoveragePluginArtifacts ( String resolvedScalaVersion , String scalaMainVersion )
431
430
throws ArtifactNotFoundException , ArtifactResolutionException
432
431
{
433
432
String resolvedScalacPluginVersion = scalacPluginVersion ;
@@ -444,22 +443,39 @@ private Artifact getScalaScoveragePluginArtifact( String resolvedScalaVersion, S
444
443
}
445
444
}
446
445
447
- try
446
+ // There are 3 cases depending on the version of scalac-scoverage-plugin
447
+ // * Version 2.0.0 onwards - 3 artifacts, plugin using resolvedScalaVersion
448
+ // * Version 1.4.2 - 1 artifact, plugin using resolvedScalaVersion falling back to scalaMainVersion
449
+ // * Version 1.4.1 older - 1 artifact, plugin using scalaMainVersion
450
+ //
451
+ final ArtifactVersion pluginArtifactVersion = new DefaultArtifactVersion (resolvedScalacPluginVersion );
452
+ if (pluginArtifactVersion .getMajorVersion () >= 2 ) {
453
+ List <Artifact > resolvedArtifacts = new ArrayList <>();
454
+ resolvedArtifacts .add (getResolvedArtifact ("org.scoverage" , "scalac-scoverage-plugin_" + resolvedScalaVersion , resolvedScalacPluginVersion ));
455
+ resolvedArtifacts .add (getResolvedArtifact ("org.scoverage" , "scalac-scoverage-domain_" + scalaMainVersion , resolvedScalacPluginVersion ));
456
+ resolvedArtifacts .add (getResolvedArtifact ("org.scoverage" , "scalac-scoverage-serializer_" + scalaMainVersion , resolvedScalacPluginVersion ));
457
+ return resolvedArtifacts ;
458
+ } else if (pluginArtifactVersion .getMajorVersion () == 1 && pluginArtifactVersion .getMinorVersion () == 4 && pluginArtifactVersion .getIncrementalVersion () == 2 )
448
459
{
449
- return getResolvedArtifact (
450
- "org.scoverage" , "scalac-scoverage-plugin_" + resolvedScalaVersion ,
451
- resolvedScalacPluginVersion );
452
- }
453
- catch ( ArtifactNotFoundException | ArtifactResolutionException e )
454
- {
455
- getLog ().warn ( String .format ( "Artifact \" org.scoverage:scalac-scoverage-plugin_%s:%s\" not found, " +
456
- "falling back to \" org.scoverage:scalac-scoverage-plugin_%s:%s\" " ,
457
- resolvedScalaVersion , resolvedScalacPluginVersion , scalaMainVersion , resolvedScalacPluginVersion ) );
458
-
459
- // for scalac-scoverage-plugin versions up to 1.4.1
460
- return getResolvedArtifact (
461
- "org.scoverage" , "scalac-scoverage-plugin_" + scalaMainVersion ,
462
- resolvedScalacPluginVersion );
460
+ try
461
+ {
462
+ return Collections .singletonList (
463
+ getResolvedArtifact ("org.scoverage" , "scalac-scoverage-plugin_" + resolvedScalaVersion , resolvedScalacPluginVersion )
464
+ );
465
+ }
466
+ catch ( ArtifactNotFoundException | ArtifactResolutionException e2 )
467
+ {
468
+ getLog ().warn ( String .format ( "Artifact \" org.scoverage:scalac-scoverage-plugin_%s:%s\" not found, " +
469
+ "falling back to \" org.scoverage:scalac-scoverage-plugin_%s:%s\" " ,
470
+ resolvedScalaVersion , resolvedScalacPluginVersion , scalaMainVersion , resolvedScalacPluginVersion ) );
471
+ return Collections .singletonList (
472
+ getResolvedArtifact ("org.scoverage" , "scalac-scoverage-plugin_" + scalaMainVersion , resolvedScalacPluginVersion )
473
+ );
474
+ }
475
+ } else {
476
+ return Collections .singletonList (
477
+ getResolvedArtifact ("org.scoverage" , "scalac-scoverage-plugin_" + scalaMainVersion , resolvedScalacPluginVersion )
478
+ );
463
479
}
464
480
}
465
481
0 commit comments