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;
20  
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.function.Predicate;
24  
25  import org.apache.maven.api.Artifact;
26  import org.apache.maven.api.DependencyCoordinate;
27  import org.apache.maven.api.JavaPathType;
28  import org.apache.maven.api.PathScope;
29  import org.apache.maven.api.PathType;
30  import org.apache.maven.api.Project;
31  import org.apache.maven.api.Session;
32  import org.apache.maven.api.annotations.Experimental;
33  import org.apache.maven.api.annotations.Nonnull;
34  import org.apache.maven.api.annotations.NotThreadSafe;
35  import org.apache.maven.api.annotations.Nullable;
36  
37  @Experimental
38  public interface DependencyResolverRequest extends DependencyCollectorRequest {
39  
40      @Nonnull
41      PathScope getPathScope();
42  
43      /**
44       * Returns a filter for the types of path (class-path, module-path, …) accepted by the tool.
45       * For example, if a Java tools accepts only class-path elements, then the filter should return
46       * {@code true} for {@link JavaPathType#CLASSES} and {@code false} for {@link JavaPathType#MODULES}.
47       * If no filter is explicitly set, then the default is a filter accepting everything.
48       *
49       * @return a filter for the types of path (class-path, module-path, …) accepted by the tool
50       */
51      Predicate<PathType> getPathTypeFilter();
52  
53      @Nonnull
54      static DependencyResolverRequestBuilder builder() {
55          return new DependencyResolverRequestBuilder();
56      }
57  
58      @Nonnull
59      static DependencyResolverRequest build(Session session, Project project) {
60          return build(session, project, PathScope.MAIN_RUNTIME);
61      }
62  
63      @Nonnull
64      static DependencyResolverRequest build(Session session, Project project, PathScope scope) {
65          return new DependencyResolverRequestBuilder()
66                  .session(session)
67                  .project(project)
68                  .pathScope(scope)
69                  .build();
70      }
71  
72      @Nonnull
73      static DependencyResolverRequest build(Session session, DependencyCoordinate dependency) {
74          return build(session, dependency, PathScope.MAIN_RUNTIME);
75      }
76  
77      @Nonnull
78      static DependencyResolverRequest build(Session session, DependencyCoordinate dependency, PathScope scope) {
79          return new DependencyResolverRequestBuilder()
80                  .session(session)
81                  .dependency(dependency)
82                  .pathScope(scope)
83                  .build();
84      }
85  
86      @Nonnull
87      static DependencyResolverRequest build(Session session, List<DependencyCoordinate> dependencies) {
88          return build(session, dependencies, PathScope.MAIN_RUNTIME);
89      }
90  
91      @Nonnull
92      static DependencyResolverRequest build(Session session, List<DependencyCoordinate> dependencies, PathScope scope) {
93          return new DependencyResolverRequestBuilder()
94                  .session(session)
95                  .dependencies(dependencies)
96                  .pathScope(scope)
97                  .build();
98      }
99  
100     @NotThreadSafe
101     class DependencyResolverRequestBuilder extends DependencyCollectorRequestBuilder {
102         PathScope pathScope;
103 
104         Predicate<PathType> pathTypeFilter;
105 
106         @Nonnull
107         @Override
108         public DependencyResolverRequestBuilder session(@Nonnull Session session) {
109             super.session(session);
110             return this;
111         }
112 
113         @Nonnull
114         @Override
115         public DependencyResolverRequestBuilder project(@Nullable Project project) {
116             super.project(project);
117             return this;
118         }
119 
120         @Nonnull
121         @Override
122         public DependencyResolverRequestBuilder rootArtifact(@Nullable Artifact rootArtifact) {
123             super.rootArtifact(rootArtifact);
124             return this;
125         }
126 
127         @Nonnull
128         @Override
129         public DependencyResolverRequestBuilder root(@Nullable DependencyCoordinate root) {
130             super.root(root);
131             return this;
132         }
133 
134         @Nonnull
135         @Override
136         public DependencyResolverRequestBuilder dependencies(@Nullable List<DependencyCoordinate> dependencies) {
137             super.dependencies(dependencies);
138             return this;
139         }
140 
141         @Nonnull
142         @Override
143         public DependencyResolverRequestBuilder dependency(@Nullable DependencyCoordinate dependency) {
144             super.dependency(dependency);
145             return this;
146         }
147 
148         @Nonnull
149         @Override
150         public DependencyResolverRequestBuilder managedDependencies(
151                 @Nullable List<DependencyCoordinate> managedDependencies) {
152             super.managedDependencies(managedDependencies);
153             return this;
154         }
155 
156         @Nonnull
157         @Override
158         public DependencyResolverRequestBuilder managedDependency(@Nullable DependencyCoordinate managedDependency) {
159             super.managedDependency(managedDependency);
160             return this;
161         }
162 
163         @Nonnull
164         @Override
165         public DependencyResolverRequestBuilder verbose(boolean verbose) {
166             super.verbose(verbose);
167             return this;
168         }
169 
170         @Nonnull
171         public DependencyResolverRequestBuilder pathScope(@Nonnull PathScope pathScope) {
172             this.pathScope = pathScope;
173             return this;
174         }
175 
176         /**
177          * Filters the types of paths to include in the result.
178          * The result will contain only the paths of types for which the predicate returned {@code true}.
179          * It is recommended to apply a filter for retaining only the types of paths of interest,
180          * because it can resolve ambiguities when a path could be of many types.
181          *
182          * @param pathTypeFilter predicate evaluating whether a path type should be included in the result
183          * @return {@code this} for method call chaining
184          */
185         @Nonnull
186         public DependencyResolverRequestBuilder pathTypeFilter(@Nonnull Predicate<PathType> pathTypeFilter) {
187             this.pathTypeFilter = pathTypeFilter;
188             return this;
189         }
190 
191         /**
192          * Specifies the type of paths to include in the result. This is a convenience method for
193          * {@link #pathTypeFilter(Predicate)} using {@link Collection#contains(Object)} as the filter.
194          *
195          * @param desiredTypes the type of paths to include in the result
196          * @return {@code this} for method call chaining
197          */
198         @Nonnull
199         public DependencyResolverRequestBuilder pathTypeFilter(@Nonnull Collection<PathType> desiredTypes) {
200             return pathTypeFilter(desiredTypes::contains);
201         }
202 
203         @Override
204         public DependencyResolverRequest build() {
205             return new DefaultDependencyResolverRequest(
206                     session,
207                     project,
208                     rootArtifact,
209                     root,
210                     dependencies,
211                     managedDependencies,
212                     verbose,
213                     pathScope,
214                     pathTypeFilter);
215         }
216 
217         static class DefaultDependencyResolverRequest extends DefaultDependencyCollectorRequest
218                 implements DependencyResolverRequest {
219             private final PathScope pathScope;
220 
221             private final Predicate<PathType> pathTypeFilter;
222 
223             DefaultDependencyResolverRequest(
224                     Session session,
225                     Project project,
226                     Artifact rootArtifact,
227                     DependencyCoordinate root,
228                     Collection<DependencyCoordinate> dependencies,
229                     Collection<DependencyCoordinate> managedDependencies,
230                     boolean verbose,
231                     PathScope pathScope,
232                     Predicate<PathType> pathTypeFilter) {
233                 super(session, project, rootArtifact, root, dependencies, managedDependencies, verbose);
234                 this.pathScope = nonNull(pathScope, "pathScope cannot be null");
235                 this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true;
236                 if (verbose) {
237                     throw new IllegalArgumentException("verbose cannot be true for resolving dependencies");
238                 }
239             }
240 
241             @Nonnull
242             @Override
243             public PathScope getPathScope() {
244                 return pathScope;
245             }
246 
247             @Override
248             public Predicate<PathType> getPathTypeFilter() {
249                 return pathTypeFilter;
250             }
251         }
252     }
253 }