27
27
import java .util .stream .Collectors ;
28
28
import javax .inject .Inject ;
29
29
30
- import org .apache .maven .artifact .Artifact ;
31
- import org .apache .maven .artifact .factory .ArtifactFactory ;
32
- import org .apache .maven .artifact .repository .ArtifactRepository ;
33
- import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
34
- import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
35
- import org .apache .maven .artifact .resolver .ArtifactResolver ;
36
30
import org .apache .maven .execution .MavenSession ;
37
31
import org .apache .maven .model .Dependency ;
38
32
import org .apache .maven .plugin .AbstractMojo ;
43
37
import org .apache .maven .project .MavenProject ;
44
38
45
39
import org .codehaus .plexus .util .StringUtils ;
40
+ import org .eclipse .aether .artifact .Artifact ;
41
+ import org .eclipse .aether .RepositorySystem ;
42
+ import org .eclipse .aether .RepositorySystemSession ;
43
+ import org .eclipse .aether .artifact .DefaultArtifact ;
44
+ import org .eclipse .aether .repository .RemoteRepository ;
45
+ import org .eclipse .aether .resolution .ArtifactRequest ;
46
+ import org .eclipse .aether .resolution .ArtifactResolutionException ;
47
+ import org .eclipse .aether .resolution .ArtifactResult ;
46
48
47
49
/**
48
50
* Configures project for compilation with SCoverage instrumentation.
@@ -168,28 +170,16 @@ public class SCoveragePreCompileMojo
168
170
private List <MavenProject > reactorProjects ;
169
171
170
172
/**
171
- * Artifact factory used to look up artifacts in the remote repository.
173
+ * Repository system used to look up artifacts in the remote repository.
172
174
*/
173
175
@ Inject
174
- private ArtifactFactory factory ;
176
+ private RepositorySystem repositorySystem ;
175
177
176
- /**
177
- * Artifact resolver used to resolve artifacts.
178
- */
179
- @ Inject
180
- private ArtifactResolver resolver ;
181
-
182
- /**
183
- * Location of the local repository.
184
- */
185
- @ Parameter ( property = "localRepository" , readonly = true , required = true )
186
- private ArtifactRepository localRepo ;
178
+ @ Parameter ( defaultValue = "${repositorySystemSession}" , readonly = true )
179
+ protected RepositorySystemSession repoSession ;
187
180
188
- /**
189
- * Remote repositories used by the resolver
190
- */
191
- @ Parameter ( property = "project.remoteArtifactRepositories" , readonly = true , required = true )
192
- private List <ArtifactRepository > remoteRepos ;
181
+ @ Parameter ( defaultValue = "${project.remoteProjectRepositories}" , readonly = true )
182
+ protected List <RemoteRepository > remoteRepos ;
193
183
194
184
/**
195
185
* Configures project for compilation with SCoverage instrumentation.
@@ -357,7 +347,7 @@ public void execute() throws MojoExecutionException
357
347
358
348
saveSourceRootsToFile ();
359
349
}
360
- catch (ArtifactNotFoundException | ArtifactResolutionException | IOException e )
350
+ catch ( ArtifactResolutionException | IOException e )
361
351
{
362
352
throw new MojoExecutionException ( "SCoverage preparation failed" , e );
363
353
}
@@ -447,8 +437,7 @@ private String getScalacPluginVersion() {
447
437
}
448
438
449
439
private List <Artifact > getScoveragePluginArtifacts (ScalaVersion scalaVersion )
450
- throws ArtifactNotFoundException , ArtifactResolutionException
451
- {
440
+ throws ArtifactResolutionException {
452
441
List <Artifact > resolvedArtifacts = new ArrayList <>();
453
442
if ( scalaVersion .isScala2 () ) // Scala 3 doesn't need scalac-scoverage-plugin
454
443
{
@@ -462,22 +451,41 @@ private List<Artifact> getScoveragePluginArtifacts(ScalaVersion scalaVersion )
462
451
/**
463
452
* We need to tweak our test classpath for Scoverage.
464
453
*/
454
+ @ SuppressWarnings ( "deprecation" ) // didn't find a good way to do this with Aether artifacts
465
455
private void addScalacScoverageRuntimeDependencyToClasspath (ScalaVersion resolvedScalaVersion )
466
- throws ArtifactResolutionException , ArtifactNotFoundException {
456
+ throws ArtifactResolutionException {
467
457
468
- Set <Artifact > set = new LinkedHashSet <>(project .getDependencyArtifacts ());
469
- set .add (resolveScoverageArtifact ( "scalac-scoverage-runtime_" + resolvedScalaVersion .compatible ) );
470
- project .setDependencyArtifacts ( set );
458
+ Set <org .apache .maven .artifact .Artifact > set = new LinkedHashSet <>(project .getDependencyArtifacts ());
459
+ set .add (toMavenClasspathArtifact (
460
+ resolveScoverageArtifact ("scalac-scoverage-runtime_" + resolvedScalaVersion .compatible )
461
+ ));
462
+ project .setDependencyArtifacts ( set );
471
463
}
472
464
473
- private Artifact resolveScoverageArtifact ( String artifactId )
474
- throws ArtifactNotFoundException , ArtifactResolutionException
475
- {
476
- Artifact artifact = factory .createArtifact (
477
- "org.scoverage" , artifactId , getScalacPluginVersion (), Artifact .SCOPE_COMPILE , "jar"
465
+ private org .apache .maven .artifact .Artifact toMavenClasspathArtifact ( Artifact artifact ) {
466
+ org .apache .maven .artifact .handler .DefaultArtifactHandler artifactHandler =
467
+ new org .apache .maven .artifact .handler .DefaultArtifactHandler ( artifact .getExtension () );
468
+ artifactHandler .setAddedToClasspath (true );
469
+ return new org .apache .maven .artifact .DefaultArtifact (
470
+ artifact .getGroupId (),
471
+ artifact .getArtifactId (),
472
+ artifact .getVersion (),
473
+ org .apache .maven .artifact .Artifact .SCOPE_COMPILE ,
474
+ artifact .getExtension (),
475
+ artifact .getClassifier (),
476
+ artifactHandler
478
477
);
479
- resolver .resolve ( artifact , remoteRepos , localRepo );
480
- return artifact ;
478
+ }
479
+
480
+ private Artifact resolveScoverageArtifact ( String artifactId )
481
+ throws ArtifactResolutionException {
482
+
483
+ ArtifactRequest request = new ArtifactRequest ();
484
+ request .setArtifact ( new DefaultArtifact ("org.scoverage" , artifactId , "jar" , getScalacPluginVersion ()) );
485
+ request .setRepositories (remoteRepos );
486
+
487
+ ArtifactResult result = repositorySystem .resolveArtifact (repoSession , request );
488
+ return result .getArtifact ();
481
489
}
482
490
483
491
private void saveSourceRootsToFile () throws IOException
0 commit comments