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