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 ;
@@ -106,7 +105,7 @@ public class SCoveragePreCompileMojo
106105 *
107106 * @since 1.0.0
108107 */
109- @ Parameter ( property = "scoverage.excludedPackages" , defaultValue = "" )
108+ @ Parameter ( property = "scoverage.excludedPackages" )
110109 private String excludedPackages ;
111110
112111 /**
@@ -115,7 +114,7 @@ public class SCoveragePreCompileMojo
115114 *
116115 * @since 1.0.0
117116 */
118- @ Parameter ( property = "scoverage.excludedFiles" , defaultValue = "" )
117+ @ Parameter ( property = "scoverage.excludedFiles" )
119118 private String excludedFiles ;
120119
121120 /**
@@ -147,7 +146,7 @@ public class SCoveragePreCompileMojo
147146 *
148147 * @since 1.4.0
149148 */
150- @ Parameter ( property = "scoverage.additionalForkedProjectProperties" , defaultValue = "" )
149+ @ Parameter ( property = "scoverage.additionalForkedProjectProperties" )
151150 private String additionalForkedProjectProperties ;
152151
153152 /**
@@ -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 /**
@@ -192,12 +191,6 @@ public class SCoveragePreCompileMojo
192191 @ Parameter ( property = "project.remoteArtifactRepositories" , readonly = true , required = true )
193192 private List <ArtifactRepository > remoteRepos ;
194193
195- /**
196- * List of artifacts this plugin depends on.
197- */
198- @ Parameter ( property = "plugin.artifacts" , readonly = true , required = true )
199- private List <Artifact > pluginArtifacts ;
200-
201194 /**
202195 * Configures project for compilation with SCoverage instrumentation.
203196 *
@@ -231,16 +224,16 @@ public void execute() throws MojoExecutionException
231224
232225 long ts = System .currentTimeMillis ();
233226
234- ScalaVersion resolvedScalaVersion = resolveScalaVersion ();
227+ ScalaVersion scalaVersion = resolveScalaVersion ();
235228
236- if ( resolvedScalaVersion != null )
229+ if ( scalaVersion != null )
237230 {
238- boolean supportedScalaVersion = resolvedScalaVersion .isScala2 () && resolvedScalaVersion .isAtLeast ( "2.12.8" ) ||
239- resolvedScalaVersion .isAtLeast ( "3.2.0" );
231+ boolean supportedScalaVersion = scalaVersion .isScala2 () && scalaVersion .isAtLeast ( "2.12.8" ) ||
232+ scalaVersion .isAtLeast ( "3.2.0" );
240233 if (!supportedScalaVersion )
241234 {
242235 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 ) );
236+ scalaVersion .full ) );
244237 return ;
245238 }
246239 }
@@ -254,7 +247,7 @@ public void execute() throws MojoExecutionException
254247 if ( additionalForkedProjectProperties != null && !additionalForkedProjectProperties .isEmpty () )
255248 {
256249 String [] props = additionalForkedProjectProperties .split ( ";" );
257- additionalProjectPropertiesMap = new HashMap <String , String >( props .length );
250+ additionalProjectPropertiesMap = new HashMap <>( props .length );
258251 for ( String propVal : props )
259252 {
260253 String [] tmp = propVal .split ( "=" , 2 );
@@ -277,17 +270,16 @@ public void execute() throws MojoExecutionException
277270
278271 try
279272 {
280- boolean scala2 = resolvedScalaVersion .isScala2 ();
273+ boolean scala2 = scalaVersion .isScala2 ();
281274 boolean filePackageExclusionSupportingScala3 =
282- resolvedScalaVersion .isAtLeast ( "3.4.2" ) ||
275+ scalaVersion .isAtLeast ( "3.4.2" ) ||
283276 // backported to Scala 3.3 LTS
284- ( resolvedScalaVersion .full .startsWith ( "3.3." ) && resolvedScalaVersion .isAtLeast ( "3.3.4" ) );
277+ ( scalaVersion .full .startsWith ( "3.3." ) && scalaVersion .isAtLeast ( "3.3.4" ) );
285278
286- List <Artifact > pluginArtifacts = getScalaScoveragePluginArtifacts ( resolvedScalaVersion );
279+ List <Artifact > pluginArtifacts = getScoveragePluginArtifacts ( scalaVersion );
287280 if ( scala2 ) // Scala 3 doesn't need scalac-scoverage-runtime
288281 {
289- Artifact runtimeArtifact = getScalaScoverageRuntimeArtifact ( resolvedScalaVersion );
290- addScoverageDependenciesToClasspath ( runtimeArtifact );
282+ addScalacScoverageRuntimeDependencyToClasspath ( scalaVersion );
291283 }
292284
293285 String arg = ( scala2 ? SCALA2_DATA_DIR_OPTION : SCALA3_COVERAGE_OUT_OPTION ) + dataDirectory .getAbsolutePath ();
@@ -404,7 +396,6 @@ private ScalaVersion resolveScalaVersion()
404396 if ( result == null || result .isEmpty () )
405397 {
406398 // check project direct dependencies (transitive dependencies cannot be checked in this Maven lifecycle phase)
407- @ SuppressWarnings ( "unchecked" )
408399 List <Dependency > dependencies = project .getDependencies ();
409400 for ( Dependency dependency : dependencies )
410401 {
@@ -445,55 +436,46 @@ private void setProperty( Properties projectProperties, String propertyName, Str
445436 }
446437 }
447438
448- private ArtifactVersion getScalacPluginVersion () {
449- if ( scalacPluginVersion == null || scalacPluginVersion .isEmpty () ) {
439+ private String getScalacPluginVersion () {
440+ if ( StringUtils .isEmpty (scalacPluginVersion ) ) {
450441 throw new IllegalStateException ("scalacPluginVersion is unset." );
451442 } else if ( scalacPluginVersion .startsWith ("1." ) ) {
452443 throw new IllegalStateException ( String .format ( "Unsupported scalacPluginVersion \" %s\" . Please use scalacPluginVersion 2.0.0+ or use older version of scoverage-maven-plugin" , scalacPluginVersion ) );
453444 } else {
454- return new DefaultArtifactVersion ( scalacPluginVersion ) ;
445+ return scalacPluginVersion ;
455446 }
456447 }
457448
458- private List <Artifact > getScalaScoveragePluginArtifacts ( ScalaVersion resolvedScalaVersion )
449+ private List <Artifact > getScoveragePluginArtifacts ( ScalaVersion scalaVersion )
459450 throws ArtifactNotFoundException , ArtifactResolutionException
460451 {
461- String resolvedScalacPluginVersion = getScalacPluginVersion ().toString ();
462452 List <Artifact > resolvedArtifacts = new ArrayList <>();
463- if ( resolvedScalaVersion .isScala2 () ) // Scala 3 doesn't need scalac-scoverage-plugin
453+ if ( scalaVersion .isScala2 () ) // Scala 3 doesn't need scalac-scoverage-plugin
464454 {
465- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-plugin_" + resolvedScalaVersion .full , resolvedScalacPluginVersion ));
455+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-plugin_" + scalaVersion .full ));
466456 }
467- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-domain_" + resolvedScalaVersion .compatible , resolvedScalacPluginVersion ));
468- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-serializer_" + resolvedScalaVersion .compatible , resolvedScalacPluginVersion ));
457+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-domain_" + scalaVersion .compatible ));
458+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-serializer_" + scalaVersion .compatible ));
469459 return resolvedArtifacts ;
470460 }
471461
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-
480462 /**
481463 * We need to tweak our test classpath for Scoverage.
482- *
483- * @throws MojoExecutionException
484464 */
485- private void addScoverageDependenciesToClasspath ( Artifact scalaScoveragePluginArtifact )
486- {
487- @ SuppressWarnings ( "unchecked" )
488- Set <Artifact > set = new LinkedHashSet <Artifact >( project .getDependencyArtifacts () );
489- set .add ( scalaScoveragePluginArtifact );
465+ private void addScalacScoverageRuntimeDependencyToClasspath ( ScalaVersion resolvedScalaVersion )
466+ throws ArtifactResolutionException , ArtifactNotFoundException {
467+
468+ Set <Artifact > set = new LinkedHashSet <>( project .getDependencyArtifacts ());
469+ set .add (resolveScoverageArtifact ( "scalac-scoverage-runtime_" + resolvedScalaVersion . compatible ) );
490470 project .setDependencyArtifacts ( set );
491471 }
492472
493- private Artifact getResolvedArtifact ( String groupId , String artifactId , String version )
473+ private Artifact resolveScoverageArtifact ( String artifactId )
494474 throws ArtifactNotFoundException , ArtifactResolutionException
495475 {
496- Artifact artifact = factory .createArtifact ( groupId , artifactId , version , Artifact .SCOPE_COMPILE , "jar" );
476+ Artifact artifact = factory .createArtifact (
477+ "org.scoverage" , artifactId , getScalacPluginVersion (), Artifact .SCOPE_COMPILE , "jar"
478+ );
497479 resolver .resolve ( artifact , remoteRepos , localRepo );
498480 return artifact ;
499481 }
@@ -509,21 +491,13 @@ private void saveSourceRootsToFile() throws IOException
509491 dataDirectory .getAbsolutePath () ) );
510492 }
511493 File sourceRootsFile = new File ( dataDirectory , "source.roots" );
512- BufferedWriter writer = new BufferedWriter (
513- new OutputStreamWriter ( new FileOutputStream ( sourceRootsFile ), "UTF-8" ) );
514- try
515- {
516- for ( String sourceRoot : sourceRoots )
517- {
494+ try ( BufferedWriter writer = new BufferedWriter (
495+ new OutputStreamWriter ( new FileOutputStream (sourceRootsFile ), StandardCharsets .UTF_8 ))) {
496+ for ( String sourceRoot : sourceRoots ) {
518497 writer .write ( sourceRoot );
519498 writer .newLine ();
520499 }
521500 }
522- finally
523- {
524- writer .close ();
525- }
526501 }
527502 }
528-
529503}
0 commit comments