1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Properties;
30 import java.util.Set;
31
32 import org.apache.maven.model.DistributionManagement;
33 import org.apache.maven.model.Model;
34 import org.apache.maven.model.Relocation;
35 import org.apache.maven.model.building.DefaultModelBuilderFactory;
36 import org.apache.maven.model.building.DefaultModelBuildingRequest;
37 import org.apache.maven.model.building.FileModelSource;
38 import org.apache.maven.model.building.ModelBuilder;
39 import org.apache.maven.model.building.ModelBuildingException;
40 import org.apache.maven.model.building.ModelBuildingRequest;
41 import org.apache.maven.model.building.ModelBuildingResult;
42 import org.apache.maven.model.building.ModelProblem;
43 import org.apache.maven.model.building.ModelProblemUtils;
44 import org.apache.maven.model.resolution.UnresolvableModelException;
45 import org.eclipse.aether.RepositoryEvent;
46 import org.eclipse.aether.RepositoryEvent.EventType;
47 import org.eclipse.aether.RepositoryException;
48 import org.eclipse.aether.RepositorySystemSession;
49 import org.eclipse.aether.RequestTrace;
50 import org.eclipse.aether.artifact.Artifact;
51 import org.eclipse.aether.impl.ArtifactDescriptorReader;
52 import org.eclipse.aether.impl.ArtifactResolver;
53 import org.eclipse.aether.impl.RemoteRepositoryManager;
54 import org.eclipse.aether.impl.RepositoryEventDispatcher;
55 import org.eclipse.aether.impl.VersionRangeResolver;
56 import org.eclipse.aether.impl.VersionResolver;
57 import org.eclipse.aether.repository.WorkspaceReader;
58 import org.eclipse.aether.repository.WorkspaceRepository;
59 import org.eclipse.aether.resolution.ArtifactDescriptorException;
60 import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
61 import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest;
62 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
63 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
64 import org.eclipse.aether.resolution.ArtifactRequest;
65 import org.eclipse.aether.resolution.ArtifactResolutionException;
66 import org.eclipse.aether.resolution.ArtifactResult;
67 import org.eclipse.aether.resolution.VersionRequest;
68 import org.eclipse.aether.resolution.VersionResolutionException;
69 import org.eclipse.aether.resolution.VersionResult;
70 import org.eclipse.aether.spi.locator.Service;
71 import org.eclipse.aether.spi.locator.ServiceLocator;
72 import org.eclipse.aether.transfer.ArtifactNotFoundException;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76
77
78
79 @Named
80 @Singleton
81 public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader, Service {
82 private RemoteRepositoryManager remoteRepositoryManager;
83
84 private VersionResolver versionResolver;
85
86 private VersionRangeResolver versionRangeResolver;
87
88 private ArtifactResolver artifactResolver;
89
90 private RepositoryEventDispatcher repositoryEventDispatcher;
91
92 private ModelBuilder modelBuilder;
93
94 private ModelCacheFactory modelCacheFactory;
95
96 private final ArtifactDescriptorReaderDelegate artifactDescriptorReaderDelegate =
97 new ArtifactDescriptorReaderDelegate();
98
99 private final Logger logger = LoggerFactory.getLogger(getClass());
100
101 @Deprecated
102 public DefaultArtifactDescriptorReader() {
103
104 }
105
106 @Inject
107 public DefaultArtifactDescriptorReader(
108 RemoteRepositoryManager remoteRepositoryManager,
109 VersionResolver versionResolver,
110 VersionRangeResolver versionRangeResolver,
111 ArtifactResolver artifactResolver,
112 ModelBuilder modelBuilder,
113 RepositoryEventDispatcher repositoryEventDispatcher,
114 ModelCacheFactory modelCacheFactory) {
115 setRemoteRepositoryManager(remoteRepositoryManager);
116 setVersionResolver(versionResolver);
117 setVersionRangeResolver(versionRangeResolver);
118 setArtifactResolver(artifactResolver);
119 setModelBuilder(modelBuilder);
120 setRepositoryEventDispatcher(repositoryEventDispatcher);
121 setModelCacheFactory(modelCacheFactory);
122 }
123
124 @Deprecated
125 public void initService(ServiceLocator locator) {
126 setRemoteRepositoryManager(locator.getService(RemoteRepositoryManager.class));
127 setVersionResolver(locator.getService(VersionResolver.class));
128 setVersionRangeResolver(locator.getService(VersionRangeResolver.class));
129 setArtifactResolver(locator.getService(ArtifactResolver.class));
130 modelBuilder = locator.getService(ModelBuilder.class);
131 if (modelBuilder == null) {
132 setModelBuilder(new DefaultModelBuilderFactory().newInstance());
133 }
134 setRepositoryEventDispatcher(locator.getService(RepositoryEventDispatcher.class));
135 setModelCacheFactory(locator.getService(ModelCacheFactory.class));
136 }
137
138 public DefaultArtifactDescriptorReader setRemoteRepositoryManager(RemoteRepositoryManager remoteRepositoryManager) {
139 this.remoteRepositoryManager =
140 Objects.requireNonNull(remoteRepositoryManager, "remoteRepositoryManager cannot be null");
141 return this;
142 }
143
144 public DefaultArtifactDescriptorReader setVersionResolver(VersionResolver versionResolver) {
145 this.versionResolver = Objects.requireNonNull(versionResolver, "versionResolver cannot be null");
146 return this;
147 }
148
149
150 public DefaultArtifactDescriptorReader setVersionRangeResolver(VersionRangeResolver versionRangeResolver) {
151 this.versionRangeResolver = Objects.requireNonNull(versionRangeResolver, "versionRangeResolver cannot be null");
152 return this;
153 }
154
155 public DefaultArtifactDescriptorReader setArtifactResolver(ArtifactResolver artifactResolver) {
156 this.artifactResolver = Objects.requireNonNull(artifactResolver, "artifactResolver cannot be null");
157 return this;
158 }
159
160 public DefaultArtifactDescriptorReader setRepositoryEventDispatcher(
161 RepositoryEventDispatcher repositoryEventDispatcher) {
162 this.repositoryEventDispatcher =
163 Objects.requireNonNull(repositoryEventDispatcher, "repositoryEventDispatcher cannot be null");
164 return this;
165 }
166
167 public DefaultArtifactDescriptorReader setModelBuilder(ModelBuilder modelBuilder) {
168 this.modelBuilder = Objects.requireNonNull(modelBuilder, "modelBuilder cannot be null");
169 return this;
170 }
171
172 public DefaultArtifactDescriptorReader setModelCacheFactory(ModelCacheFactory modelCacheFactory) {
173 this.modelCacheFactory = Objects.requireNonNull(modelCacheFactory, "modelCacheFactory cannot be null");
174 return this;
175 }
176
177 @Override
178 public ArtifactDescriptorResult readArtifactDescriptor(
179 RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
180 ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
181
182 Model model = loadPom(session, request, result);
183 if (model != null) {
184 Map<String, Object> config = session.getConfigProperties();
185 ArtifactDescriptorReaderDelegate delegate =
186 (ArtifactDescriptorReaderDelegate) config.get(ArtifactDescriptorReaderDelegate.class.getName());
187
188 if (delegate == null) {
189 delegate = artifactDescriptorReaderDelegate;
190 }
191
192 delegate.populateResult(session, result, model);
193 }
194
195 return result;
196 }
197
198 private Model loadPom(
199 RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
200 throws ArtifactDescriptorException {
201 RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
202
203 Set<String> visited = new LinkedHashSet<>();
204 for (Artifact a = request.getArtifact(); ; ) {
205 Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact(a);
206 try {
207 VersionRequest versionRequest =
208 new VersionRequest(a, request.getRepositories(), request.getRequestContext());
209 versionRequest.setTrace(trace);
210 VersionResult versionResult = versionResolver.resolveVersion(session, versionRequest);
211
212 a = a.setVersion(versionResult.getVersion());
213
214 versionRequest =
215 new VersionRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
216 versionRequest.setTrace(trace);
217 versionResult = versionResolver.resolveVersion(session, versionRequest);
218
219 pomArtifact = pomArtifact.setVersion(versionResult.getVersion());
220 } catch (VersionResolutionException e) {
221 result.addException(e);
222 throw new ArtifactDescriptorException(result);
223 }
224
225 if (!visited.add(a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion())) {
226 RepositoryException exception =
227 new RepositoryException("Artifact relocations form a cycle: " + visited);
228 invalidDescriptor(session, trace, a, exception);
229 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
230 return null;
231 }
232 result.addException(exception);
233 throw new ArtifactDescriptorException(result);
234 }
235
236 ArtifactResult resolveResult;
237 try {
238 ArtifactRequest resolveRequest =
239 new ArtifactRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
240 resolveRequest.setTrace(trace);
241 resolveResult = artifactResolver.resolveArtifact(session, resolveRequest);
242 pomArtifact = resolveResult.getArtifact();
243 result.setRepository(resolveResult.getRepository());
244 } catch (ArtifactResolutionException e) {
245 if (e.getCause() instanceof ArtifactNotFoundException) {
246 missingDescriptor(session, trace, a, (Exception) e.getCause());
247 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_MISSING) != 0) {
248 return null;
249 }
250 }
251 result.addException(e);
252 throw new ArtifactDescriptorException(result);
253 }
254
255 Model model;
256
257
258 final WorkspaceReader workspace = session.getWorkspaceReader();
259 if (workspace instanceof MavenWorkspaceReader) {
260 model = ((MavenWorkspaceReader) workspace).findModel(pomArtifact);
261 if (model != null) {
262 return model;
263 }
264 }
265
266 try {
267 ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
268 modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
269 modelRequest.setProcessPlugins(false);
270 modelRequest.setTwoPhaseBuilding(false);
271
272
273 modelRequest.setSystemProperties(
274 toProperties(session.getUserProperties(), session.getSystemProperties()));
275 modelRequest.setUserProperties(new Properties());
276 modelRequest.setModelCache(modelCacheFactory.createCache(session));
277 modelRequest.setModelResolver(new DefaultModelResolver(
278 session,
279 trace.newChild(modelRequest),
280 request.getRequestContext(),
281 artifactResolver,
282 versionRangeResolver,
283 remoteRepositoryManager,
284 request.getRepositories()));
285 if (resolveResult.getRepository() instanceof WorkspaceRepository) {
286 modelRequest.setPomFile(pomArtifact.getFile());
287 } else {
288 modelRequest.setModelSource(new FileModelSource(pomArtifact.getFile()));
289 }
290
291 ModelBuildingResult modelResult = modelBuilder.build(modelRequest);
292
293
294 if (!modelResult.getProblems().isEmpty()) {
295 List<ModelProblem> problems = modelResult.getProblems();
296 logger.warn(
297 "{} {} encountered while building the effective model for {}",
298 problems.size(),
299 (problems.size() == 1) ? "problem was" : "problems were",
300 request.getArtifact());
301 if (logger.isDebugEnabled()) {
302 for (ModelProblem problem : problems) {
303 logger.warn(
304 "{} @ {}", problem.getMessage(), ModelProblemUtils.formatLocation(problem, null));
305 }
306 }
307 }
308 model = modelResult.getEffectiveModel();
309 } catch (ModelBuildingException e) {
310 for (ModelProblem problem : e.getProblems()) {
311 if (problem.getException() instanceof UnresolvableModelException) {
312 result.addException(problem.getException());
313 throw new ArtifactDescriptorException(result);
314 }
315 }
316 invalidDescriptor(session, trace, a, e);
317 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
318 return null;
319 }
320 result.addException(e);
321 throw new ArtifactDescriptorException(result);
322 }
323
324 Relocation relocation = getRelocation(model);
325
326 if (relocation != null) {
327 result.addRelocation(a);
328 a = new RelocatedArtifact(
329 a,
330 relocation.getGroupId(),
331 relocation.getArtifactId(),
332 relocation.getVersion(),
333 relocation.getMessage());
334 result.setArtifact(a);
335 } else {
336 return model;
337 }
338 }
339 }
340
341 private Properties toProperties(Map<String, String> dominant, Map<String, String> recessive) {
342 Properties props = new Properties();
343 if (recessive != null) {
344 props.putAll(recessive);
345 }
346 if (dominant != null) {
347 props.putAll(dominant);
348 }
349 return props;
350 }
351
352 private Relocation getRelocation(Model model) {
353 Relocation relocation = null;
354 DistributionManagement distMgmt = model.getDistributionManagement();
355 if (distMgmt != null) {
356 relocation = distMgmt.getRelocation();
357 }
358 return relocation;
359 }
360
361 private void missingDescriptor(
362 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
363 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_MISSING);
364 event.setTrace(trace);
365 event.setArtifact(artifact);
366 event.setException(exception);
367
368 repositoryEventDispatcher.dispatch(event.build());
369 }
370
371 private void invalidDescriptor(
372 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
373 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_INVALID);
374 event.setTrace(trace);
375 event.setArtifact(artifact);
376 event.setException(exception);
377
378 repositoryEventDispatcher.dispatch(event.build());
379 }
380
381 private int getPolicy(RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request) {
382 ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
383 if (policy == null) {
384 return ArtifactDescriptorPolicy.STRICT;
385 }
386 return policy.getPolicy(session, new ArtifactDescriptorPolicyRequest(a, request.getRequestContext()));
387 }
388 }