Skip to content

Commit 936f7c9

Browse files
committed
Reduce usage of deprecated APIs
1 parent 676eb94 commit 936f7c9

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

src/main/java/org/scoverage/plugin/SCoveragePreCompileMojo.java

+46-38
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
import java.util.stream.Collectors;
2828
import javax.inject.Inject;
2929

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;
3630
import org.apache.maven.execution.MavenSession;
3731
import org.apache.maven.model.Dependency;
3832
import org.apache.maven.plugin.AbstractMojo;
@@ -43,6 +37,14 @@
4337
import org.apache.maven.project.MavenProject;
4438

4539
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;
4648

4749
/**
4850
* Configures project for compilation with SCoverage instrumentation.
@@ -168,28 +170,16 @@ public class SCoveragePreCompileMojo
168170
private List<MavenProject> reactorProjects;
169171

170172
/**
171-
* Artifact factory used to look up artifacts in the remote repository.
173+
* Repository system used to look up artifacts in the remote repository.
172174
*/
173175
@Inject
174-
private ArtifactFactory factory;
176+
private RepositorySystem repositorySystem;
175177

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;
187180

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;
193183

194184
/**
195185
* Configures project for compilation with SCoverage instrumentation.
@@ -357,7 +347,7 @@ public void execute() throws MojoExecutionException
357347

358348
saveSourceRootsToFile();
359349
}
360-
catch (ArtifactNotFoundException | ArtifactResolutionException | IOException e )
350+
catch ( ArtifactResolutionException | IOException e )
361351
{
362352
throw new MojoExecutionException( "SCoverage preparation failed", e );
363353
}
@@ -447,8 +437,7 @@ private String getScalacPluginVersion() {
447437
}
448438

449439
private List<Artifact> getScoveragePluginArtifacts(ScalaVersion scalaVersion )
450-
throws ArtifactNotFoundException, ArtifactResolutionException
451-
{
440+
throws ArtifactResolutionException {
452441
List<Artifact> resolvedArtifacts = new ArrayList<>();
453442
if ( scalaVersion.isScala2() ) // Scala 3 doesn't need scalac-scoverage-plugin
454443
{
@@ -462,22 +451,41 @@ private List<Artifact> getScoveragePluginArtifacts(ScalaVersion scalaVersion )
462451
/**
463452
* We need to tweak our test classpath for Scoverage.
464453
*/
454+
@SuppressWarnings( "deprecation" ) // didn't find a good way to do this with Aether artifacts
465455
private void addScalacScoverageRuntimeDependencyToClasspath(ScalaVersion resolvedScalaVersion )
466-
throws ArtifactResolutionException, ArtifactNotFoundException {
456+
throws ArtifactResolutionException {
467457

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);
471463
}
472464

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
478477
);
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();
481489
}
482490

483491
private void saveSourceRootsToFile() throws IOException

0 commit comments

Comments
 (0)