View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.codehaus.plexus.util.StringUtils;
46  import org.eclipse.aether.RepositoryEvent;
47  import org.eclipse.aether.RepositoryEvent.EventType;
48  import org.eclipse.aether.RepositoryException;
49  import org.eclipse.aether.RepositorySystemSession;
50  import org.eclipse.aether.RequestTrace;
51  import org.eclipse.aether.artifact.Artifact;
52  import org.eclipse.aether.impl.ArtifactDescriptorReader;
53  import org.eclipse.aether.impl.ArtifactResolver;
54  import org.eclipse.aether.impl.RemoteRepositoryManager;
55  import org.eclipse.aether.impl.RepositoryEventDispatcher;
56  import org.eclipse.aether.impl.VersionRangeResolver;
57  import org.eclipse.aether.impl.VersionResolver;
58  import org.eclipse.aether.repository.WorkspaceReader;
59  import org.eclipse.aether.repository.WorkspaceRepository;
60  import org.eclipse.aether.resolution.ArtifactDescriptorException;
61  import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
62  import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest;
63  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
64  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
65  import org.eclipse.aether.resolution.ArtifactRequest;
66  import org.eclipse.aether.resolution.ArtifactResolutionException;
67  import org.eclipse.aether.resolution.ArtifactResult;
68  import org.eclipse.aether.resolution.VersionRequest;
69  import org.eclipse.aether.resolution.VersionResolutionException;
70  import org.eclipse.aether.resolution.VersionResult;
71  import org.eclipse.aether.spi.locator.Service;
72  import org.eclipse.aether.spi.locator.ServiceLocator;
73  import org.eclipse.aether.transfer.ArtifactNotFoundException;
74  import org.slf4j.Logger;
75  import org.slf4j.LoggerFactory;
76  
77  /**
78   * @author Benjamin Bentmann
79   */
80  @Named
81  @Singleton
82  public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader, Service {
83      private RemoteRepositoryManager remoteRepositoryManager;
84  
85      private VersionResolver versionResolver;
86  
87      private VersionRangeResolver versionRangeResolver;
88  
89      private ArtifactResolver artifactResolver;
90  
91      private RepositoryEventDispatcher repositoryEventDispatcher;
92  
93      private ModelBuilder modelBuilder;
94  
95      private ModelCacheFactory modelCacheFactory;
96  
97      private final ArtifactDescriptorReaderDelegate artifactDescriptorReaderDelegate =
98              new ArtifactDescriptorReaderDelegate();
99  
100     private final Logger logger = LoggerFactory.getLogger(getClass());
101 
102     @Deprecated
103     public DefaultArtifactDescriptorReader() {
104         // enable no-arg constructor
105     }
106 
107     @Inject
108     public DefaultArtifactDescriptorReader(
109             RemoteRepositoryManager remoteRepositoryManager,
110             VersionResolver versionResolver,
111             VersionRangeResolver versionRangeResolver,
112             ArtifactResolver artifactResolver,
113             ModelBuilder modelBuilder,
114             RepositoryEventDispatcher repositoryEventDispatcher,
115             ModelCacheFactory modelCacheFactory) {
116         setRemoteRepositoryManager(remoteRepositoryManager);
117         setVersionResolver(versionResolver);
118         setVersionRangeResolver(versionRangeResolver);
119         setArtifactResolver(artifactResolver);
120         setModelBuilder(modelBuilder);
121         setRepositoryEventDispatcher(repositoryEventDispatcher);
122         setModelCacheFactory(modelCacheFactory);
123     }
124 
125     @Deprecated
126     public void initService(ServiceLocator locator) {
127         setRemoteRepositoryManager(locator.getService(RemoteRepositoryManager.class));
128         setVersionResolver(locator.getService(VersionResolver.class));
129         setVersionRangeResolver(locator.getService(VersionRangeResolver.class));
130         setArtifactResolver(locator.getService(ArtifactResolver.class));
131         modelBuilder = locator.getService(ModelBuilder.class);
132         if (modelBuilder == null) {
133             setModelBuilder(new DefaultModelBuilderFactory().newInstance());
134         }
135         setRepositoryEventDispatcher(locator.getService(RepositoryEventDispatcher.class));
136         setModelCacheFactory(locator.getService(ModelCacheFactory.class));
137     }
138 
139     public DefaultArtifactDescriptorReader setRemoteRepositoryManager(RemoteRepositoryManager remoteRepositoryManager) {
140         this.remoteRepositoryManager =
141                 Objects.requireNonNull(remoteRepositoryManager, "remoteRepositoryManager cannot be null");
142         return this;
143     }
144 
145     public DefaultArtifactDescriptorReader setVersionResolver(VersionResolver versionResolver) {
146         this.versionResolver = Objects.requireNonNull(versionResolver, "versionResolver cannot be null");
147         return this;
148     }
149 
150     /** @since 3.2.2 */
151     public DefaultArtifactDescriptorReader setVersionRangeResolver(VersionRangeResolver versionRangeResolver) {
152         this.versionRangeResolver = Objects.requireNonNull(versionRangeResolver, "versionRangeResolver cannot be null");
153         return this;
154     }
155 
156     public DefaultArtifactDescriptorReader setArtifactResolver(ArtifactResolver artifactResolver) {
157         this.artifactResolver = Objects.requireNonNull(artifactResolver, "artifactResolver cannot be null");
158         return this;
159     }
160 
161     public DefaultArtifactDescriptorReader setRepositoryEventDispatcher(
162             RepositoryEventDispatcher repositoryEventDispatcher) {
163         this.repositoryEventDispatcher =
164                 Objects.requireNonNull(repositoryEventDispatcher, "repositoryEventDispatcher cannot be null");
165         return this;
166     }
167 
168     public DefaultArtifactDescriptorReader setModelBuilder(ModelBuilder modelBuilder) {
169         this.modelBuilder = Objects.requireNonNull(modelBuilder, "modelBuilder cannot be null");
170         return this;
171     }
172 
173     public DefaultArtifactDescriptorReader setModelCacheFactory(ModelCacheFactory modelCacheFactory) {
174         this.modelCacheFactory = Objects.requireNonNull(modelCacheFactory, "modelCacheFactory cannot be null");
175         return this;
176     }
177 
178     @Override
179     public ArtifactDescriptorResult readArtifactDescriptor(
180             RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
181         ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
182 
183         Model model = loadPom(session, request, result);
184         if (model != null) {
185             Map<String, Object> config = session.getConfigProperties();
186             ArtifactDescriptorReaderDelegate delegate =
187                     (ArtifactDescriptorReaderDelegate) config.get(ArtifactDescriptorReaderDelegate.class.getName());
188 
189             if (delegate == null) {
190                 delegate = artifactDescriptorReaderDelegate;
191             }
192 
193             delegate.populateResult(session, result, model);
194         }
195 
196         return result;
197     }
198 
199     @SuppressWarnings("MethodLength")
200     private Model loadPom(
201             RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
202             throws ArtifactDescriptorException {
203         RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
204 
205         Set<String> visited = new LinkedHashSet<>();
206         for (Artifact a = request.getArtifact(); ; ) {
207             Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact(a);
208             try {
209                 VersionRequest versionRequest =
210                         new VersionRequest(a, request.getRepositories(), request.getRequestContext());
211                 versionRequest.setTrace(trace);
212                 VersionResult versionResult = versionResolver.resolveVersion(session, versionRequest);
213 
214                 a = a.setVersion(versionResult.getVersion());
215 
216                 versionRequest =
217                         new VersionRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
218                 versionRequest.setTrace(trace);
219                 versionResult = versionResolver.resolveVersion(session, versionRequest);
220 
221                 pomArtifact = pomArtifact.setVersion(versionResult.getVersion());
222             } catch (VersionResolutionException e) {
223                 result.addException(e);
224                 throw new ArtifactDescriptorException(result);
225             }
226 
227             if (!visited.add(a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion())) {
228                 RepositoryException exception =
229                         new RepositoryException("Artifact relocations form a cycle: " + visited);
230                 invalidDescriptor(session, trace, a, exception);
231                 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
232                     return null;
233                 }
234                 result.addException(exception);
235                 throw new ArtifactDescriptorException(result);
236             }
237 
238             ArtifactResult resolveResult;
239             try {
240                 ArtifactRequest resolveRequest =
241                         new ArtifactRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
242                 resolveRequest.setTrace(trace);
243                 resolveResult = artifactResolver.resolveArtifact(session, resolveRequest);
244                 pomArtifact = resolveResult.getArtifact();
245                 result.setRepository(resolveResult.getRepository());
246             } catch (ArtifactResolutionException e) {
247                 if (e.getCause() instanceof ArtifactNotFoundException) {
248                     missingDescriptor(session, trace, a, (Exception) e.getCause());
249                     if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_MISSING) != 0) {
250                         return null;
251                     }
252                 }
253                 result.addException(e);
254                 throw new ArtifactDescriptorException(result);
255             }
256 
257             Model model;
258 
259             // TODO hack: don't rebuild model if it was already loaded during reactor resolution
260             final WorkspaceReader workspace = session.getWorkspaceReader();
261             if (workspace instanceof MavenWorkspaceReader) {
262                 model = ((MavenWorkspaceReader) workspace).findModel(pomArtifact);
263                 if (model != null) {
264                     return model;
265                 }
266             }
267 
268             try {
269                 ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
270                 modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
271                 modelRequest.setProcessPlugins(false);
272                 modelRequest.setTwoPhaseBuilding(false);
273                 // This merge is on purpose because otherwise user properties would override model
274                 // properties in dependencies the user does not know. See MNG-7563 for details.
275                 modelRequest.setSystemProperties(
276                         toProperties(session.getUserProperties(), session.getSystemProperties()));
277                 modelRequest.setUserProperties(new Properties());
278                 modelRequest.setModelCache(modelCacheFactory.createCache(session));
279                 modelRequest.setModelResolver(new DefaultModelResolver(
280                         session,
281                         trace.newChild(modelRequest),
282                         request.getRequestContext(),
283                         artifactResolver,
284                         versionRangeResolver,
285                         remoteRepositoryManager,
286                         request.getRepositories()));
287                 if (resolveResult.getRepository() instanceof WorkspaceRepository) {
288                     modelRequest.setPomFile(pomArtifact.getFile());
289                 } else {
290                     modelRequest.setModelSource(new FileModelSource(pomArtifact.getFile()));
291                 }
292 
293                 ModelBuildingResult modelResult = modelBuilder.build(modelRequest);
294                 // ModelBuildingEx is thrown only on FATAL and ERROR severities, but we still can have WARNs
295                 // that may lead to unexpected build failure, log them
296                 if (!modelResult.getProblems().isEmpty()) {
297                     List<ModelProblem> problems = modelResult.getProblems();
298                     if (logger.isDebugEnabled()) {
299                         String problem = (problems.size() == 1) ? "problem" : "problems";
300                         String problemPredicate = problem + ((problems.size() == 1) ? " was" : " were");
301                         String message = String.format(
302                                 "%s %s encountered while building the effective model for %s during %s\n",
303                                 problems.size(),
304                                 problemPredicate,
305                                 request.getArtifact(),
306                                 RequestTraceHelper.interpretTrace(true, request.getTrace()));
307                         message += StringUtils.capitalizeFirstLetter(problem);
308                         for (ModelProblem modelProblem : problems) {
309                             message += String.format(
310                                     "\n* %s @ %s",
311                                     modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null));
312                         }
313                         logger.warn(message);
314                     } else {
315                         logger.warn(
316                                 "{} {} encountered while building the effective model for {} during {} (use -X to see details)",
317                                 problems.size(),
318                                 (problems.size() == 1) ? "problem was" : "problems were",
319                                 request.getArtifact(),
320                                 RequestTraceHelper.interpretTrace(false, request.getTrace()));
321                     }
322                 }
323                 model = modelResult.getEffectiveModel();
324             } catch (ModelBuildingException e) {
325                 for (ModelProblem problem : e.getProblems()) {
326                     if (problem.getException() instanceof UnresolvableModelException) {
327                         result.addException(problem.getException());
328                         throw new ArtifactDescriptorException(result);
329                     }
330                 }
331                 invalidDescriptor(session, trace, a, e);
332                 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
333                     return null;
334                 }
335                 result.addException(e);
336                 throw new ArtifactDescriptorException(result);
337             }
338 
339             Relocation relocation = getRelocation(model);
340 
341             if (relocation != null) {
342                 result.addRelocation(a);
343                 a = new RelocatedArtifact(
344                         a,
345                         relocation.getGroupId(),
346                         relocation.getArtifactId(),
347                         relocation.getVersion(),
348                         relocation.getMessage());
349                 result.setArtifact(a);
350             } else {
351                 return model;
352             }
353         }
354     }
355 
356     private Properties toProperties(Map<String, String> dominant, Map<String, String> recessive) {
357         Properties props = new Properties();
358         if (recessive != null) {
359             props.putAll(recessive);
360         }
361         if (dominant != null) {
362             props.putAll(dominant);
363         }
364         return props;
365     }
366 
367     private Relocation getRelocation(Model model) {
368         Relocation relocation = null;
369         DistributionManagement distMgmt = model.getDistributionManagement();
370         if (distMgmt != null) {
371             relocation = distMgmt.getRelocation();
372         }
373         return relocation;
374     }
375 
376     private void missingDescriptor(
377             RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
378         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_MISSING);
379         event.setTrace(trace);
380         event.setArtifact(artifact);
381         event.setException(exception);
382 
383         repositoryEventDispatcher.dispatch(event.build());
384     }
385 
386     private void invalidDescriptor(
387             RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
388         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_INVALID);
389         event.setTrace(trace);
390         event.setArtifact(artifact);
391         event.setException(exception);
392 
393         repositoryEventDispatcher.dispatch(event.build());
394     }
395 
396     private int getPolicy(RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request) {
397         ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
398         if (policy == null) {
399             return ArtifactDescriptorPolicy.STRICT;
400         }
401         return policy.getPolicy(session, new ArtifactDescriptorPolicyRequest(a, request.getRequestContext()));
402     }
403 }