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