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.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              @Nullable String extension)
90              implements Request<Session> {
91          public ModelResolverRequest {
92              Objects.requireNonNull(session, "session cannot be null");
93              Objects.requireNonNull(groupId, "groupId cannot be null");
94              Objects.requireNonNull(artifactId, "artifactId cannot be null");
95              Objects.requireNonNull(version, "version cannot be null");
96          }
97  
98          @Nonnull
99          @Override
100         public Session getSession() {
101             return session;
102         }
103 
104         @Nullable
105         @Override
106         public RequestTrace getTrace() {
107             return trace;
108         }
109 
110         @Override
111         public boolean equals(Object o) {
112             return o instanceof ModelResolverRequest that
113                     && repositories == that.repositories
114                     && Objects.equals(groupId, that.groupId)
115                     && Objects.equals(artifactId, that.artifactId)
116                     && Objects.equals(version, that.version)
117                     && Objects.equals(classifier, that.classifier)
118                     && Objects.equals(extension, that.extension);
119         }
120 
121         @Override
122         public int hashCode() {
123             return Objects.hash(repositories, groupId, artifactId, version, classifier, extension);
124         }
125 
126         @Override
127         @Nonnull
128         public String toString() {
129             return getClass().getSimpleName() + "[" + "repositories="
130                     + repositories + ", groupId="
131                     + groupId
132                     + ", artifactId=" + artifactId
133                     + ", version=" + version
134                     + ", classifier=" + classifier
135                     + ", extension=" + extension
136                     + ']';
137         }
138     }
139 
140     record ModelResolverResult(ModelResolverRequest request, ModelSource source, String version)
141             implements Result<ModelResolverRequest> {
142         @Nonnull
143         @Override
144         public ModelResolverRequest getRequest() {
145             return request;
146         }
147     }
148 }