2222import java .io .FileOutputStream ;
2323import java .io .IOException ;
2424import 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 ;
3128
29+ import edu .emory .mathcs .backport .java .util .Arrays ;
3230import org .apache .maven .artifact .Artifact ;
3331import org .apache .maven .artifact .factory .ArtifactFactory ;
3432import org .apache .maven .artifact .repository .ArtifactRepository ;
3533import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
3634import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
3735import org .apache .maven .artifact .resolver .ArtifactResolver ;
36+ import org .apache .maven .artifact .versioning .ArtifactVersion ;
37+ import org .apache .maven .artifact .versioning .DefaultArtifactVersion ;
3838import org .apache .maven .model .Dependency ;
3939import org .apache .maven .plugin .AbstractMojo ;
4040import org .apache .maven .plugin .MojoExecutionException ;
@@ -286,20 +286,19 @@ else if ( "2.13".equals( resolvedScalaVersion ) || resolvedScalaVersion.startsWi
286286
287287 try
288288 {
289- Artifact pluginArtifact = getScalaScoveragePluginArtifact ( resolvedScalaVersion , scalaBinaryVersion );
289+ List < Artifact > pluginArtifacts = getScalaScoveragePluginArtifacts ( resolvedScalaVersion , scalaBinaryVersion );
290290 Artifact runtimeArtifact = getScalaScoverageRuntimeArtifact ( scalaBinaryVersion );
291291
292- if ( pluginArtifact == null )
293- {
294- return ; // scoverage plugin will not be configured
295- }
296-
297292 addScoverageDependenciesToClasspath ( runtimeArtifact );
298293
299294 String arg = DATA_DIR_OPTION + dataDirectory .getAbsolutePath ();
300295 String _scalacOptions = quoteArgument ( arg );
301296 String addScalacArgs = arg ;
302297
298+ arg = SOURCE_ROOT_OPTION + project .getBasedir ().getAbsolutePath ();
299+ _scalacOptions = _scalacOptions + SPACE + quoteArgument ( arg );
300+ addScalacArgs = addScalacArgs + PIPE + arg ;
301+
303302 if ( !StringUtils .isEmpty ( excludedPackages ) )
304303 {
305304 arg = EXCLUDED_PACKAGES_OPTION + excludedPackages .replace ( "(empty)" , "<empty>" );
@@ -320,11 +319,10 @@ else if ( "2.13".equals( resolvedScalaVersion ) || resolvedScalaVersion.startsWi
320319 addScalacArgs = addScalacArgs + PIPE + "-Yrangepos" ;
321320 }
322321
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 (" " ));
326324
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 )) );
328326 addScalacArgs = addScalacArgs + PIPE + arg ;
329327
330328 Properties projectProperties = project .getProperties ();
@@ -371,6 +369,7 @@ else if ( "2.13".equals( resolvedScalaVersion ) || resolvedScalaVersion.startsWi
371369 private static final String SCALA_LIBRARY_ARTIFACT_ID = "scala-library" ;
372370
373371 private static final String DATA_DIR_OPTION = "-P:scoverage:dataDir:" ;
372+ private static final String SOURCE_ROOT_OPTION = "-P:scoverage:sourceRoot:" ;
374373 private static final String EXCLUDED_PACKAGES_OPTION = "-P:scoverage:excludedPackages:" ;
375374 private static final String EXCLUDED_FILES_OPTION = "-P:scoverage:excludedFiles:" ;
376375 private static final String PLUGIN_OPTION = "-Xplugin:" ;
@@ -427,7 +426,7 @@ private void setProperty( Properties projectProperties, String propertyName, Str
427426 }
428427 }
429428
430- private Artifact getScalaScoveragePluginArtifact ( String resolvedScalaVersion , String scalaMainVersion )
429+ private List < Artifact > getScalaScoveragePluginArtifacts ( String resolvedScalaVersion , String scalaMainVersion )
431430 throws ArtifactNotFoundException , ArtifactResolutionException
432431 {
433432 String resolvedScalacPluginVersion = scalacPluginVersion ;
@@ -444,22 +443,39 @@ private Artifact getScalaScoveragePluginArtifact( String resolvedScalaVersion, S
444443 }
445444 }
446445
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 )
448459 {
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+ );
463479 }
464480 }
465481
0 commit comments