1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30
31 import org.apache.maven.RepositoryUtils;
32 import org.apache.maven.api.DependencyScope;
33 import org.apache.maven.model.Dependency;
34 import org.apache.maven.model.Plugin;
35 import org.apache.maven.plugin.PluginResolutionException;
36 import org.eclipse.aether.DefaultRepositorySystemSession;
37 import org.eclipse.aether.RepositorySystem;
38 import org.eclipse.aether.RepositorySystemSession;
39 import org.eclipse.aether.RequestTrace;
40 import org.eclipse.aether.artifact.Artifact;
41 import org.eclipse.aether.artifact.DefaultArtifact;
42 import org.eclipse.aether.collection.CollectRequest;
43 import org.eclipse.aether.collection.DependencyCollectionException;
44 import org.eclipse.aether.graph.DependencyFilter;
45 import org.eclipse.aether.graph.DependencyNode;
46 import org.eclipse.aether.repository.RemoteRepository;
47 import org.eclipse.aether.resolution.ArtifactDescriptorException;
48 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
49 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
50 import org.eclipse.aether.resolution.ArtifactRequest;
51 import org.eclipse.aether.resolution.ArtifactResolutionException;
52 import org.eclipse.aether.resolution.DependencyRequest;
53 import org.eclipse.aether.resolution.DependencyResolutionException;
54 import org.eclipse.aether.resolution.DependencyResult;
55 import org.eclipse.aether.util.filter.AndDependencyFilter;
56 import org.eclipse.aether.util.filter.ScopeDependencyFilter;
57 import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
58 import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62
63
64
65
66
67
68
69 @Named
70 @Singleton
71 public class DefaultPluginDependenciesResolver implements PluginDependenciesResolver {
72 private static final String REPOSITORY_CONTEXT = "plugin";
73
74 private final Logger logger = LoggerFactory.getLogger(getClass());
75
76 private final RepositorySystem repoSystem;
77
78 private final List<MavenPluginDependenciesValidator> dependenciesValidators;
79
80 @Inject
81 public DefaultPluginDependenciesResolver(
82 RepositorySystem repoSystem, List<MavenPluginDependenciesValidator> dependenciesValidators) {
83 this.repoSystem = repoSystem;
84 this.dependenciesValidators = dependenciesValidators;
85 }
86
87 private Artifact toArtifact(Plugin plugin, RepositorySystemSession session) {
88 return new DefaultArtifact(
89 plugin.getGroupId(),
90 plugin.getArtifactId(),
91 null,
92 "jar",
93 plugin.getVersion(),
94 session.getArtifactTypeRegistry().get("maven-plugin"));
95 }
96
97 public Artifact resolve(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session)
98 throws PluginResolutionException {
99 RequestTrace trace = RequestTrace.newChild(null, plugin);
100
101 Artifact pluginArtifact = toArtifact(plugin, session);
102
103 try {
104 DefaultRepositorySystemSession pluginSession = new DefaultRepositorySystemSession(session);
105 pluginSession.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, false));
106
107 ArtifactDescriptorRequest request =
108 new ArtifactDescriptorRequest(pluginArtifact, repositories, REPOSITORY_CONTEXT);
109 request.setTrace(trace);
110 ArtifactDescriptorResult result = repoSystem.readArtifactDescriptor(pluginSession, request);
111
112 for (MavenPluginDependenciesValidator dependenciesValidator : dependenciesValidators) {
113 dependenciesValidator.validate(session, pluginArtifact, result);
114 }
115
116 pluginArtifact = result.getArtifact();
117
118 if (logger.isWarnEnabled() && !result.getRelocations().isEmpty()) {
119 String message =
120 pluginArtifact instanceof org.apache.maven.internal.impl.resolver.RelocatedArtifact relocated
121 ? ": " + relocated.getMessage()
122 : "";
123 logger.warn(
124 "The artifact {} has been relocated to {}{}",
125 result.getRelocations().get(0),
126 pluginArtifact,
127 message);
128 }
129
130 String requiredMavenVersion = (String) result.getProperties().get("prerequisites.maven");
131 if (requiredMavenVersion != null) {
132 Map<String, String> props = new LinkedHashMap<>(pluginArtifact.getProperties());
133 props.put("requiredMavenVersion", requiredMavenVersion);
134 pluginArtifact = pluginArtifact.setProperties(props);
135 }
136 } catch (ArtifactDescriptorException e) {
137 throw new PluginResolutionException(plugin, e.getResult().getExceptions(), e);
138 }
139
140 try {
141 ArtifactRequest request = new ArtifactRequest(pluginArtifact, repositories, REPOSITORY_CONTEXT);
142 request.setTrace(trace);
143 pluginArtifact = repoSystem.resolveArtifact(session, request).getArtifact();
144 } catch (ArtifactResolutionException e) {
145 throw new PluginResolutionException(plugin, e.getResult().getExceptions(), e);
146 }
147
148 return pluginArtifact;
149 }
150
151
152
153
154 public DependencyResult resolveCoreExtension(
155 Plugin plugin,
156 DependencyFilter dependencyFilter,
157 List<RemoteRepository> repositories,
158 RepositorySystemSession session)
159 throws PluginResolutionException {
160 return resolveInternal(plugin, null , dependencyFilter, repositories, session);
161 }
162
163 public DependencyResult resolvePlugin(
164 Plugin plugin,
165 Artifact pluginArtifact,
166 DependencyFilter dependencyFilter,
167 List<RemoteRepository> repositories,
168 RepositorySystemSession session)
169 throws PluginResolutionException {
170 return resolveInternal(plugin, pluginArtifact, dependencyFilter, repositories, session);
171 }
172
173 public DependencyNode resolve(
174 Plugin plugin,
175 Artifact pluginArtifact,
176 DependencyFilter dependencyFilter,
177 List<RemoteRepository> repositories,
178 RepositorySystemSession session)
179 throws PluginResolutionException {
180 return resolveInternal(plugin, pluginArtifact, dependencyFilter, repositories, session)
181 .getRoot();
182 }
183
184 private DependencyResult resolveInternal(
185 Plugin plugin,
186 Artifact pluginArtifact,
187 DependencyFilter dependencyFilter,
188 List<RemoteRepository> repositories,
189 RepositorySystemSession session)
190 throws PluginResolutionException {
191 RequestTrace trace = RequestTrace.newChild(null, plugin);
192
193 if (pluginArtifact == null) {
194 pluginArtifact = toArtifact(plugin, session);
195 }
196
197 DependencyFilter collectionFilter = new ScopeDependencyFilter("provided", "test");
198 DependencyFilter resolutionFilter = AndDependencyFilter.newInstance(collectionFilter, dependencyFilter);
199
200 DependencyNode node;
201
202 try {
203 DefaultRepositorySystemSession pluginSession = new DefaultRepositorySystemSession(session);
204 pluginSession.setDependencySelector(session.getDependencySelector());
205 pluginSession.setDependencyGraphTransformer(session.getDependencyGraphTransformer());
206
207 CollectRequest request = new CollectRequest();
208 request.setRequestContext(REPOSITORY_CONTEXT);
209 request.setRepositories(repositories);
210 request.setRoot(new org.eclipse.aether.graph.Dependency(pluginArtifact, null));
211 for (Dependency dependency : plugin.getDependencies()) {
212 org.eclipse.aether.graph.Dependency pluginDep =
213 RepositoryUtils.toDependency(dependency, session.getArtifactTypeRegistry());
214 if (!DependencyScope.SYSTEM.is(pluginDep.getScope())) {
215 pluginDep = pluginDep.setScope(DependencyScope.RUNTIME.id());
216 }
217 request.addDependency(pluginDep);
218 }
219
220 DependencyRequest depRequest = new DependencyRequest(request, resolutionFilter);
221 depRequest.setTrace(trace);
222
223 request.setTrace(RequestTrace.newChild(trace, depRequest));
224
225 node = repoSystem.collectDependencies(pluginSession, request).getRoot();
226
227 if (logger.isDebugEnabled()) {
228 node.accept(new DependencyGraphDumper(logger::debug));
229 }
230
231 depRequest.setRoot(node);
232 return repoSystem.resolveDependencies(session, depRequest);
233 } catch (DependencyCollectionException e) {
234 throw new PluginResolutionException(plugin, e.getResult().getExceptions(), e);
235 } catch (DependencyResolutionException e) {
236 List<Exception> exceptions = Stream.concat(
237 e.getResult().getCollectExceptions().stream(),
238 e.getResult().getArtifactResults().stream()
239 .filter(r -> !r.isResolved())
240 .flatMap(r -> r.getExceptions().stream()))
241 .collect(Collectors.toList());
242 throw new PluginResolutionException(plugin, exceptions, e);
243 }
244 }
245 }