22
22
import java .io .FileOutputStream ;
23
23
import java .io .IOException ;
24
24
import java .io .OutputStreamWriter ;
25
+ import java .nio .charset .StandardCharsets ;
25
26
import java .util .*;
26
27
import java .util .stream .Collectors ;
28
+ import javax .inject .Inject ;
27
29
28
30
import org .apache .maven .artifact .Artifact ;
29
31
import org .apache .maven .artifact .factory .ArtifactFactory ;
30
32
import org .apache .maven .artifact .repository .ArtifactRepository ;
31
33
import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
32
34
import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
33
35
import org .apache .maven .artifact .resolver .ArtifactResolver ;
34
- import org .apache .maven .artifact .versioning .ArtifactVersion ;
35
- import org .apache .maven .artifact .versioning .DefaultArtifactVersion ;
36
36
import org .apache .maven .execution .MavenSession ;
37
37
import org .apache .maven .model .Dependency ;
38
38
import org .apache .maven .plugin .AbstractMojo ;
39
39
import org .apache .maven .plugin .MojoExecutionException ;
40
- import org .apache .maven .plugins .annotations .Component ;
41
40
import org .apache .maven .plugins .annotations .LifecyclePhase ;
42
41
import org .apache .maven .plugins .annotations .Mojo ;
43
42
import org .apache .maven .plugins .annotations .Parameter ;
@@ -106,7 +105,7 @@ public class SCoveragePreCompileMojo
106
105
*
107
106
* @since 1.0.0
108
107
*/
109
- @ Parameter ( property = "scoverage.excludedPackages" , defaultValue = "" )
108
+ @ Parameter ( property = "scoverage.excludedPackages" )
110
109
private String excludedPackages ;
111
110
112
111
/**
@@ -115,7 +114,7 @@ public class SCoveragePreCompileMojo
115
114
*
116
115
* @since 1.0.0
117
116
*/
118
- @ Parameter ( property = "scoverage.excludedFiles" , defaultValue = "" )
117
+ @ Parameter ( property = "scoverage.excludedFiles" )
119
118
private String excludedFiles ;
120
119
121
120
/**
@@ -147,7 +146,7 @@ public class SCoveragePreCompileMojo
147
146
*
148
147
* @since 1.4.0
149
148
*/
150
- @ Parameter ( property = "scoverage.additionalForkedProjectProperties" , defaultValue = "" )
149
+ @ Parameter ( property = "scoverage.additionalForkedProjectProperties" )
151
150
private String additionalForkedProjectProperties ;
152
151
153
152
/**
@@ -171,13 +170,13 @@ public class SCoveragePreCompileMojo
171
170
/**
172
171
* Artifact factory used to look up artifacts in the remote repository.
173
172
*/
174
- @ Component
173
+ @ Inject
175
174
private ArtifactFactory factory ;
176
175
177
176
/**
178
177
* Artifact resolver used to resolve artifacts.
179
178
*/
180
- @ Component
179
+ @ Inject
181
180
private ArtifactResolver resolver ;
182
181
183
182
/**
@@ -192,12 +191,6 @@ public class SCoveragePreCompileMojo
192
191
@ Parameter ( property = "project.remoteArtifactRepositories" , readonly = true , required = true )
193
192
private List <ArtifactRepository > remoteRepos ;
194
193
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
-
201
194
/**
202
195
* Configures project for compilation with SCoverage instrumentation.
203
196
*
@@ -231,16 +224,16 @@ public void execute() throws MojoExecutionException
231
224
232
225
long ts = System .currentTimeMillis ();
233
226
234
- ScalaVersion resolvedScalaVersion = resolveScalaVersion ();
227
+ ScalaVersion scalaVersion = resolveScalaVersion ();
235
228
236
- if ( resolvedScalaVersion != null )
229
+ if ( scalaVersion != null )
237
230
{
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" );
240
233
if (!supportedScalaVersion )
241
234
{
242
235
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 ) );
244
237
return ;
245
238
}
246
239
}
@@ -254,7 +247,7 @@ public void execute() throws MojoExecutionException
254
247
if ( additionalForkedProjectProperties != null && !additionalForkedProjectProperties .isEmpty () )
255
248
{
256
249
String [] props = additionalForkedProjectProperties .split ( ";" );
257
- additionalProjectPropertiesMap = new HashMap <String , String >( props .length );
250
+ additionalProjectPropertiesMap = new HashMap <>( props .length );
258
251
for ( String propVal : props )
259
252
{
260
253
String [] tmp = propVal .split ( "=" , 2 );
@@ -277,17 +270,16 @@ public void execute() throws MojoExecutionException
277
270
278
271
try
279
272
{
280
- boolean scala2 = resolvedScalaVersion .isScala2 ();
273
+ boolean scala2 = scalaVersion .isScala2 ();
281
274
boolean filePackageExclusionSupportingScala3 =
282
- resolvedScalaVersion .isAtLeast ( "3.4.2" ) ||
275
+ scalaVersion .isAtLeast ( "3.4.2" ) ||
283
276
// 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" ) );
285
278
286
- List <Artifact > pluginArtifacts = getScalaScoveragePluginArtifacts ( resolvedScalaVersion );
279
+ List <Artifact > pluginArtifacts = getScoveragePluginArtifacts ( scalaVersion );
287
280
if ( scala2 ) // Scala 3 doesn't need scalac-scoverage-runtime
288
281
{
289
- Artifact runtimeArtifact = getScalaScoverageRuntimeArtifact ( resolvedScalaVersion );
290
- addScoverageDependenciesToClasspath ( runtimeArtifact );
282
+ addScalacScoverageRuntimeDependencyToClasspath ( scalaVersion );
291
283
}
292
284
293
285
String arg = ( scala2 ? SCALA2_DATA_DIR_OPTION : SCALA3_COVERAGE_OUT_OPTION ) + dataDirectory .getAbsolutePath ();
@@ -404,7 +396,6 @@ private ScalaVersion resolveScalaVersion()
404
396
if ( result == null || result .isEmpty () )
405
397
{
406
398
// check project direct dependencies (transitive dependencies cannot be checked in this Maven lifecycle phase)
407
- @ SuppressWarnings ( "unchecked" )
408
399
List <Dependency > dependencies = project .getDependencies ();
409
400
for ( Dependency dependency : dependencies )
410
401
{
@@ -445,55 +436,46 @@ private void setProperty( Properties projectProperties, String propertyName, Str
445
436
}
446
437
}
447
438
448
- private ArtifactVersion getScalacPluginVersion () {
449
- if ( scalacPluginVersion == null || scalacPluginVersion .isEmpty () ) {
439
+ private String getScalacPluginVersion () {
440
+ if ( StringUtils .isEmpty (scalacPluginVersion ) ) {
450
441
throw new IllegalStateException ("scalacPluginVersion is unset." );
451
442
} else if ( scalacPluginVersion .startsWith ("1." ) ) {
452
443
throw new IllegalStateException ( String .format ( "Unsupported scalacPluginVersion \" %s\" . Please use scalacPluginVersion 2.0.0+ or use older version of scoverage-maven-plugin" , scalacPluginVersion ) );
453
444
} else {
454
- return new DefaultArtifactVersion ( scalacPluginVersion ) ;
445
+ return scalacPluginVersion ;
455
446
}
456
447
}
457
448
458
- private List <Artifact > getScalaScoveragePluginArtifacts ( ScalaVersion resolvedScalaVersion )
449
+ private List <Artifact > getScoveragePluginArtifacts ( ScalaVersion scalaVersion )
459
450
throws ArtifactNotFoundException , ArtifactResolutionException
460
451
{
461
- String resolvedScalacPluginVersion = getScalacPluginVersion ().toString ();
462
452
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
464
454
{
465
- resolvedArtifacts .add (getResolvedArtifact ( "org.scoverage" , " scalac-scoverage-plugin_" + resolvedScalaVersion .full , resolvedScalacPluginVersion ));
455
+ resolvedArtifacts .add (resolveScoverageArtifact ( " scalac-scoverage-plugin_" + scalaVersion .full ));
466
456
}
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 ));
469
459
return resolvedArtifacts ;
470
460
}
471
461
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
-
480
462
/**
481
463
* We need to tweak our test classpath for Scoverage.
482
- *
483
- * @throws MojoExecutionException
484
464
*/
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 ) );
490
470
project .setDependencyArtifacts ( set );
491
471
}
492
472
493
- private Artifact getResolvedArtifact ( String groupId , String artifactId , String version )
473
+ private Artifact resolveScoverageArtifact ( String artifactId )
494
474
throws ArtifactNotFoundException , ArtifactResolutionException
495
475
{
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
+ );
497
479
resolver .resolve ( artifact , remoteRepos , localRepo );
498
480
return artifact ;
499
481
}
@@ -509,21 +491,13 @@ private void saveSourceRootsToFile() throws IOException
509
491
dataDirectory .getAbsolutePath () ) );
510
492
}
511
493
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 ) {
518
497
writer .write ( sourceRoot );
519
498
writer .newLine ();
520
499
}
521
500
}
522
- finally
523
- {
524
- writer .close ();
525
- }
526
501
}
527
502
}
528
-
529
503
}
0 commit comments