|
21 | 21 | import javax.inject.Inject;
|
22 | 22 | import javax.inject.Named;
|
23 | 23 |
|
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
24 | 26 | import java.util.Objects;
|
25 | 27 | import java.util.stream.Collectors;
|
26 | 28 |
|
27 | 29 | import org.apache.maven.RepositoryUtils;
|
28 |
| -import org.apache.maven.artifact.Artifact; |
29 | 30 | import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
|
30 | 31 | import org.apache.maven.execution.MavenSession;
|
31 | 32 | import org.apache.maven.model.DependencyManagement;
|
32 | 33 | import org.apache.maven.project.MavenProject;
|
33 | 34 | import org.eclipse.aether.DefaultRepositorySystemSession;
|
34 | 35 | import org.eclipse.aether.RepositorySystem;
|
| 36 | +import org.eclipse.aether.RepositorySystemSession; |
35 | 37 | import org.eclipse.aether.artifact.ArtifactTypeRegistry;
|
36 | 38 | import org.eclipse.aether.collection.CollectRequest;
|
37 | 39 | import org.eclipse.aether.collection.DependencyCollectionException;
|
38 |
| -import org.eclipse.aether.collection.DependencySelector; |
| 40 | +import org.eclipse.aether.graph.Dependency; |
39 | 41 | import org.eclipse.aether.graph.DependencyNode;
|
40 | 42 | import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
|
41 |
| -import org.eclipse.aether.util.graph.selector.AndDependencySelector; |
42 | 43 | import org.eclipse.aether.util.graph.transformer.ConflictResolver;
|
43 | 44 |
|
44 | 45 | import static java.util.Optional.ofNullable;
|
| 46 | +import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED; |
| 47 | +import static org.apache.maven.artifact.Artifact.SCOPE_TEST; |
45 | 48 |
|
46 | 49 | /**
|
47 | 50 | * Resolver helper class.
|
@@ -72,59 +75,59 @@ class ResolveUtil {
|
72 | 75 | * Please consult {@link ConflictResolver} and {@link DependencyManagerUtils}>
|
73 | 76 | * </p>
|
74 | 77 | *
|
75 |
| - * @param selectors zero or more {@link DependencySelector} instances |
| 78 | + * @param excludedScopes a project dependency scope to excluded |
76 | 79 | * @return a Dependency Node which is the root of the project's dependency tree
|
77 | 80 | * @throws EnforcerRuleException thrown if the lookup fails
|
78 | 81 | */
|
79 |
| - DependencyNode resolveTransitiveDependenciesVerbose(DependencySelector... selectors) throws EnforcerRuleException { |
80 |
| - return resolveTransitiveDependencies(true, selectors); |
| 82 | + DependencyNode resolveTransitiveDependenciesVerbose(List<String> excludedScopes) throws EnforcerRuleException { |
| 83 | + return resolveTransitiveDependencies(true, true, excludedScopes); |
81 | 84 | }
|
82 | 85 |
|
83 | 86 | /**
|
84 | 87 | * Retrieves the {@link DependencyNode} instance containing the result of the transitive dependency
|
85 | 88 | * for the current {@link MavenProject}.
|
86 | 89 | *
|
87 |
| - * @param selectors zero or more {@link DependencySelector} instances |
88 | 90 | * @return a Dependency Node which is the root of the project's dependency tree
|
89 | 91 | * @throws EnforcerRuleException thrown if the lookup fails
|
90 | 92 | */
|
91 |
| - DependencyNode resolveTransitiveDependencies(DependencySelector... selectors) throws EnforcerRuleException { |
92 |
| - return resolveTransitiveDependencies(false, selectors); |
| 93 | + DependencyNode resolveTransitiveDependencies() throws EnforcerRuleException { |
| 94 | + return resolveTransitiveDependencies(false, true, Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED)); |
93 | 95 | }
|
94 | 96 |
|
95 |
| - private DependencyNode resolveTransitiveDependencies(boolean verbose, DependencySelector... selectors) |
96 |
| - throws EnforcerRuleException { |
| 97 | + private DependencyNode resolveTransitiveDependencies( |
| 98 | + boolean verbose, boolean excludeOptional, List<String> excludedScopes) throws EnforcerRuleException { |
97 | 99 |
|
98 | 100 | try {
|
| 101 | + RepositorySystemSession repositorySystemSession = session.getRepositorySession(); |
| 102 | + |
| 103 | + if (verbose) { |
| 104 | + DefaultRepositorySystemSession defaultRepositorySystemSession = |
| 105 | + new DefaultRepositorySystemSession(repositorySystemSession); |
| 106 | + defaultRepositorySystemSession.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true); |
| 107 | + defaultRepositorySystemSession.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true); |
| 108 | + repositorySystemSession = defaultRepositorySystemSession; |
| 109 | + } |
| 110 | + |
99 | 111 | MavenProject project = session.getCurrentProject();
|
100 | 112 | ArtifactTypeRegistry artifactTypeRegistry =
|
101 | 113 | session.getRepositorySession().getArtifactTypeRegistry();
|
102 | 114 |
|
103 |
| - DefaultRepositorySystemSession repositorySystemSession = |
104 |
| - new DefaultRepositorySystemSession(session.getRepositorySession()); |
105 |
| - |
106 |
| - if (selectors.length > 0) { |
107 |
| - repositorySystemSession.setDependencySelector(new AndDependencySelector(selectors)); |
108 |
| - } |
| 115 | + List<Dependency> dependencies = project.getDependencies().stream() |
| 116 | + .filter(d -> !(excludeOptional && d.isOptional())) |
| 117 | + .filter(d -> !excludedScopes.contains(d.getScope())) |
| 118 | + .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) |
| 119 | + .collect(Collectors.toList()); |
109 | 120 |
|
110 |
| - if (verbose) { |
111 |
| - repositorySystemSession.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true); |
112 |
| - repositorySystemSession.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true); |
113 |
| - } |
114 |
| - |
115 |
| - CollectRequest collectRequest = new CollectRequest( |
116 |
| - project.getDependencies().stream() |
| 121 | + List<Dependency> managedDependencies = ofNullable(project.getDependencyManagement()) |
| 122 | + .map(DependencyManagement::getDependencies) |
| 123 | + .map(list -> list.stream() |
117 | 124 | .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
|
118 |
| - .collect(Collectors.toList()), |
119 |
| - ofNullable(project.getDependencyManagement()) |
120 |
| - .map(DependencyManagement::getDependencies) |
121 |
| - .map(list -> list.stream() |
122 |
| - .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) |
123 |
| - .collect(Collectors.toList())) |
124 |
| - .orElse(null), |
125 |
| - project.getRemoteProjectRepositories()); |
126 |
| - Artifact artifact = project.getArtifact(); |
127 |
| - collectRequest.setRootArtifact(RepositoryUtils.toArtifact(artifact)); |
| 125 | + .collect(Collectors.toList())) |
| 126 | + .orElse(null); |
| 127 | + |
| 128 | + CollectRequest collectRequest = |
| 129 | + new CollectRequest(dependencies, managedDependencies, project.getRemoteProjectRepositories()); |
| 130 | + collectRequest.setRootArtifact(RepositoryUtils.toArtifact(project.getArtifact())); |
128 | 131 |
|
129 | 132 | return repositorySystem
|
130 | 133 | .collectDependencies(repositorySystemSession, collectRequest)
|
|
0 commit comments