Skip to content

Commit

Permalink
[MNG-8041] Move PathScope into the dependency collection request (#1807)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Oct 16, 2024
1 parent ee29050 commit 0d04bb9
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -612,25 +612,27 @@ Collection<DownloadedArtifact> resolveArtifacts(
* Shortcut for {@code getService(DependencyResolver.class).collect(...)}
*
* @param artifact artifact for which to get the dependencies, including transitive ones
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
* @return root node of the dependency graph for the given artifact
*
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Artifact)
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Artifact, PathScope)
* @throws org.apache.maven.api.services.DependencyResolverException if the dependency collection failed
*/
@Nonnull
Node collectDependencies(@Nonnull Artifact artifact);
Node collectDependencies(@Nonnull Artifact artifact, @Nonnull PathScope scope);

/**
* Shortcut for {@code getService(DependencyResolver.class).collect(...)}
*
* @param project project for which to get the dependencies, including transitive ones
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
* @return root node of the dependency graph for the given project
*
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Project)
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Project, PathScope)
* @throws org.apache.maven.api.services.DependencyResolverException if the dependency collection failed
*/
@Nonnull
Node collectDependencies(@Nonnull Project project);
Node collectDependencies(@Nonnull Project project, @Nonnull PathScope scope);

/**
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
Expand All @@ -640,13 +642,14 @@ Collection<DownloadedArtifact> resolveArtifacts(
* Shortcut for {@code getService(DependencyResolver.class).resolve(...)}
*
* @param dependency dependency for which to get transitive dependencies
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
* @return root node of the dependency graph for the given artifact
*
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, DependencyCoordinates)
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, DependencyCoordinates, PathScope)
* @throws org.apache.maven.api.services.DependencyResolverException if the dependency collection failed
*/
@Nonnull
Node collectDependencies(@Nonnull DependencyCoordinates dependency);
Node collectDependencies(@Nonnull DependencyCoordinates dependency, @Nonnull PathScope scope);

/**
* Shortcut for {@code getService(DependencyResolver.class).flatten(...)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.api.Session;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

/**
* Collects, flattens and resolves dependencies.
Expand All @@ -37,56 +38,63 @@
public interface DependencyResolver extends Service {

/**
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
* only concerned about determining the coordinates of the transitive dependencies and does not actually resolve the
* artifact files.
* Collects the transitive dependencies of some artifacts and builds a dependency graph for the given path scope.
* Note that this operation is only concerned about determining the coordinates of the transitive dependencies and
* does not actually resolve the artifact files.
*
* @param session the {@link Session}, must not be {@code null}
* @param root the Maven Dependency, must not be {@code null}
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
* @return the collection result, never {@code null}
* @throws DependencyResolverException if the dependency tree could not be built
* @throws IllegalArgumentException if an argument is null or invalid
* @see #collect(DependencyResolverRequest)
*/
@Nonnull
default DependencyResolverResult collect(@Nonnull Session session, @Nonnull DependencyCoordinates root) {
return collect(DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, root));
default DependencyResolverResult collect(
@Nonnull Session session, @Nonnull DependencyCoordinates root, @Nonnull PathScope scope) {
return collect(
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, root, scope));
}

/**
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
* only concerned about determining the coordinates of the transitive dependencies and does not actually resolve the
* artifact files.
* Collects the transitive dependencies of some artifacts and builds a dependency graph for the given path scope.
* Note that this operation is only concerned about determining the coordinates of the transitive dependencies and
* does not actually resolve the artifact files.
*
* @param session the {@link Session}, must not be {@code null}
* @param project the {@link Project}, must not be {@code null}
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
* @return the collection result, never {@code null}
* @throws DependencyResolverException if the dependency tree could not be built
* @throws IllegalArgumentException if an argument is null or invalid
* @see #collect(DependencyResolverRequest)
*/
@Nonnull
default DependencyResolverResult collect(@Nonnull Session session, @Nonnull Project project) {
return collect(
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, project));
default DependencyResolverResult collect(
@Nonnull Session session, @Nonnull Project project, @Nonnull PathScope scope) {
return collect(DependencyResolverRequest.build(
session, DependencyResolverRequest.RequestType.COLLECT, project, scope));
}

/**
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
* only concerned about determining the coordinates of the transitive dependencies and does not actually resolve the
* artifact files.
* Collects the transitive dependencies of some artifacts and builds a dependency graph for the given path scope.
* Note that this operation is only concerned about determining the coordinates of the transitive dependencies and
* does not actually resolve the artifact files.
*
* @param session the {@link Session}, must not be {@code null}
* @param artifact the {@link Artifact}, must not be {@code null}
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
* @return the collection result, never {@code null}
* @throws DependencyResolverException if the dependency tree could not be built
* @throws IllegalArgumentException if an argument is null or invalid
* @see #collect(DependencyResolverRequest)
*/
@Nonnull
default DependencyResolverResult collect(@Nonnull Session session, @Nonnull Artifact artifact) {
return collect(
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, artifact));
default DependencyResolverResult collect(
@Nonnull Session session, @Nonnull Artifact artifact, @Nonnull PathScope scope) {
return collect(DependencyResolverRequest.build(
session, DependencyResolverRequest.RequestType.COLLECT, artifact, scope));
}

/**
Expand All @@ -99,9 +107,9 @@ default DependencyResolverResult collect(@Nonnull Session session, @Nonnull Arti
* @throws DependencyResolverException if the dependency tree could not be built
* @throws IllegalArgumentException if an argument is null or invalid
*
* @see DependencyResolver#collect(Session, Project)
* @see DependencyResolver#collect(Session, DependencyCoordinates)
* @see DependencyResolver#collect(Session, Artifact)
* @see DependencyResolver#collect(Session, Project, PathScope)
* @see DependencyResolver#collect(Session, DependencyCoordinates, PathScope)
* @see DependencyResolver#collect(Session, Artifact, PathScope)
*/
@Nonnull
default DependencyResolverResult collect(@Nonnull DependencyResolverRequest request) {
Expand All @@ -113,20 +121,17 @@ default DependencyResolverResult collect(@Nonnull DependencyResolverRequest requ

/**
* Flattens a list of nodes.
* Note that the {@code PathScope} argument should usually be null as the dependency tree has been
* filtered during collection for the appropriate scope.
*
* @param session
* @param node
* @param scope
* @return
* @param session the {@link Session}, must not be {@code null}
* @param node the {@link Node} to flatten, must not be {@code null}
* @param scope an optional {@link PathScope} to filter out dependencies
* @return the flattened list of node
* @throws DependencyResolverException
*/
List<Node> flatten(Session session, Node node, PathScope scope) throws DependencyResolverException;

@Nonnull
default DependencyResolverResult flatten(@Nonnull Session session, @Nonnull Project project) {
return flatten(
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.FLATTEN, project));
}
List<Node> flatten(@Nonnull Session session, @Nonnull Node node, @Nullable PathScope scope)
throws DependencyResolverException;

@Nonnull
default DependencyResolverResult flatten(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ enum RequestType {

boolean getVerbose();

@Nullable
@Nonnull
PathScope getPathScope();

/**
Expand All @@ -105,11 +105,17 @@ static DependencyResolverRequestBuilder builder() {

@Nonnull
static DependencyResolverRequest build(Session session, RequestType requestType, Artifact rootArtifact) {
return build(session, requestType, rootArtifact, PathScope.MAIN_RUNTIME);
}

@Nonnull
static DependencyResolverRequest build(
Session session, RequestType requestType, Artifact rootArtifact, PathScope scope) {
return new DependencyResolverRequestBuilder()
.session(session)
.requestType(requestType)
.rootArtifact(rootArtifact)
.pathScope(PathScope.MAIN_RUNTIME)
.pathScope(scope)
.build();
}

Expand Down Expand Up @@ -395,7 +401,7 @@ static class DefaultDependencyResolverRequest extends BaseRequest implements Dep
this.managedDependencies =
unmodifiable(nonNull(managedDependencies, "managedDependencies cannot be null"));
this.verbose = verbose;
this.pathScope = pathScope;
this.pathScope = nonNull(pathScope, "pathScope cannot be null");
this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true;
this.repositories = repositories;
if (verbose && requestType != RequestType.COLLECT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,37 +720,42 @@ public DependencyCoordinates createDependencyCoordinates(@Nonnull Dependency dep
* Shortcut for <code>getService(DependencyResolver.class).collect(...)</code>
*
* @throws DependencyResolverException if the dependency collection failed
* @see DependencyResolver#collect(Session, Artifact)
* @see DependencyResolver#collect(Session, Artifact, PathScope)
*/
@Nonnull
@Override
public Node collectDependencies(@Nonnull Artifact artifact) {
return getService(DependencyResolver.class).collect(this, artifact).getRoot();
public Node collectDependencies(@Nonnull Artifact artifact, @Nonnull PathScope scope) {
return getService(DependencyResolver.class)
.collect(this, artifact, scope)
.getRoot();
}

/**
* Shortcut for <code>getService(DependencyResolver.class).collect(...)</code>
*
* @throws DependencyResolverException if the dependency collection failed
* @see DependencyResolver#collect(Session, Project)
* @see DependencyResolver#collect(Session, Project, PathScope)
*/
@Nonnull
@Override
public Node collectDependencies(@Nonnull Project project) {
return getService(DependencyResolver.class).collect(this, project).getRoot();
public Node collectDependencies(@Nonnull Project project, @Nonnull PathScope scope) {
return getService(DependencyResolver.class)
.collect(this, project, scope)
.getRoot();
}

/**
* Shortcut for <code>getService(DependencyResolver.class).collect(...)</code>
*
* @throws DependencyResolverException if the dependency collection failed
* @see DependencyResolver#collect(Session, DependencyCoordinates)
* @see DependencyResolver#collect(Session, DependencyCoordinates, PathScope)
*/
@Nonnull
@Override
public Node collectDependencies(@Nonnull DependencyCoordinates dependency) {
Node root =
getService(DependencyResolver.class).collect(this, dependency).getRoot();
public Node collectDependencies(@Nonnull DependencyCoordinates dependency, @Nonnull PathScope scope) {
Node root = getService(DependencyResolver.class)
.collect(this, dependency, scope)
.getRoot();
return root.getChildren().iterator().next();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.ArtifactResolver;
Expand All @@ -56,6 +57,7 @@
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.scope.ResolutionScope;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;

Expand Down Expand Up @@ -96,12 +98,20 @@ public DependencyResolverResult collect(@Nonnull DependencyResolverRequest reque
remoteRepositories =
request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories();
}
ResolutionScope resolutionScope = null;
if (request.getPathScope() != null) {
resolutionScope = session.getSession()
.getScopeManager()
.getResolutionScope(request.getPathScope().id())
.orElseThrow();
}
CollectRequest collectRequest = new CollectRequest()
.setRootArtifact(rootArtifact != null ? session.toArtifact(rootArtifact) : null)
.setRoot(root != null ? session.toDependency(root, false) : null)
.setDependencies(session.toDependencies(dependencies, false))
.setManagedDependencies(session.toDependencies(managedDependencies, true))
.setRepositories(session.toRepositories(remoteRepositories));
collectRequest.setResolutionScope(resolutionScope);

RepositorySystemSession systemSession = session.getSession();
if (request.getVerbose()) {
Expand All @@ -120,8 +130,10 @@ public DependencyResolverResult collect(@Nonnull DependencyResolverRequest reque
}
}

@Nonnull
@Override
public List<Node> flatten(Session s, Node node, PathScope scope) throws DependencyResolverException {
public List<Node> flatten(@Nonnull Session s, @Nonnull Node node, @Nullable PathScope scope)
throws DependencyResolverException {
InternalSession session = InternalSession.from(s);
DependencyNode root = cast(AbstractNode.class, node, "node").getDependencyNode();
List<DependencyNode> dependencies = session.getRepositorySystem()
Expand All @@ -131,6 +143,9 @@ public List<Node> flatten(Session s, Node node, PathScope scope) throws Dependen
}

private static DependencyFilter getScopeDependencyFilter(PathScope scope) {
if (scope == null) {
return null;
}
Set<String> scopes =
scope.dependencyScopes().stream().map(DependencyScope::id).collect(Collectors.toSet());
return (n, p) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
import org.apache.maven.internal.impl.AbstractSession;
import org.apache.maven.internal.impl.InternalSession;
import org.apache.maven.internal.impl.di.SessionScope;
import org.apache.maven.internal.impl.resolver.scopes.Maven4ScopeManagerConfiguration;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.impl.RemoteRepositoryManager;
import org.eclipse.aether.internal.impl.scope.ScopeManagerImpl;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.LocalRepositoryManager;

Expand Down Expand Up @@ -328,6 +330,7 @@ static Session newSession(RepositorySystem system, Lookup lookup) {
: properties.containsKey("env.MAVEN_HOME") ? Paths.get(properties.get("env.MAVEN_HOME")) : null;

DefaultRepositorySystemSession rsession = new DefaultRepositorySystemSession(h -> false);
rsession.setScopeManager(new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE));
rsession.setSystemProperties(properties);
rsession.setConfigProperties(properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.api.ArtifactCoordinates;
import org.apache.maven.api.DownloadedArtifact;
import org.apache.maven.api.Node;
import org.apache.maven.api.PathScope;
import org.apache.maven.api.Session;
import org.apache.maven.api.services.ModelBuilder;
import org.apache.maven.api.services.ModelBuilderRequest;
Expand Down Expand Up @@ -58,8 +59,8 @@ void testStandalone() {
assertNotNull(res.getPath());
assertTrue(Files.exists(res.getPath()));

Node node = session.collectDependencies(session.createDependencyCoordinates(coords));
Node node = session.collectDependencies(session.createDependencyCoordinates(coords), PathScope.MAIN_RUNTIME);
assertNotNull(node);
assertEquals(8, node.getChildren().size());
assertEquals(6, node.getChildren().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,9 @@ private <T> T loadV4Mojo(
}
} else {
// collection
DependencyResolverResult res =
sessionV4.getService(DependencyResolver.class).collect(sessionV4, project);
DependencyResolverResult res = sessionV4
.getService(DependencyResolver.class)
.collect(sessionV4, project, PathScope.MAIN_RUNTIME);
if (field.getType() == DependencyResolverResult.class) {
result = res;
} else if (field.getType() == Node.class) {
Expand Down
Loading

0 comments on commit 0d04bb9

Please sign in to comment.