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.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
40
41
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
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 }