Skip to content

Commit

Permalink
[MDEP-940] Use Resolver API instead of m-a-t for resolving artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jun 16, 2024
1 parent 8e1f1b5 commit 82a9d60
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -32,12 +33,12 @@
import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.plugins.dependency.utils.translators.ArtifactTranslator;
import org.apache.maven.plugins.dependency.utils.translators.ClassifierTypeTranslator;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
Expand All @@ -47,11 +48,9 @@
import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.eclipse.aether.resolution.ArtifactResolutionException;

/**
* Class that encapsulates the plugin parameters, and contains methods that handle dependency filtering
Expand All @@ -60,8 +59,9 @@
* @see org.apache.maven.plugins.dependency.AbstractDependencyMojo
*/
public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMojo {

@Component
private ArtifactResolver artifactResolver;
private ResolverUtil resolverUtil;

@Component
private DependencyResolver dependencyResolver;
Expand Down Expand Up @@ -364,14 +364,11 @@ private void addParentArtifacts(MavenProject project, Set<Artifact> artifacts) t
break;
}
try {
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();

Artifact resolvedArtifact = artifactResolver
.resolveArtifact(buildingRequest, project.getArtifact())
.getArtifact();
org.eclipse.aether.artifact.Artifact resolvedArtifact = resolverUtil.resolveArtifact(
RepositoryUtils.toArtifact(project.getArtifact()), project.getRemoteProjectRepositories());

artifacts.add(resolvedArtifact);
} catch (ArtifactResolverException e) {
artifacts.add(RepositoryUtils.toArtifact(resolvedArtifact));
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
Expand All @@ -385,7 +382,7 @@ private void addParentArtifacts(MavenProject project, Set<Artifact> artifacts) t
* @return DependencyStatusSets - Bean of TreeSets that contains information on the projects dependencies
* @throws MojoExecutionException in case of an error.
*/
protected DependencyStatusSets getClassifierTranslatedDependencies(Set<Artifact> artifacts, boolean stopOnFailure)
private DependencyStatusSets getClassifierTranslatedDependencies(Set<Artifact> artifacts, boolean stopOnFailure)
throws MojoExecutionException {
Set<Artifact> unResolvedArtifacts = new LinkedHashSet<>();
Set<Artifact> resolvedArtifacts = artifacts;
Expand All @@ -397,7 +394,7 @@ protected DependencyStatusSets getClassifierTranslatedDependencies(Set<Artifact>
if (classifier != null && !classifier.isEmpty()) {
ArtifactTranslator translator =
new ClassifierTypeTranslator(artifactHandlerManager, this.classifier, this.type);
Collection<ArtifactCoordinate> coordinates = translator.translate(artifacts, getLog());
Collection<org.eclipse.aether.artifact.Artifact> coordinates = translator.translate(artifacts, getLog());

status = filterMarkedDependencies(artifacts);

Expand Down Expand Up @@ -447,29 +444,26 @@ protected DependencyStatusSets filterMarkedDependencies(Set<Artifact> artifacts)
}

/**
* @param coordinates The set of artifact coordinates{@link ArtifactCoordinate}.
* @param artifacts The set of artifacts
* @param stopOnFailure <code>true</code> if we should fail with exception if an artifact couldn't be resolved
* <code>false</code> otherwise.
* @return the resolved artifacts. {@link Artifact}.
* @throws MojoExecutionException in case of error.
*/
protected Set<Artifact> resolve(Set<ArtifactCoordinate> coordinates, boolean stopOnFailure)
private Set<Artifact> resolve(Set<org.eclipse.aether.artifact.Artifact> artifacts, boolean stopOnFailure)
throws MojoExecutionException {
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();

Set<Artifact> resolvedArtifacts = new LinkedHashSet<>();
for (ArtifactCoordinate coordinate : coordinates) {
for (org.eclipse.aether.artifact.Artifact artifact : artifacts) {
try {
Artifact artifact = artifactResolver
.resolveArtifact(buildingRequest, coordinate)
.getArtifact();
resolvedArtifacts.add(artifact);
} catch (ArtifactResolverException ex) {
org.eclipse.aether.artifact.Artifact resolveArtifact =
resolverUtil.resolveArtifact(artifact, getProject().getRemoteProjectRepositories());
resolvedArtifacts.add(RepositoryUtils.toArtifact(resolveArtifact));
} catch (ArtifactResolutionException ex) {
// an error occurred during resolution, log it an continue
getLog().debug("error resolving: " + coordinate);
getLog().debug(ex);
getLog().debug("error resolving: " + artifact, ex);
if (stopOnFailure) {
throw new MojoExecutionException("error resolving: " + coordinate, ex);
throw new MojoExecutionException("error resolving: " + artifact, ex);
}
}
}
Expand Down Expand Up @@ -507,10 +501,10 @@ public void setPrependGroupId(boolean prependGroupId) {
}

/**
* @return {@link #artifactResolver}
* @return {@link #resolverUtil}
*/
protected final ArtifactResolver getArtifactResolver() {
return artifactResolver;
protected final ResolverUtil getResolverUtil() {
return resolverUtil;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
import java.util.Set;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -38,10 +39,10 @@
import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.util.artifact.SubArtifact;

/**
* Goal that copies the project dependencies from the repository to a defined location.
Expand Down Expand Up @@ -289,21 +290,16 @@ public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersio
* @return {@link Artifact}
*/
protected Artifact getResolvedPomArtifact(Artifact artifact) {
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(artifact.getGroupId());
coordinate.setArtifactId(artifact.getArtifactId());
coordinate.setVersion(artifact.getVersion());
coordinate.setExtension("pom");

Artifact pomArtifact = null;
// Resolve the pom artifact using repos
try {
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();

pomArtifact = getArtifactResolver()
.resolveArtifact(buildingRequest, coordinate)
.getArtifact();
} catch (ArtifactResolverException e) {
org.eclipse.aether.artifact.Artifact aArtifact = RepositoryUtils.toArtifact(artifact);
org.eclipse.aether.artifact.Artifact aPomArtifact = new SubArtifact(aArtifact, null, "pom");
org.eclipse.aether.artifact.Artifact resolvedPom =
getResolverUtil().resolveArtifact(aPomArtifact, getProject().getRemoteProjectRepositories());
pomArtifact = RepositoryUtils.toArtifact(resolvedPom);
} catch (ArtifactResolutionException e) {
getLog().info(e.getMessage());
}
return pomArtifact;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.apache.maven.plugins.dependency.resolvers;

import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -43,6 +45,7 @@
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
import org.eclipse.aether.resolution.ArtifactResolutionException;

/**
* Goal that resolves all project plugins and reports and their dependencies.
Expand Down Expand Up @@ -139,7 +142,11 @@ protected void doExecute() throws MojoExecutionException {
DependencyUtil.write(output, outputFile, appendOutput, encoding);
}
}
} catch (IOException | ArtifactFilterException | ArtifactResolverException | DependencyResolverException e) {
} catch (IOException
| ArtifactFilterException
| ArtifactResolverException
| DependencyResolverException
| ArtifactResolutionException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
Expand Down Expand Up @@ -208,39 +215,27 @@ private FilterArtifacts getArtifactsFilter() {
* @throws ArtifactFilterException in case of an error
* @throws ArtifactResolverException in case of an error
*/
private Set<Artifact> resolvePluginArtifacts() throws ArtifactFilterException, ArtifactResolverException {
private Set<Artifact> resolvePluginArtifacts()
throws ArtifactFilterException, ArtifactResolverException, ArtifactResolutionException {
final Set<Artifact> plugins = getProject().getPluginArtifacts();
final Set<Artifact> reports = getProject().getReportArtifacts();

Set<Artifact> artifacts = new LinkedHashSet<>();
Set<Artifact> artifacts = new HashSet<>();
artifacts.addAll(reports);
artifacts.addAll(plugins);

final FilterArtifacts filter = getArtifactsFilter();
artifacts = filter.filter(artifacts);

// final ArtifactFilter filter = getPluginFilter();
Set<Artifact> result = new HashSet<>();
for (final Artifact artifact : new LinkedHashSet<>(artifacts)) {
// if ( !filter.include( artifact ) )
// {
// final String logStr =
// String.format( " Plugin SKIPPED: %s", DependencyUtil.getFormattedFileName( artifact, false ) );
//
// if ( !silent )
// {
// this.getLog().info( logStr );
// }
//
// artifacts.remove( artifact );
// continue;
// }

ProjectBuildingRequest buildingRequest = newResolvePluginProjectBuildingRequest();

// resolve the new artifact
getArtifactResolver().resolveArtifact(buildingRequest, artifact).getArtifact();

org.eclipse.aether.artifact.Artifact resolveArtifact = getResolverUtil()
.resolveArtifact(
RepositoryUtils.toArtifact(artifact), getProject().getRemotePluginRepositories());
result.add(RepositoryUtils.toArtifact(resolveArtifact));
}
return artifacts;
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;

/**
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
Expand All @@ -31,7 +30,7 @@ public interface ArtifactTranslator {
/**
* @param artifacts set of {@link Artifact}s.
* @param log {@link Log}
* @return {@link ArtifactCoordinate}
* @return set of {@link org.eclipse.aether.artifact.Artifact}
*/
Set<ArtifactCoordinate> translate(Set<Artifact> artifacts, Log log);
Set<org.eclipse.aether.artifact.Artifact> translate(Set<Artifact> artifacts, Log log);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.eclipse.aether.util.artifact.SubArtifact;

/**
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
Expand All @@ -39,13 +39,13 @@ public class ClassifierTypeTranslator implements ArtifactTranslator {
private String type;

/**
* @param artifactHanderManager {@link ArtifactHandlerManager}.
* @param artifactHandlerManager {@link ArtifactHandlerManager}.
* @param theClassifier The classifier to use.
* @param theType The type.
*/
public ClassifierTypeTranslator(
ArtifactHandlerManager artifactHanderManager, String theClassifier, String theType) {
this.artifactHandlerManager = artifactHanderManager;
ArtifactHandlerManager artifactHandlerManager, String theClassifier, String theType) {
this.artifactHandlerManager = artifactHandlerManager;
this.classifier = theClassifier;
this.type = theType;
}
Expand All @@ -56,8 +56,8 @@ public ClassifierTypeTranslator(
* org.apache.maven.plugin.logging.Log)
*/
@Override
public Set<ArtifactCoordinate> translate(Set<Artifact> artifacts, Log log) {
Set<ArtifactCoordinate> results;
public Set<org.eclipse.aether.artifact.Artifact> translate(Set<Artifact> artifacts, Log log) {
Set<org.eclipse.aether.artifact.Artifact> results;

log.debug("Translating Artifacts using Classifier: " + this.classifier + " and Type: " + this.type);
results = new LinkedHashSet<>();
Expand Down Expand Up @@ -88,30 +88,7 @@ public Set<ArtifactCoordinate> translate(Set<Artifact> artifacts, Log log) {
useClassifier = artifact.getClassifier();
}

DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(artifact.getGroupId());
coordinate.setArtifactId(artifact.getArtifactId());
coordinate.setVersion(artifact.getVersion());
coordinate.setClassifier(useClassifier);
coordinate.setExtension(extension);

// // Create a new artifact
// Artifact newArtifact = factory.createArtifactWithClassifier( artifact.getGroupId(), artifact
// .getArtifactId(), artifact.getVersion(), useType, useClassifier );
//
// // note the new artifacts will always have the scope set to null. We
// // should
// // reset it here so that it will pass other filters if needed
// newArtifact.setScope( artifact.getScope() );
//
// if ( Artifact.SCOPE_SYSTEM.equals( newArtifact.getScope() ) )
// {
// File baseDir = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
// String path = repositoryManager.getPathForLocalArtifact( buildingRequest, newArtifact );
// newArtifact.setFile( new File( baseDir, path ) );
// }

results.add(coordinate);
results.add(new SubArtifact(RepositoryUtils.toArtifact(artifact), useClassifier, extension));
}

return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;

public class TestCopyDependenciesMojo extends AbstractDependencyMojoTestCase {

Expand All @@ -52,6 +54,10 @@ protected void setUp() throws Exception {
MavenSession session = newMavenSession(project);
getContainer().addComponent(session, MavenSession.class.getName());

RepositorySystem repositorySystem = lookup(RepositorySystem.class);
ResolverUtil resolverUtil = new ResolverUtil(repositorySystem, () -> session);
getContainer().addComponent(resolverUtil, ResolverUtil.class.getName());

File testPom = new File(getBasedir(), "target/test-classes/unit/copy-dependencies-test/plugin-config.xml");
mojo = (CopyDependenciesMojo) lookupMojo("copy-dependencies", testPom);
mojo.outputDirectory = new File(this.testDir, "outputDirectory");
Expand Down
Loading

0 comments on commit 82a9d60

Please sign in to comment.