Skip to content

Commit 0d04bb9

Browse files
authored
[MNG-8041] Move PathScope into the dependency collection request (#1807)
JIRA issue: [MNG-8041](https://issues.apache.org/jira/browse/MNG-8041) IT PR: apache/maven-integration-testing#390
1 parent ee29050 commit 0d04bb9

File tree

9 files changed

+96
-57
lines changed

9 files changed

+96
-57
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/Session.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,25 +612,27 @@ Collection<DownloadedArtifact> resolveArtifacts(
612612
* Shortcut for {@code getService(DependencyResolver.class).collect(...)}
613613
*
614614
* @param artifact artifact for which to get the dependencies, including transitive ones
615+
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
615616
* @return root node of the dependency graph for the given artifact
616617
*
617-
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Artifact)
618+
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Artifact, PathScope)
618619
* @throws org.apache.maven.api.services.DependencyResolverException if the dependency collection failed
619620
*/
620621
@Nonnull
621-
Node collectDependencies(@Nonnull Artifact artifact);
622+
Node collectDependencies(@Nonnull Artifact artifact, @Nonnull PathScope scope);
622623

623624
/**
624625
* Shortcut for {@code getService(DependencyResolver.class).collect(...)}
625626
*
626627
* @param project project for which to get the dependencies, including transitive ones
628+
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
627629
* @return root node of the dependency graph for the given project
628630
*
629-
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Project)
631+
* @see org.apache.maven.api.services.DependencyResolver#collect(Session, Project, PathScope)
630632
* @throws org.apache.maven.api.services.DependencyResolverException if the dependency collection failed
631633
*/
632634
@Nonnull
633-
Node collectDependencies(@Nonnull Project project);
635+
Node collectDependencies(@Nonnull Project project, @Nonnull PathScope scope);
634636

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

651654
/**
652655
* Shortcut for {@code getService(DependencyResolver.class).flatten(...)}.

api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyResolver.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.maven.api.Session;
3030
import org.apache.maven.api.annotations.Experimental;
3131
import org.apache.maven.api.annotations.Nonnull;
32+
import org.apache.maven.api.annotations.Nullable;
3233

3334
/**
3435
* Collects, flattens and resolves dependencies.
@@ -37,56 +38,63 @@
3738
public interface DependencyResolver extends Service {
3839

3940
/**
40-
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
41-
* only concerned about determining the coordinates of the transitive dependencies and does not actually resolve the
42-
* artifact files.
41+
* Collects the transitive dependencies of some artifacts and builds a dependency graph for the given path scope.
42+
* Note that this operation is only concerned about determining the coordinates of the transitive dependencies and
43+
* does not actually resolve the artifact files.
4344
*
4445
* @param session the {@link Session}, must not be {@code null}
4546
* @param root the Maven Dependency, must not be {@code null}
47+
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
4648
* @return the collection result, never {@code null}
4749
* @throws DependencyResolverException if the dependency tree could not be built
4850
* @throws IllegalArgumentException if an argument is null or invalid
4951
* @see #collect(DependencyResolverRequest)
5052
*/
5153
@Nonnull
52-
default DependencyResolverResult collect(@Nonnull Session session, @Nonnull DependencyCoordinates root) {
53-
return collect(DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, root));
54+
default DependencyResolverResult collect(
55+
@Nonnull Session session, @Nonnull DependencyCoordinates root, @Nonnull PathScope scope) {
56+
return collect(
57+
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, root, scope));
5458
}
5559

5660
/**
57-
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
58-
* only concerned about determining the coordinates of the transitive dependencies and does not actually resolve the
59-
* artifact files.
61+
* Collects the transitive dependencies of some artifacts and builds a dependency graph for the given path scope.
62+
* Note that this operation is only concerned about determining the coordinates of the transitive dependencies and
63+
* does not actually resolve the artifact files.
6064
*
6165
* @param session the {@link Session}, must not be {@code null}
6266
* @param project the {@link Project}, must not be {@code null}
67+
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
6368
* @return the collection result, never {@code null}
6469
* @throws DependencyResolverException if the dependency tree could not be built
6570
* @throws IllegalArgumentException if an argument is null or invalid
6671
* @see #collect(DependencyResolverRequest)
6772
*/
6873
@Nonnull
69-
default DependencyResolverResult collect(@Nonnull Session session, @Nonnull Project project) {
70-
return collect(
71-
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, project));
74+
default DependencyResolverResult collect(
75+
@Nonnull Session session, @Nonnull Project project, @Nonnull PathScope scope) {
76+
return collect(DependencyResolverRequest.build(
77+
session, DependencyResolverRequest.RequestType.COLLECT, project, scope));
7278
}
7379

7480
/**
75-
* Collects the transitive dependencies of some artifacts and builds a dependency graph. Note that this operation is
76-
* only concerned about determining the coordinates of the transitive dependencies and does not actually resolve the
77-
* artifact files.
81+
* Collects the transitive dependencies of some artifacts and builds a dependency graph for the given path scope.
82+
* Note that this operation is only concerned about determining the coordinates of the transitive dependencies and
83+
* does not actually resolve the artifact files.
7884
*
7985
* @param session the {@link Session}, must not be {@code null}
8086
* @param artifact the {@link Artifact}, must not be {@code null}
87+
* @param scope the {link PathScope} to collect dependencies, must not be {@code null}
8188
* @return the collection result, never {@code null}
8289
* @throws DependencyResolverException if the dependency tree could not be built
8390
* @throws IllegalArgumentException if an argument is null or invalid
8491
* @see #collect(DependencyResolverRequest)
8592
*/
8693
@Nonnull
87-
default DependencyResolverResult collect(@Nonnull Session session, @Nonnull Artifact artifact) {
88-
return collect(
89-
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.COLLECT, artifact));
94+
default DependencyResolverResult collect(
95+
@Nonnull Session session, @Nonnull Artifact artifact, @Nonnull PathScope scope) {
96+
return collect(DependencyResolverRequest.build(
97+
session, DependencyResolverRequest.RequestType.COLLECT, artifact, scope));
9098
}
9199

92100
/**
@@ -99,9 +107,9 @@ default DependencyResolverResult collect(@Nonnull Session session, @Nonnull Arti
99107
* @throws DependencyResolverException if the dependency tree could not be built
100108
* @throws IllegalArgumentException if an argument is null or invalid
101109
*
102-
* @see DependencyResolver#collect(Session, Project)
103-
* @see DependencyResolver#collect(Session, DependencyCoordinates)
104-
* @see DependencyResolver#collect(Session, Artifact)
110+
* @see DependencyResolver#collect(Session, Project, PathScope)
111+
* @see DependencyResolver#collect(Session, DependencyCoordinates, PathScope)
112+
* @see DependencyResolver#collect(Session, Artifact, PathScope)
105113
*/
106114
@Nonnull
107115
default DependencyResolverResult collect(@Nonnull DependencyResolverRequest request) {
@@ -113,20 +121,17 @@ default DependencyResolverResult collect(@Nonnull DependencyResolverRequest requ
113121

114122
/**
115123
* Flattens a list of nodes.
124+
* Note that the {@code PathScope} argument should usually be null as the dependency tree has been
125+
* filtered during collection for the appropriate scope.
116126
*
117-
* @param session
118-
* @param node
119-
* @param scope
120-
* @return
127+
* @param session the {@link Session}, must not be {@code null}
128+
* @param node the {@link Node} to flatten, must not be {@code null}
129+
* @param scope an optional {@link PathScope} to filter out dependencies
130+
* @return the flattened list of node
121131
* @throws DependencyResolverException
122132
*/
123-
List<Node> flatten(Session session, Node node, PathScope scope) throws DependencyResolverException;
124-
125-
@Nonnull
126-
default DependencyResolverResult flatten(@Nonnull Session session, @Nonnull Project project) {
127-
return flatten(
128-
DependencyResolverRequest.build(session, DependencyResolverRequest.RequestType.FLATTEN, project));
129-
}
133+
List<Node> flatten(@Nonnull Session session, @Nonnull Node node, @Nullable PathScope scope)
134+
throws DependencyResolverException;
130135

131136
@Nonnull
132137
default DependencyResolverResult flatten(

api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyResolverRequest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ enum RequestType {
8181

8282
boolean getVerbose();
8383

84-
@Nullable
84+
@Nonnull
8585
PathScope getPathScope();
8686

8787
/**
@@ -105,11 +105,17 @@ static DependencyResolverRequestBuilder builder() {
105105

106106
@Nonnull
107107
static DependencyResolverRequest build(Session session, RequestType requestType, Artifact rootArtifact) {
108+
return build(session, requestType, rootArtifact, PathScope.MAIN_RUNTIME);
109+
}
110+
111+
@Nonnull
112+
static DependencyResolverRequest build(
113+
Session session, RequestType requestType, Artifact rootArtifact, PathScope scope) {
108114
return new DependencyResolverRequestBuilder()
109115
.session(session)
110116
.requestType(requestType)
111117
.rootArtifact(rootArtifact)
112-
.pathScope(PathScope.MAIN_RUNTIME)
118+
.pathScope(scope)
113119
.build();
114120
}
115121

@@ -395,7 +401,7 @@ static class DefaultDependencyResolverRequest extends BaseRequest implements Dep
395401
this.managedDependencies =
396402
unmodifiable(nonNull(managedDependencies, "managedDependencies cannot be null"));
397403
this.verbose = verbose;
398-
this.pathScope = pathScope;
404+
this.pathScope = nonNull(pathScope, "pathScope cannot be null");
399405
this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true;
400406
this.repositories = repositories;
401407
if (verbose && requestType != RequestType.COLLECT) {

maven-api-impl/src/main/java/org/apache/maven/internal/impl/AbstractSession.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -720,37 +720,42 @@ public DependencyCoordinates createDependencyCoordinates(@Nonnull Dependency dep
720720
* Shortcut for <code>getService(DependencyResolver.class).collect(...)</code>
721721
*
722722
* @throws DependencyResolverException if the dependency collection failed
723-
* @see DependencyResolver#collect(Session, Artifact)
723+
* @see DependencyResolver#collect(Session, Artifact, PathScope)
724724
*/
725725
@Nonnull
726726
@Override
727-
public Node collectDependencies(@Nonnull Artifact artifact) {
728-
return getService(DependencyResolver.class).collect(this, artifact).getRoot();
727+
public Node collectDependencies(@Nonnull Artifact artifact, @Nonnull PathScope scope) {
728+
return getService(DependencyResolver.class)
729+
.collect(this, artifact, scope)
730+
.getRoot();
729731
}
730732

731733
/**
732734
* Shortcut for <code>getService(DependencyResolver.class).collect(...)</code>
733735
*
734736
* @throws DependencyResolverException if the dependency collection failed
735-
* @see DependencyResolver#collect(Session, Project)
737+
* @see DependencyResolver#collect(Session, Project, PathScope)
736738
*/
737739
@Nonnull
738740
@Override
739-
public Node collectDependencies(@Nonnull Project project) {
740-
return getService(DependencyResolver.class).collect(this, project).getRoot();
741+
public Node collectDependencies(@Nonnull Project project, @Nonnull PathScope scope) {
742+
return getService(DependencyResolver.class)
743+
.collect(this, project, scope)
744+
.getRoot();
741745
}
742746

743747
/**
744748
* Shortcut for <code>getService(DependencyResolver.class).collect(...)</code>
745749
*
746750
* @throws DependencyResolverException if the dependency collection failed
747-
* @see DependencyResolver#collect(Session, DependencyCoordinates)
751+
* @see DependencyResolver#collect(Session, DependencyCoordinates, PathScope)
748752
*/
749753
@Nonnull
750754
@Override
751-
public Node collectDependencies(@Nonnull DependencyCoordinates dependency) {
752-
Node root =
753-
getService(DependencyResolver.class).collect(this, dependency).getRoot();
755+
public Node collectDependencies(@Nonnull DependencyCoordinates dependency, @Nonnull PathScope scope) {
756+
Node root = getService(DependencyResolver.class)
757+
.collect(this, dependency, scope)
758+
.getRoot();
754759
return root.getChildren().iterator().next();
755760
}
756761

maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolver.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.maven.api.RemoteRepository;
4040
import org.apache.maven.api.Session;
4141
import org.apache.maven.api.annotations.Nonnull;
42+
import org.apache.maven.api.annotations.Nullable;
4243
import org.apache.maven.api.di.Named;
4344
import org.apache.maven.api.di.Singleton;
4445
import org.apache.maven.api.services.ArtifactResolver;
@@ -56,6 +57,7 @@
5657
import org.eclipse.aether.collection.DependencyCollectionException;
5758
import org.eclipse.aether.graph.DependencyFilter;
5859
import org.eclipse.aether.graph.DependencyNode;
60+
import org.eclipse.aether.scope.ResolutionScope;
5961
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
6062
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
6163

@@ -96,12 +98,20 @@ public DependencyResolverResult collect(@Nonnull DependencyResolverRequest reque
9698
remoteRepositories =
9799
request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories();
98100
}
101+
ResolutionScope resolutionScope = null;
102+
if (request.getPathScope() != null) {
103+
resolutionScope = session.getSession()
104+
.getScopeManager()
105+
.getResolutionScope(request.getPathScope().id())
106+
.orElseThrow();
107+
}
99108
CollectRequest collectRequest = new CollectRequest()
100109
.setRootArtifact(rootArtifact != null ? session.toArtifact(rootArtifact) : null)
101110
.setRoot(root != null ? session.toDependency(root, false) : null)
102111
.setDependencies(session.toDependencies(dependencies, false))
103112
.setManagedDependencies(session.toDependencies(managedDependencies, true))
104113
.setRepositories(session.toRepositories(remoteRepositories));
114+
collectRequest.setResolutionScope(resolutionScope);
105115

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

133+
@Nonnull
123134
@Override
124-
public List<Node> flatten(Session s, Node node, PathScope scope) throws DependencyResolverException {
135+
public List<Node> flatten(@Nonnull Session s, @Nonnull Node node, @Nullable PathScope scope)
136+
throws DependencyResolverException {
125137
InternalSession session = InternalSession.from(s);
126138
DependencyNode root = cast(AbstractNode.class, node, "node").getDependencyNode();
127139
List<DependencyNode> dependencies = session.getRepositorySystem()
@@ -131,6 +143,9 @@ public List<Node> flatten(Session s, Node node, PathScope scope) throws Dependen
131143
}
132144

133145
private static DependencyFilter getScopeDependencyFilter(PathScope scope) {
146+
if (scope == null) {
147+
return null;
148+
}
134149
Set<String> scopes =
135150
scope.dependencyScopes().stream().map(DependencyScope::id).collect(Collectors.toSet());
136151
return (n, p) -> {

maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/ApiRunner.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@
6161
import org.apache.maven.internal.impl.AbstractSession;
6262
import org.apache.maven.internal.impl.InternalSession;
6363
import org.apache.maven.internal.impl.di.SessionScope;
64+
import org.apache.maven.internal.impl.resolver.scopes.Maven4ScopeManagerConfiguration;
6465
import org.eclipse.aether.DefaultRepositorySystemSession;
6566
import org.eclipse.aether.RepositorySystem;
6667
import org.eclipse.aether.RepositorySystemSession;
6768
import org.eclipse.aether.impl.RemoteRepositoryManager;
69+
import org.eclipse.aether.internal.impl.scope.ScopeManagerImpl;
6870
import org.eclipse.aether.repository.LocalRepository;
6971
import org.eclipse.aether.repository.LocalRepositoryManager;
7072

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

330332
DefaultRepositorySystemSession rsession = new DefaultRepositorySystemSession(h -> false);
333+
rsession.setScopeManager(new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE));
331334
rsession.setSystemProperties(properties);
332335
rsession.setConfigProperties(properties);
333336

maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/TestApiStandalone.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.maven.api.ArtifactCoordinates;
2525
import org.apache.maven.api.DownloadedArtifact;
2626
import org.apache.maven.api.Node;
27+
import org.apache.maven.api.PathScope;
2728
import org.apache.maven.api.Session;
2829
import org.apache.maven.api.services.ModelBuilder;
2930
import org.apache.maven.api.services.ModelBuilderRequest;
@@ -58,8 +59,8 @@ void testStandalone() {
5859
assertNotNull(res.getPath());
5960
assertTrue(Files.exists(res.getPath()));
6061

61-
Node node = session.collectDependencies(session.createDependencyCoordinates(coords));
62+
Node node = session.collectDependencies(session.createDependencyCoordinates(coords), PathScope.MAIN_RUNTIME);
6263
assertNotNull(node);
63-
assertEquals(8, node.getChildren().size());
64+
assertEquals(6, node.getChildren().size());
6465
}
6566
}

maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,9 @@ private <T> T loadV4Mojo(
642642
}
643643
} else {
644644
// collection
645-
DependencyResolverResult res =
646-
sessionV4.getService(DependencyResolver.class).collect(sessionV4, project);
645+
DependencyResolverResult res = sessionV4
646+
.getService(DependencyResolver.class)
647+
.collect(sessionV4, project, PathScope.MAIN_RUNTIME);
647648
if (field.getType() == DependencyResolverResult.class) {
648649
result = res;
649650
} else if (field.getType() == Node.class) {

0 commit comments

Comments
 (0)