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.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.RemoteRepository;
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.ModelSource;
38  import org.apache.maven.api.services.model.ModelResolverException;
39  import org.apache.maven.internal.impl.InternalSession;
40  import org.apache.maven.internal.impl.model.ModelProblemUtils;
41  import org.eclipse.aether.RepositoryEvent;
42  import org.eclipse.aether.RepositoryEvent.EventType;
43  import org.eclipse.aether.RepositoryException;
44  import org.eclipse.aether.RepositorySystemSession;
45  import org.eclipse.aether.RequestTrace;
46  import org.eclipse.aether.artifact.Artifact;
47  import org.eclipse.aether.impl.ArtifactDescriptorReader;
48  import org.eclipse.aether.impl.ArtifactResolver;
49  import org.eclipse.aether.impl.RepositoryEventDispatcher;
50  import org.eclipse.aether.impl.VersionResolver;
51  import org.eclipse.aether.repository.WorkspaceReader;
52  import org.eclipse.aether.resolution.ArtifactDescriptorException;
53  import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
54  import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest;
55  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
56  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
57  import org.eclipse.aether.resolution.ArtifactRequest;
58  import org.eclipse.aether.resolution.ArtifactResolutionException;
59  import org.eclipse.aether.resolution.ArtifactResult;
60  import org.eclipse.aether.resolution.VersionRequest;
61  import org.eclipse.aether.resolution.VersionResolutionException;
62  import org.eclipse.aether.resolution.VersionResult;
63  import org.eclipse.aether.transfer.ArtifactNotFoundException;
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  /**
68   * Default artifact descriptor reader.
69   */
70  @Named
71  @Singleton
72  public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader {
73      private final VersionResolver versionResolver;
74      private final ArtifactResolver artifactResolver;
75      private final RepositoryEventDispatcher repositoryEventDispatcher;
76      private final ModelBuilder modelBuilder;
77      private final Map<String, MavenArtifactRelocationSource> artifactRelocationSources;
78      private final ArtifactDescriptorReaderDelegate delegate;
79      private final Logger logger = LoggerFactory.getLogger(getClass());
80  
81      @Inject
82      public DefaultArtifactDescriptorReader(
83              VersionResolver versionResolver,
84              ArtifactResolver artifactResolver,
85              ModelBuilder modelBuilder,
86              RepositoryEventDispatcher repositoryEventDispatcher,
87              Map<String, MavenArtifactRelocationSource> artifactRelocationSources) {
88          this.versionResolver = Objects.requireNonNull(versionResolver, "versionResolver cannot be null");
89          this.artifactResolver = Objects.requireNonNull(artifactResolver, "artifactResolver cannot be null");
90          this.modelBuilder = Objects.requireNonNull(modelBuilder, "modelBuilder cannot be null");
91          this.repositoryEventDispatcher =
92                  Objects.requireNonNull(repositoryEventDispatcher, "repositoryEventDispatcher cannot be null");
93          this.artifactRelocationSources =
94                  Objects.requireNonNull(artifactRelocationSources, "artifactRelocationSources cannot be null");
95          this.delegate = new ArtifactDescriptorReaderDelegate();
96      }
97  
98      @Override
99      public ArtifactDescriptorResult readArtifactDescriptor(
100             RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
101         ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
102 
103         Model model = loadPom(session, request, result);
104         if (model != null) {
105             Map<String, Object> config = session.getConfigProperties();
106             ArtifactDescriptorReaderDelegate delegate =
107                     (ArtifactDescriptorReaderDelegate) config.get(ArtifactDescriptorReaderDelegate.class.getName());
108 
109             if (delegate == null) {
110                 delegate = this.delegate;
111             }
112 
113             delegate.populateResult(InternalSession.from(session), result, model);
114         }
115 
116         return result;
117     }
118 
119     @SuppressWarnings("MethodLength")
120     private Model loadPom(
121             RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
122             throws ArtifactDescriptorException {
123         RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
124 
125         LinkedHashSet<String> visited = new LinkedHashSet<>();
126         for (Artifact a = request.getArtifact(); ; ) {
127             Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifactUnconditionally(a);
128             try {
129                 VersionRequest versionRequest =
130                         new VersionRequest(a, request.getRepositories(), request.getRequestContext());
131                 versionRequest.setTrace(trace);
132                 VersionResult versionResult = versionResolver.resolveVersion(session, versionRequest);
133 
134                 a = a.setVersion(versionResult.getVersion());
135 
136                 versionRequest =
137                         new VersionRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
138                 versionRequest.setTrace(trace);
139                 versionResult = versionResolver.resolveVersion(session, versionRequest);
140 
141                 pomArtifact = pomArtifact.setVersion(versionResult.getVersion());
142             } catch (VersionResolutionException e) {
143                 result.addException(e);
144                 throw new ArtifactDescriptorException(result);
145             }
146 
147             if (!visited.add(a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion())) {
148                 RepositoryException exception =
149                         new RepositoryException("Artifact relocations form a cycle: " + visited);
150                 invalidDescriptor(session, trace, a, exception);
151                 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
152                     return null;
153                 }
154                 result.addException(exception);
155                 throw new ArtifactDescriptorException(result);
156             }
157 
158             ArtifactResult resolveResult;
159             try {
160                 ArtifactRequest resolveRequest =
161                         new ArtifactRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
162                 resolveRequest.setTrace(trace);
163                 resolveResult = artifactResolver.resolveArtifact(session, resolveRequest);
164                 pomArtifact = resolveResult.getArtifact();
165                 result.setRepository(resolveResult.getRepository());
166             } catch (ArtifactResolutionException e) {
167                 if (e.getCause() instanceof ArtifactNotFoundException) {
168                     missingDescriptor(session, trace, a, (Exception) e.getCause());
169                     if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_MISSING) != 0) {
170                         return null;
171                     }
172                 }
173                 result.addException(e);
174                 throw new ArtifactDescriptorException(result);
175             }
176 
177             Model model;
178 
179             // TODO hack: don't rebuild model if it was already loaded during reactor resolution
180             final WorkspaceReader workspace = session.getWorkspaceReader();
181             if (workspace instanceof MavenWorkspaceReader) {
182                 model = ((MavenWorkspaceReader) workspace).findModel(pomArtifact);
183                 if (model != null) {
184                     return model;
185                 }
186             }
187 
188             try {
189                 InternalSession iSession = InternalSession.from(session);
190                 List<RemoteRepository> repositories = request.getRepositories().stream()
191                         .map(iSession::getRemoteRepository)
192                         .toList();
193                 String gav =
194                         pomArtifact.getGroupId() + ":" + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion();
195                 ModelBuilderRequest modelRequest = ModelBuilderRequest.builder()
196                         .session(iSession)
197                         .requestType(ModelBuilderRequest.RequestType.DEPENDENCY)
198                         .source(ModelSource.fromPath(pomArtifact.getPath(), gav))
199                         // This merge is on purpose because otherwise user properties would override model
200                         // properties in dependencies the user does not know. See MNG-7563 for details.
201                         .systemProperties(toProperties(session.getUserProperties(), session.getSystemProperties()))
202                         .userProperties(Map.of())
203                         .repositoryMerging(ModelBuilderRequest.RepositoryMerging.REQUEST_DOMINANT)
204                         .repositories(repositories)
205                         .build();
206 
207                 ModelBuilderResult modelResult = modelBuilder.newSession().build(modelRequest);
208                 // ModelBuildingEx is thrown only on FATAL and ERROR severities, but we still can have WARNs
209                 // that may lead to unexpected build failure, log them
210                 if (!modelResult.getProblems().isEmpty()) {
211                     List<ModelProblem> problems = modelResult.getProblems();
212                     if (logger.isDebugEnabled()) {
213                         StringBuilder sb = new StringBuilder();
214                         sb.append(problems.size())
215                                 .append(" ")
216                                 .append((problems.size() == 1) ? "problem was" : "problems were")
217                                 .append(" encountered while building the effective model for ")
218                                 .append(request.getArtifact())
219                                 .append(" during ")
220                                 .append(RequestTraceHelper.interpretTrace(true, request.getTrace()))
221                                 .append("\n")
222                                 .append((problems.size() == 1) ? "Problem" : "Problems");
223                         for (ModelProblem modelProblem : problems) {
224                             sb.append("\n* ")
225                                     .append(modelProblem.getMessage())
226                                     .append(" @ ")
227                                     .append(ModelProblemUtils.formatLocation(modelProblem, null));
228                         }
229                         logger.warn(sb.toString());
230                     } else {
231                         logger.warn(
232                                 "{} {} encountered while building the effective model for {} during {} (use -X to see details)",
233                                 problems.size(),
234                                 (problems.size() == 1) ? "problem was" : "problems were",
235                                 request.getArtifact(),
236                                 RequestTraceHelper.interpretTrace(false, request.getTrace()));
237                     }
238                 }
239                 model = modelResult.getEffectiveModel();
240             } catch (ModelBuilderException e) {
241                 for (ModelProblem problem : e.getResult().getProblems()) {
242                     if (problem.getException() instanceof ModelResolverException) {
243                         result.addException(problem.getException());
244                         throw new ArtifactDescriptorException(result);
245                     }
246                 }
247                 invalidDescriptor(session, trace, a, e);
248                 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
249                     return null;
250                 }
251                 result.addException(e);
252                 throw new ArtifactDescriptorException(result);
253             }
254 
255             Artifact relocatedArtifact = getRelocation(session, result, model);
256             if (relocatedArtifact != null) {
257                 if (withinSameGav(relocatedArtifact, a)) {
258                     result.setArtifact(relocatedArtifact);
259                     return model; // they share same model
260                 } else {
261                     result.addRelocation(a);
262                     a = relocatedArtifact;
263                     result.setArtifact(a);
264                 }
265             } else {
266                 return model;
267             }
268         }
269     }
270 
271     private boolean withinSameGav(Artifact a1, Artifact a2) {
272         return Objects.equals(a1.getGroupId(), a2.getGroupId())
273                 && Objects.equals(a1.getArtifactId(), a2.getArtifactId())
274                 && Objects.equals(a1.getVersion(), a2.getVersion());
275     }
276 
277     private Map<String, String> toProperties(Map<String, String> dominant, Map<String, String> recessive) {
278         Map<String, String> props = new HashMap<>();
279         if (recessive != null) {
280             props.putAll(recessive);
281         }
282         if (dominant != null) {
283             props.putAll(dominant);
284         }
285         return props;
286     }
287 
288     private Artifact getRelocation(
289             RepositorySystemSession session, ArtifactDescriptorResult artifactDescriptorResult, Model model)
290             throws ArtifactDescriptorException {
291         Artifact result = null;
292         for (MavenArtifactRelocationSource source : artifactRelocationSources.values()) {
293             result = source.relocatedTarget(session, artifactDescriptorResult, model);
294             if (result != null) {
295                 break;
296             }
297         }
298         return result;
299     }
300 
301     private void missingDescriptor(
302             RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
303         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_MISSING);
304         event.setTrace(trace);
305         event.setArtifact(artifact);
306         event.setException(exception);
307 
308         repositoryEventDispatcher.dispatch(event.build());
309     }
310 
311     private void invalidDescriptor(
312             RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
313         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_INVALID);
314         event.setTrace(trace);
315         event.setArtifact(artifact);
316         event.setException(exception);
317 
318         repositoryEventDispatcher.dispatch(event.build());
319     }
320 
321     private int getPolicy(RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request) {
322         ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
323         if (policy == null) {
324             return ArtifactDescriptorPolicy.STRICT;
325         }
326         return policy.getPolicy(session, new ArtifactDescriptorPolicyRequest(a, request.getRequestContext()));
327     }
328 }