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.io.IOException;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.List;
26  import java.util.function.Consumer;
27  import java.util.stream.Collectors;
28  
29  import org.apache.maven.api.ArtifactCoordinates;
30  import org.apache.maven.api.DownloadedArtifact;
31  import org.apache.maven.api.Session;
32  import org.apache.maven.api.Version;
33  import org.apache.maven.api.di.Named;
34  import org.apache.maven.api.di.Singleton;
35  import org.apache.maven.api.services.ArtifactResolverException;
36  import org.apache.maven.api.services.ModelResolver;
37  import org.apache.maven.api.services.ModelResolverException;
38  import org.apache.maven.api.services.ModelSource;
39  import org.apache.maven.api.services.Source;
40  import org.apache.maven.api.services.VersionRangeResolverException;
41  
42  /**
43   * A model resolver to assist building of dependency POMs.
44   *
45   * @see DefaultArtifactDescriptorReader
46   */
47  @Named
48  @Singleton
49  public class DefaultModelResolver implements ModelResolver {
50  
51      @Override
52      public ModelSource resolveModel(
53              Session session, String groupId, String artifactId, String version, Consumer<String> resolvedVersion)
54              throws ModelResolverException {
55          try {
56              ArtifactCoordinates coords = session.createArtifactCoordinates(groupId, artifactId, version, "pom");
57              if (coords.getVersionConstraint().getVersionRange() != null
58                      && coords.getVersionConstraint().getVersionRange().getUpperBoundary() == null) {
59                  // Message below is checked for in the MNG-2199 core IT.
60                  throw new ModelResolverException(
61                          String.format("The requested version range '%s' does not specify an upper bound", version),
62                          groupId,
63                          artifactId,
64                          version);
65              }
66              List<Version> versions = session.resolveVersionRange(coords);
67              if (versions.isEmpty()) {
68                  throw new ModelResolverException(
69                          String.format("No versions matched the requested version range '%s'", version),
70                          groupId,
71                          artifactId,
72                          version);
73              }
74              String newVersion = versions.get(versions.size() - 1).asString();
75              if (!version.equals(newVersion)) {
76                  resolvedVersion.accept(newVersion);
77              }
78  
79              DownloadedArtifact resolved =
80                      session.resolveArtifact(session.createArtifactCoordinates(groupId, artifactId, newVersion, "pom"));
81              Path path = resolved.getPath();
82              String location = groupId + ":" + artifactId + ":" + newVersion;
83              return new ModelSource() {
84                  @Override
85                  public ModelSource resolve(ModelLocator modelLocator, String relative) {
86                      return null;
87                  }
88  
89                  @Override
90                  public Path getPath() {
91                      return null;
92                  }
93  
94                  @Override
95                  public InputStream openStream() throws IOException {
96                      return Files.newInputStream(path);
97                  }
98  
99                  @Override
100                 public String getLocation() {
101                     return location;
102                 }
103 
104                 @Override
105                 public Source resolve(String relative) {
106                     return null;
107                 }
108             };
109         } catch (VersionRangeResolverException | ArtifactResolverException e) {
110             throw new ModelResolverException(
111                     e.getMessage() + " (remote repositories: "
112                             + session.getRemoteRepositories().stream()
113                                     .map(Object::toString)
114                                     .collect(Collectors.joining(", "))
115                             + ")",
116                     groupId,
117                     artifactId,
118                     version,
119                     e);
120         }
121     }
122 }