1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
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
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 }