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.api.services.model;
20
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.concurrent.atomic.AtomicReference;
24
25 import org.apache.maven.api.RemoteRepository;
26 import org.apache.maven.api.Service;
27 import org.apache.maven.api.Session;
28 import org.apache.maven.api.annotations.Nonnull;
29 import org.apache.maven.api.annotations.Nullable;
30 import org.apache.maven.api.model.Dependency;
31 import org.apache.maven.api.model.Parent;
32 import org.apache.maven.api.services.ModelSource;
33 import org.apache.maven.api.services.Request;
34 import org.apache.maven.api.services.RequestTrace;
35 import org.apache.maven.api.services.Result;
36
37 /**
38 * Resolves a POM from its coordinates.
39 */
40 public interface ModelResolver extends Service {
41
42 /**
43 * Tries to resolve the POM for the specified parent coordinates possibly updating {@code parent}.
44 *
45 * @param session The session to use to resolve the model, must not be {@code null}.
46 * @param repositories The repositories to use to resolve the model, may be {@code null} in which case the {@code Session} repositories will be used.
47 * @param parent The parent coordinates to resolve, must not be {@code null}.
48 * @param modified a holder for the updated parent, must not be {@code null}.
49 * @return The source of the requested POM, never {@code null}.
50 * @throws ModelResolverException If the POM could not be resolved from any configured repository.
51 */
52 @Nonnull
53 ModelSource resolveModel(
54 @Nonnull Session session,
55 @Nullable List<RemoteRepository> repositories,
56 @Nonnull Parent parent,
57 @Nonnull AtomicReference<Parent> modified)
58 throws ModelResolverException;
59
60 /**
61 * Tries to resolve the POM for the specified dependency coordinates possibly updating {@code dependency}.
62 *
63 * @param session The session to use to resolve the model, must not be {@code null}.
64 * @param repositories The repositories to use to resolve the model, may be {@code null} in which case the {@code Session} repositories will be used.
65 * @param dependency The dependency coordinates to resolve, must not be {@code null}.
66 * @param modified a holder for the updated dependency, must not be {@code null}.
67 * @return The source of the requested POM, never {@code null}.
68 * @throws ModelResolverException If the POM could not be resolved from any configured repository.
69 */
70 @Nonnull
71 ModelSource resolveModel(
72 @Nonnull Session session,
73 @Nullable List<RemoteRepository> repositories,
74 @Nonnull Dependency dependency,
75 @Nonnull AtomicReference<Dependency> modified)
76 throws ModelResolverException;
77
78 @Nonnull
79 ModelResolverResult resolveModel(@Nonnull ModelResolverRequest request) throws ModelResolverException;
80
81 record ModelResolverRequest(
82 @Nonnull Session session,
83 @Nullable RequestTrace trace,
84 @Nullable List<RemoteRepository> repositories,
85 @Nonnull String groupId,
86 @Nonnull String artifactId,
87 @Nonnull String version,
88 @Nullable String classifier)
89 implements Request<Session> {
90 @Nonnull
91 @Override
92 public Session getSession() {
93 return session;
94 }
95
96 @Nullable
97 @Override
98 public RequestTrace getTrace() {
99 return trace;
100 }
101
102 @Override
103 public boolean equals(Object o) {
104 return o instanceof ModelResolverRequest that
105 && repositories == that.repositories
106 && Objects.equals(groupId, that.groupId)
107 && Objects.equals(artifactId, that.artifactId)
108 && Objects.equals(version, that.version)
109 && Objects.equals(classifier, that.classifier);
110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hash(repositories, groupId, artifactId, version, classifier);
115 }
116
117 @Override
118 @Nonnull
119 public String toString() {
120 return getClass().getSimpleName() + "[" + "repositories="
121 + repositories + ", groupId="
122 + groupId
123 + ", artifactId=" + artifactId
124 + ", version=" + version
125 + ", classifier=" + classifier
126 + ']';
127 }
128 }
129
130 record ModelResolverResult(ModelResolverRequest request, ModelSource source, String version)
131 implements Result<ModelResolverRequest> {
132 @Nonnull
133 @Override
134 public ModelResolverRequest getRequest() {
135 return request;
136 }
137 }
138 }