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.model.resolution;
20  
21  import java.util.concurrent.atomic.AtomicReference;
22  
23  import org.apache.maven.api.model.Dependency;
24  import org.apache.maven.api.model.Parent;
25  import org.apache.maven.api.model.Repository;
26  import org.apache.maven.model.building.ModelSource;
27  
28  /**
29   * Resolves a POM from its coordinates. During the build process, the
30   * {@link org.apache.maven.model.building.ModelBuilder} will add any relevant repositories to the model resolver. In
31   * other words, the model resolver is stateful and should not be reused across multiple model building requests.
32   *
33   */
34  public interface ModelResolver {
35  
36      /**
37       * Tries to resolve the POM for the specified coordinates.
38       *
39       * @param groupId The group identifier of the POM, must not be {@code null}.
40       * @param artifactId The artifact identifier of the POM, must not be {@code null}.
41       * @param version The version of the POM, must not be {@code null}.
42       * @return The source of the requested POM, never {@code null}.
43       * @throws UnresolvableModelException If the POM could not be resolved from any configured repository.
44       */
45      ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException;
46  
47      /**
48       * Tries to resolve the POM for the specified parent coordinates possibly updating {@code parent}.
49       * <p>
50       * Unlike the {@link #resolveModel(java.lang.String, java.lang.String, java.lang.String)} method, this method
51       * supports version ranges and updates the given {@code parent} instance to match the returned {@code ModelSource}.
52       * If {@code parent} declares a version range, the version corresponding to the returned {@code ModelSource} will
53       * be set on the given {@code parent}.
54       * </p>
55       *
56       * @param parent The parent coordinates to resolve, must not be {@code null}.
57       *
58       * @return The source of the requested POM, never {@code null}.
59       *
60       * @throws UnresolvableModelException If the POM could not be resolved from any configured repository.
61       * @since 3.2.2
62       *
63       * @see Parent#clone()
64       */
65      ModelSource resolveModel(org.apache.maven.model.Parent parent) throws UnresolvableModelException;
66  
67      /**
68       * Tries to resolve the POM for the specified dependency coordinates possibly updating {@code dependency}.
69       * <p>
70       * Unlike the {@link #resolveModel(java.lang.String, java.lang.String, java.lang.String)} method, this method
71       * supports version ranges and updates the given {@code dependency} instance to match the returned
72       * {@code ModelSource}. If {@code dependency} declares a version range, the version corresponding to the returned
73       * {@code ModelSource} will be set on the given {@code dependency}.
74       * </p>
75       *
76       * @param dependency The dependency coordinates to resolve, must not be {@code null}.
77       *
78       * @return The source of the requested POM, never {@code null}.
79       *
80       * @throws UnresolvableModelException If the POM could not be resolved from any configured repository.
81       * @since 3.5.0
82       *
83       * @see Dependency#clone()
84       */
85      ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException;
86  
87      /**
88       * Adds a repository to use for subsequent resolution requests. The order in which repositories are added matters,
89       * repositories that were added first should also be searched first. When multiple repositories with the same
90       * identifier are added, only the first repository being added will be used.
91       *
92       * @param repository The repository to add to the internal search chain, must not be {@code null}.
93       * @throws InvalidRepositoryException If the repository could not be added (e.g. due to invalid URL or layout).
94       */
95      void addRepository(org.apache.maven.model.Repository repository) throws InvalidRepositoryException;
96  
97      /**
98       * Adds a repository to use for subsequent resolution requests. The order in which repositories are added matters,
99       * repositories that were added first should also be searched first. When multiple repositories with the same
100      * identifier are added, then the value of the replace argument determines the behaviour.
101      *
102      * If replace is false then any existing repository with the same Id will remain in use. If replace
103      * is true the new repository replaces the original.
104      *
105      * @param repository The repository to add to the internal search chain, must not be {@code null}.
106      * @throws InvalidRepositoryException If the repository could not be added (e.g. due to invalid URL or layout).
107      */
108     void addRepository(org.apache.maven.model.Repository repository, boolean replace) throws InvalidRepositoryException;
109 
110     /**
111      * Clones this resolver for usage in a forked resolution process. In general, implementors need not provide a deep
112      * clone. The only requirement is that invocations of {@link #addRepository(Repository)} on the clone do not affect
113      * the state of the original resolver and vice versa.
114      *
115      * @return The cloned resolver, never {@code null}.
116      */
117     ModelResolver newCopy();
118 
119     default ModelSource resolveModel(Parent parent, AtomicReference<Parent> modified)
120             throws UnresolvableModelException {
121         org.apache.maven.model.Parent p = new org.apache.maven.model.Parent(parent);
122         ModelSource result = resolveModel(p);
123         if (p.getDelegate() != parent) {
124             modified.set(p.getDelegate());
125         }
126         return result;
127     }
128 
129     default ModelSource resolveModel(Dependency dependency, AtomicReference<Dependency> modified)
130             throws UnresolvableModelException {
131         org.apache.maven.model.Dependency d = new org.apache.maven.model.Dependency(dependency);
132         ModelSource result = resolveModel(d);
133         if (d.getDelegate() != dependency) {
134             modified.set(d.getDelegate());
135         }
136         return result;
137     }
138 
139     default void addRepository(Repository repository) throws InvalidRepositoryException {
140         addRepository(new org.apache.maven.model.Repository(repository));
141     }
142 
143     default void addRepository(Repository repository, boolean replace) throws InvalidRepositoryException {
144         addRepository(new org.apache.maven.model.Repository(repository), replace);
145     }
146 }