1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.resolver;
20
21 import java.util.HashMap;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26
27 import org.apache.maven.api.Session;
28 import org.apache.maven.api.di.Inject;
29 import org.apache.maven.api.di.Named;
30 import org.apache.maven.api.di.Singleton;
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.api.services.ModelBuilder;
33 import org.apache.maven.api.services.ModelBuilderException;
34 import org.apache.maven.api.services.ModelBuilderRequest;
35 import org.apache.maven.api.services.ModelBuilderResult;
36 import org.apache.maven.api.services.ModelProblem;
37 import org.apache.maven.api.services.ModelRepositoryHolder;
38 import org.apache.maven.api.services.ModelResolver;
39 import org.apache.maven.api.services.ModelResolverException;
40 import org.apache.maven.api.services.ModelSource;
41 import org.apache.maven.internal.impl.InternalSession;
42 import org.apache.maven.internal.impl.model.ModelProblemUtils;
43 import org.codehaus.plexus.interpolation.util.StringUtils;
44 import org.eclipse.aether.RepositoryEvent;
45 import org.eclipse.aether.RepositoryEvent.EventType;
46 import org.eclipse.aether.RepositoryException;
47 import org.eclipse.aether.RepositorySystemSession;
48 import org.eclipse.aether.RequestTrace;
49 import org.eclipse.aether.artifact.Artifact;
50 import org.eclipse.aether.impl.ArtifactDescriptorReader;
51 import org.eclipse.aether.impl.ArtifactResolver;
52 import org.eclipse.aether.impl.RemoteRepositoryManager;
53 import org.eclipse.aether.impl.RepositoryEventDispatcher;
54 import org.eclipse.aether.impl.VersionRangeResolver;
55 import org.eclipse.aether.impl.VersionResolver;
56 import org.eclipse.aether.repository.WorkspaceReader;
57 import org.eclipse.aether.resolution.ArtifactDescriptorException;
58 import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
59 import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest;
60 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
61 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
62 import org.eclipse.aether.resolution.ArtifactRequest;
63 import org.eclipse.aether.resolution.ArtifactResolutionException;
64 import org.eclipse.aether.resolution.ArtifactResult;
65 import org.eclipse.aether.resolution.VersionRequest;
66 import org.eclipse.aether.resolution.VersionResolutionException;
67 import org.eclipse.aether.resolution.VersionResult;
68 import org.eclipse.aether.transfer.ArtifactNotFoundException;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72
73
74
75 @Named
76 @Singleton
77 public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader {
78 private final RemoteRepositoryManager remoteRepositoryManager;
79 private final VersionResolver versionResolver;
80 private final VersionRangeResolver versionRangeResolver;
81 private final ArtifactResolver artifactResolver;
82 private final RepositoryEventDispatcher repositoryEventDispatcher;
83 private final ModelBuilder modelBuilder;
84 private final Map<String, MavenArtifactRelocationSource> artifactRelocationSources;
85 private final ArtifactDescriptorReaderDelegate delegate;
86 private final Logger logger = LoggerFactory.getLogger(getClass());
87
88 @Inject
89 public DefaultArtifactDescriptorReader(
90 RemoteRepositoryManager remoteRepositoryManager,
91 VersionResolver versionResolver,
92 VersionRangeResolver versionRangeResolver,
93 ArtifactResolver artifactResolver,
94 ModelBuilder modelBuilder,
95 RepositoryEventDispatcher repositoryEventDispatcher,
96 Map<String, MavenArtifactRelocationSource> artifactRelocationSources) {
97 this.remoteRepositoryManager =
98 Objects.requireNonNull(remoteRepositoryManager, "remoteRepositoryManager cannot be null");
99 this.versionResolver = Objects.requireNonNull(versionResolver, "versionResolver cannot be null");
100 this.versionRangeResolver = Objects.requireNonNull(versionRangeResolver, "versionRangeResolver cannot be null");
101 this.artifactResolver = Objects.requireNonNull(artifactResolver, "artifactResolver cannot be null");
102 this.modelBuilder = Objects.requireNonNull(modelBuilder, "modelBuilder cannot be null");
103 this.repositoryEventDispatcher =
104 Objects.requireNonNull(repositoryEventDispatcher, "repositoryEventDispatcher cannot be null");
105 this.artifactRelocationSources =
106 Objects.requireNonNull(artifactRelocationSources, "artifactRelocationSources cannot be null");
107 this.delegate = new ArtifactDescriptorReaderDelegate();
108 }
109
110 @Override
111 public ArtifactDescriptorResult readArtifactDescriptor(
112 RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
113 ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
114
115 Model model = loadPom(session, request, result);
116 if (model != null) {
117 Map<String, Object> config = session.getConfigProperties();
118 ArtifactDescriptorReaderDelegate delegate =
119 (ArtifactDescriptorReaderDelegate) config.get(ArtifactDescriptorReaderDelegate.class.getName());
120
121 if (delegate == null) {
122 delegate = this.delegate;
123 }
124
125 delegate.populateResult(InternalSession.from(session), result, model);
126 }
127
128 return result;
129 }
130
131 @SuppressWarnings("MethodLength")
132 private Model loadPom(
133 RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
134 throws ArtifactDescriptorException {
135 RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
136
137 LinkedHashSet<String> visited = new LinkedHashSet<>();
138 for (Artifact a = request.getArtifact(); ; ) {
139 Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifactUnconditionally(a);
140 try {
141 VersionRequest versionRequest =
142 new VersionRequest(a, request.getRepositories(), request.getRequestContext());
143 versionRequest.setTrace(trace);
144 VersionResult versionResult = versionResolver.resolveVersion(session, versionRequest);
145
146 a = a.setVersion(versionResult.getVersion());
147
148 versionRequest =
149 new VersionRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
150 versionRequest.setTrace(trace);
151 versionResult = versionResolver.resolveVersion(session, versionRequest);
152
153 pomArtifact = pomArtifact.setVersion(versionResult.getVersion());
154 } catch (VersionResolutionException e) {
155 result.addException(e);
156 throw new ArtifactDescriptorException(result);
157 }
158
159 if (!visited.add(a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion())) {
160 RepositoryException exception =
161 new RepositoryException("Artifact relocations form a cycle: " + visited);
162 invalidDescriptor(session, trace, a, exception);
163 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
164 return null;
165 }
166 result.addException(exception);
167 throw new ArtifactDescriptorException(result);
168 }
169
170 ArtifactResult resolveResult;
171 try {
172 ArtifactRequest resolveRequest =
173 new ArtifactRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
174 resolveRequest.setTrace(trace);
175 resolveResult = artifactResolver.resolveArtifact(session, resolveRequest);
176 pomArtifact = resolveResult.getArtifact();
177 result.setRepository(resolveResult.getRepository());
178 } catch (ArtifactResolutionException e) {
179 if (e.getCause() instanceof ArtifactNotFoundException) {
180 missingDescriptor(session, trace, a, (Exception) e.getCause());
181 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_MISSING) != 0) {
182 return null;
183 }
184 }
185 result.addException(e);
186 throw new ArtifactDescriptorException(result);
187 }
188
189 Model model;
190
191
192 final WorkspaceReader workspace = session.getWorkspaceReader();
193 if (workspace instanceof MavenWorkspaceReader) {
194 model = ((MavenWorkspaceReader) workspace).findModel(pomArtifact);
195 if (model != null) {
196 return model;
197 }
198 }
199
200 try {
201 InternalSession iSession = InternalSession.from(session);
202 Session iSessionWithRepos = iSession.withRemoteRepositories(request.getRepositories().stream()
203 .map(iSession::getRemoteRepository)
204 .toList());
205 String gav =
206 pomArtifact.getGroupId() + ":" + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion();
207 ModelResolver modelResolver = new DefaultModelResolver();
208 ModelRepositoryHolder modelRepositoryHolder = new DefaultModelRepositoryHolder(
209 iSessionWithRepos,
210 DefaultModelRepositoryHolder.RepositoryMerging.REQUEST_DOMINANT,
211 iSessionWithRepos.getRemoteRepositories());
212 ModelBuilderRequest modelRequest = ModelBuilderRequest.builder()
213 .session(iSessionWithRepos)
214 .projectBuild(false)
215 .processPlugins(false)
216 .twoPhaseBuilding(false)
217 .source(ModelSource.fromPath(pomArtifact.getPath(), gav))
218
219
220 .systemProperties(toProperties(session.getUserProperties(), session.getSystemProperties()))
221 .userProperties(Map.of())
222 .modelResolver(modelResolver)
223 .modelRepositoryHolder(modelRepositoryHolder)
224 .modelCache(DefaultModelCache.newInstance(session, false))
225 .build();
226
227 ModelBuilderResult modelResult = modelBuilder.build(modelRequest);
228
229
230 if (!modelResult.getProblems().isEmpty()) {
231 List<ModelProblem> problems = modelResult.getProblems();
232 if (logger.isDebugEnabled()) {
233 String problem = (problems.size() == 1) ? "problem" : "problems";
234 String problemPredicate = problem + ((problems.size() == 1) ? " was" : " were");
235 String message = String.format(
236 "%s %s encountered while building the effective model for %s during %s\n",
237 problems.size(),
238 problemPredicate,
239 request.getArtifact(),
240 RequestTraceHelper.interpretTrace(true, request.getTrace()));
241 message += StringUtils.capitalizeFirstLetter(problem);
242 for (ModelProblem modelProblem : problems) {
243 message += String.format(
244 "\n* %s @ %s",
245 modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null));
246 }
247 logger.warn(message);
248 } else {
249 logger.warn(
250 "{} {} encountered while building the effective model for {} during {} (use -X to see details)",
251 problems.size(),
252 (problems.size() == 1) ? "problem was" : "problems were",
253 request.getArtifact(),
254 RequestTraceHelper.interpretTrace(false, request.getTrace()));
255 }
256 }
257 model = modelResult.getEffectiveModel();
258 } catch (ModelBuilderException e) {
259 for (ModelProblem problem : e.getResult().getProblems()) {
260 if (problem.getException() instanceof ModelResolverException) {
261 result.addException(problem.getException());
262 throw new ArtifactDescriptorException(result);
263 }
264 }
265 invalidDescriptor(session, trace, a, e);
266 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
267 return null;
268 }
269 result.addException(e);
270 throw new ArtifactDescriptorException(result);
271 }
272
273 Artifact relocatedArtifact = getRelocation(session, result, model);
274 if (relocatedArtifact != null) {
275 if (withinSameGav(relocatedArtifact, a)) {
276 result.setArtifact(relocatedArtifact);
277 return model;
278 } else {
279 result.addRelocation(a);
280 a = relocatedArtifact;
281 result.setArtifact(a);
282 }
283 } else {
284 return model;
285 }
286 }
287 }
288
289 private boolean withinSameGav(Artifact a1, Artifact a2) {
290 return Objects.equals(a1.getGroupId(), a2.getGroupId())
291 && Objects.equals(a1.getArtifactId(), a2.getArtifactId())
292 && Objects.equals(a1.getVersion(), a2.getVersion());
293 }
294
295 private Map<String, String> toProperties(Map<String, String> dominant, Map<String, String> recessive) {
296 Map<String, String> props = new HashMap<>();
297 if (recessive != null) {
298 props.putAll(recessive);
299 }
300 if (dominant != null) {
301 props.putAll(dominant);
302 }
303 return props;
304 }
305
306 private Artifact getRelocation(
307 RepositorySystemSession session, ArtifactDescriptorResult artifactDescriptorResult, Model model)
308 throws ArtifactDescriptorException {
309 Artifact result = null;
310 for (MavenArtifactRelocationSource source : artifactRelocationSources.values()) {
311 result = source.relocatedTarget(session, artifactDescriptorResult, model);
312 if (result != null) {
313 break;
314 }
315 }
316 return result;
317 }
318
319 private void missingDescriptor(
320 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
321 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_MISSING);
322 event.setTrace(trace);
323 event.setArtifact(artifact);
324 event.setException(exception);
325
326 repositoryEventDispatcher.dispatch(event.build());
327 }
328
329 private void invalidDescriptor(
330 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
331 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_INVALID);
332 event.setTrace(trace);
333 event.setArtifact(artifact);
334 event.setException(exception);
335
336 repositoryEventDispatcher.dispatch(event.build());
337 }
338
339 private int getPolicy(RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request) {
340 ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
341 if (policy == null) {
342 return ArtifactDescriptorPolicy.STRICT;
343 }
344 return policy.getPolicy(session, new ArtifactDescriptorPolicyRequest(a, request.getRequestContext()));
345 }
346 }