2222import java .io .FileOutputStream ;
2323import java .io .IOException ;
2424import java .io .OutputStreamWriter ;
25+ import java .nio .charset .StandardCharsets ;
2526import java .util .*;
2627import java .util .stream .Collectors ;
28+ import javax .inject .Inject ;
2729
2830import org .apache .maven .artifact .Artifact ;
2931import org .apache .maven .artifact .factory .ArtifactFactory ;
3032import org .apache .maven .artifact .repository .ArtifactRepository ;
3133import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
3234import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
3335import org .apache .maven .artifact .resolver .ArtifactResolver ;
34- import org .apache .maven .artifact .versioning .ArtifactVersion ;
35- import org .apache .maven .artifact .versioning .DefaultArtifactVersion ;
3636import org .apache .maven .execution .MavenSession ;
3737import org .apache .maven .model .Dependency ;
3838import org .apache .maven .plugin .AbstractMojo ;
3939import org .apache .maven .plugin .MojoExecutionException ;
40- import org .apache .maven .plugins .annotations .Component ;
4140import org .apache .maven .plugins .annotations .LifecyclePhase ;
4241import org .apache .maven .plugins .annotations .Mojo ;
4342import org .apache .maven .plugins .annotations .Parameter ;
@@ -171,13 +170,13 @@ public class SCoveragePreCompileMojo
171170 /**
172171 * Artifact factory used to look up artifacts in the remote repository.
173172 */
174- @ Component
173+ @ Inject
175174 private ArtifactFactory factory ;
176175
177176 /**
178177 * Artifact resolver used to resolve artifacts.
179178 */
180- @ Component
179+ @ Inject
181180 private ArtifactResolver resolver ;
182181
183182 /**
@@ -231,16 +230,16 @@ public void execute() throws MojoExecutionException
231230
232231 long ts = System .currentTimeMillis ();
233232
234- ScalaVersion resolvedScalaVersion = resolveScalaVersion ();
233+ ScalaVersion scalaVersion = resolveScalaVersion ();
235234
236- if ( resolvedScalaVersion != null )
235+ if ( scalaVersion != null )
237236 {
238- boolean supportedScalaVersion = resolvedScalaVersion .isScala2 () && resolvedScalaVersion .isAtLeast ( "2.12.8" ) ||
239- resolvedScalaVersion .isAtLeast ( "3.2.0" );
237+ boolean supportedScalaVersion = scalaVersion .isScala2 () && scalaVersion .isAtLeast ( "2.12.8" ) ||
238+ scalaVersion .isAtLeast ( "3.2.0" );
240239 if (!supportedScalaVersion )
241240 {
242241 getLog ().warn ( String .format ( "Skipping SCoverage execution - unsupported Scala version \" %s\" . Supported Scala versions are 2.12.8+, 2.13.0+ and 3.2.0+ ." ,
243- resolvedScalaVersion .full ) );
242+ scalaVersion .full ) );
244243 return ;
245244 }
246245 }
@@ -277,17 +276,16 @@ public void execute() throws MojoExecutionException
277276
278277 try
279278 {
280- boolean scala2 = resolvedScalaVersion .isScala2 ();
279+ boolean scala2 = scalaVersion .isScala2 ();
281280 boolean filePackageExclusionSupportingScala3 =
282- resolvedScalaVersion .isAtLeast ( "3.4.2" ) ||
281+ scalaVersion .isAtLeast ( "3.4.2" ) ||
283282 // backported to Scala 3.3 LTS
284- ( resolvedScalaVersion .full .startsWith ( "3.3." ) && resolvedScalaVersion .isAtLeast ( "3.3.4" ) );
283+ ( scalaVersion .full .startsWith ( "3.3." ) && scalaVersion .isAtLeast ( "3.3.4" ) );
285284
286- List <Artifact > pluginArtifacts = getScalaScoveragePluginArtifacts ( resolvedScalaVersion );
285+ List <Artifact > pluginArtifacts = getScoveragePluginArtifacts ( scalaVersion );
287286 if ( scala2 ) // Scala 3 doesn't need scalac-scoverage-runtime
288287 {
289- Artifact runtimeArtifact = getScalaScoverageRuntimeArtifact ( resolvedScalaVersion );
290- addScoverageDependenciesToClasspath ( runtimeArtifact );
288+ addScalacScoverageRuntimeDependencyToClasspath ( scalaVersion );
291289 }
292290
293291 String arg = ( scala2 ? SCALA2_DATA_DIR_OPTION : SCALA3_COVERAGE_OUT_OPTION ) + dataDirectory .getAbsolutePath ();
@@ -445,55 +443,47 @@ private void setProperty( Properties projectProperties, String propertyName, Str
445443 }
446444 }
447445
448- private ArtifactVersion getScalacPluginVersion () {
449- if ( scalacPluginVersion == null || scalacPluginVersion .isEmpty () ) {
446+ private String getScalacPluginVersion () {
447+ if ( StringUtils .isEmpty (scalacPluginVersion ) ) {
450448 throw new IllegalStateException ("scalacPluginVersion is unset." );
451449 } else if ( scalacPluginVersion .startsWith ("1." ) ) {
452450 throw new IllegalStateException ( String .format ( "Unsupported scalacPluginVersion \" %s\" . Please use scalacPluginVersion 2.0.0+ or use older version of scoverage-maven-plugin" , scalacPluginVersion ) );
453451 } else {
454- return new DefaultArtifactVersion ( scalacPluginVersion ) ;
452+ return scalacPluginVersion ;
455453 }
456454 }
457455
458- private List <Artifact > getScalaScoveragePluginArtifacts ( ScalaVersion resolvedScalaVersion )
456+ private List <Artifact > getScoveragePluginArtifacts ( ScalaVersion scalaVersion )
459457 throws ArtifactNotFoundException , ArtifactResolutionException
460458 {
461- String resolvedScalacPluginVersion = getScalacPluginVersion ().toString ();
462459 List <Artifact > resolvedArtifacts = new ArrayList <>();
463- if ( resolvedScalaVersion .isScala2 () ) // Scala 3 doesn't need scalac-scoverage-plugin
460+ if ( scalaVersion .isScala2 () ) // Scala 3 doesn't need scalac-scoverage-plugin
464461 {
465- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-plugin_" + resolvedScalaVersion .full , resolvedScalacPluginVersion ));
462+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-plugin_" + scalaVersion .full ));
466463 }
467- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-domain_" + resolvedScalaVersion .compatible , resolvedScalacPluginVersion ));
468- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-serializer_" + resolvedScalaVersion .compatible , resolvedScalacPluginVersion ));
464+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-domain_" + scalaVersion .compatible ));
465+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-serializer_" + scalaVersion .compatible ));
469466 return resolvedArtifacts ;
470467 }
471468
472- private Artifact getScalaScoverageRuntimeArtifact ( ScalaVersion resolvedScalaVersion )
473- throws ArtifactNotFoundException , ArtifactResolutionException
474- {
475- return getResolvedArtifact (
476- "org.scoverage" , "scalac-scoverage-runtime_" + resolvedScalaVersion .compatible ,
477- getScalacPluginVersion ().toString () );
478- }
479-
480469 /**
481470 * We need to tweak our test classpath for Scoverage.
482- *
483- * @throws MojoExecutionException
484471 */
485- private void addScoverageDependenciesToClasspath ( Artifact scalaScoveragePluginArtifact )
486- {
472+ private void addScalacScoverageRuntimeDependencyToClasspath (ScalaVersion resolvedScalaVersion )
473+ throws ArtifactResolutionException , ArtifactNotFoundException {
474+
487475 @ SuppressWarnings ( "unchecked" )
488476 Set <Artifact > set = new LinkedHashSet <Artifact >( project .getDependencyArtifacts () );
489- set .add ( scalaScoveragePluginArtifact );
477+ set .add (resolveScoverageArtifact ( "scalac-scoverage-runtime_" + resolvedScalaVersion . compatible ) );
490478 project .setDependencyArtifacts ( set );
491479 }
492480
493- private Artifact getResolvedArtifact ( String groupId , String artifactId , String version )
481+ private Artifact resolveScoverageArtifact ( String artifactId )
494482 throws ArtifactNotFoundException , ArtifactResolutionException
495483 {
496- Artifact artifact = factory .createArtifact ( groupId , artifactId , version , Artifact .SCOPE_COMPILE , "jar" );
484+ Artifact artifact = factory .createArtifact (
485+ "org.scoverage" , artifactId , getScalacPluginVersion (), Artifact .SCOPE_COMPILE , "jar"
486+ );
497487 resolver .resolve ( artifact , remoteRepos , localRepo );
498488 return artifact ;
499489 }
@@ -510,7 +500,7 @@ private void saveSourceRootsToFile() throws IOException
510500 }
511501 File sourceRootsFile = new File ( dataDirectory , "source.roots" );
512502 BufferedWriter writer = new BufferedWriter (
513- new OutputStreamWriter ( new FileOutputStream ( sourceRootsFile ), "UTF-8" ) );
503+ new OutputStreamWriter ( new FileOutputStream ( sourceRootsFile ), StandardCharsets . UTF_8 ) );
514504 try
515505 {
516506 for ( String sourceRoot : sourceRoots )
0 commit comments