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.nio.file.Path;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.function.Consumer;
25  import java.util.stream.Collectors;
26  
27  import org.apache.maven.api.ArtifactCoordinate;
28  import org.apache.maven.api.Session;
29  import org.apache.maven.api.Version;
30  import org.apache.maven.api.di.Named;
31  import org.apache.maven.api.di.Singleton;
32  import org.apache.maven.api.services.ArtifactResolverException;
33  import org.apache.maven.api.services.ModelResolver;
34  import org.apache.maven.api.services.ModelResolverException;
35  import org.apache.maven.api.services.ModelSource;
36  import org.apache.maven.api.services.VersionRangeResolverException;
37  
38  /**
39   * A model resolver to assist building of dependency POMs.
40   *
41   * @see DefaultArtifactDescriptorReader
42   */
43  @Named
44  @Singleton
45  public class DefaultModelResolver implements ModelResolver {
46  
47      @Override
48      public ModelSource resolveModel(
49              Session session, String groupId, String artifactId, String version, Consumer<String> resolvedVersion)
50              throws ModelResolverException {
51          try {
52              ArtifactCoordinate coord = session.createArtifactCoordinate(groupId, artifactId, version, "pom");
53              if (coord.getVersion().getVersionRange() != null
54                      && coord.getVersion().getVersionRange().getUpperBoundary() == null) {
55                  // Message below is checked for in the MNG-2199 core IT.
56                  throw new ModelResolverException(
57                          String.format("The requested version range '%s' does not specify an upper bound", version),
58                          groupId,
59                          artifactId,
60                          version);
61              }
62              List<Version> versions = session.resolveVersionRange(coord);
63              if (versions.isEmpty()) {
64                  throw new ModelResolverException(
65                          String.format("No versions matched the requested version range '%s'", version),
66                          groupId,
67                          artifactId,
68                          version);
69              }
70              String newVersion = versions.get(versions.size() - 1).asString();
71              if (!version.equals(newVersion)) {
72                  resolvedVersion.accept(newVersion);
73              }
74  
75              Map.Entry<org.apache.maven.api.Artifact, Path> resolved =
76                      session.resolveArtifact(session.createArtifactCoordinate(groupId, artifactId, newVersion, "pom"));
77              return ModelSource.fromPath(resolved.getValue(), groupId + ":" + artifactId + ":" + newVersion);
78          } catch (VersionRangeResolverException | ArtifactResolverException e) {
79              throw new ModelResolverException(
80                      e.getMessage() + " (remote repositories: "
81                              + session.getRemoteRepositories().stream()
82                                      .map(Object::toString)
83                                      .collect(Collectors.joining(", "))
84                              + ")",
85                      groupId,
86                      artifactId,
87                      version,
88                      e);
89          }
90      }
91  }