1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.api.services;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.function.Predicate;
27
28 import org.apache.maven.api.Artifact;
29 import org.apache.maven.api.DependencyCoordinates;
30 import org.apache.maven.api.JavaPathType;
31 import org.apache.maven.api.PathScope;
32 import org.apache.maven.api.PathType;
33 import org.apache.maven.api.Project;
34 import org.apache.maven.api.RemoteRepository;
35 import org.apache.maven.api.Session;
36 import org.apache.maven.api.annotations.Experimental;
37 import org.apache.maven.api.annotations.Immutable;
38 import org.apache.maven.api.annotations.Nonnull;
39 import org.apache.maven.api.annotations.NotThreadSafe;
40 import org.apache.maven.api.annotations.Nullable;
41
42
43
44
45
46
47
48
49
50
51 @Experimental
52 @Immutable
53 public interface DependencyResolverRequest {
54
55 enum RequestType {
56 COLLECT,
57 FLATTEN,
58 RESOLVE
59 }
60
61 @Nonnull
62 Session getSession();
63
64 @Nonnull
65 RequestType getRequestType();
66
67 @Nonnull
68 Optional<Project> getProject();
69
70 @Nonnull
71 Optional<Artifact> getRootArtifact();
72
73 @Nonnull
74 Optional<DependencyCoordinates> getRoot();
75
76 @Nonnull
77 Collection<DependencyCoordinates> getDependencies();
78
79 @Nonnull
80 Collection<DependencyCoordinates> getManagedDependencies();
81
82 boolean getVerbose();
83
84 @Nonnull
85 PathScope getPathScope();
86
87
88
89
90
91
92
93
94
95 @Nullable
96 Predicate<PathType> getPathTypeFilter();
97
98 @Nullable
99 List<RemoteRepository> getRepositories();
100
101 @Nonnull
102 static DependencyResolverRequestBuilder builder() {
103 return new DependencyResolverRequestBuilder();
104 }
105
106 @Nonnull
107 static DependencyResolverRequest build(Session session, RequestType requestType, Artifact rootArtifact) {
108 return build(session, requestType, rootArtifact, PathScope.MAIN_RUNTIME);
109 }
110
111 @Nonnull
112 static DependencyResolverRequest build(
113 Session session, RequestType requestType, Artifact rootArtifact, PathScope scope) {
114 return new DependencyResolverRequestBuilder()
115 .session(session)
116 .requestType(requestType)
117 .rootArtifact(rootArtifact)
118 .pathScope(scope)
119 .build();
120 }
121
122 @Nonnull
123 static DependencyResolverRequest build(Session session, RequestType requestType, Project project) {
124 return build(session, requestType, project, PathScope.MAIN_RUNTIME);
125 }
126
127 @Nonnull
128 static DependencyResolverRequest build(Session session, RequestType requestType, Project project, PathScope scope) {
129 return new DependencyResolverRequestBuilder()
130 .session(session)
131 .requestType(requestType)
132 .project(project)
133 .pathScope(scope)
134 .build();
135 }
136
137 @Nonnull
138 static DependencyResolverRequest build(Session session, RequestType requestType, DependencyCoordinates dependency) {
139 return build(session, requestType, dependency, PathScope.MAIN_RUNTIME);
140 }
141
142 @Nonnull
143 static DependencyResolverRequest build(
144 Session session, RequestType requestType, DependencyCoordinates dependency, PathScope scope) {
145 return new DependencyResolverRequestBuilder()
146 .session(session)
147 .requestType(requestType)
148 .dependency(dependency)
149 .pathScope(scope)
150 .build();
151 }
152
153 @Nonnull
154 static DependencyResolverRequest build(
155 Session session, RequestType requestType, List<DependencyCoordinates> dependencies) {
156 return build(session, requestType, dependencies, PathScope.MAIN_RUNTIME);
157 }
158
159 @Nonnull
160 static DependencyResolverRequest build(
161 Session session, RequestType requestType, List<DependencyCoordinates> dependencies, PathScope scope) {
162 return new DependencyResolverRequestBuilder()
163 .session(session)
164 .requestType(requestType)
165 .dependencies(dependencies)
166 .pathScope(scope)
167 .build();
168 }
169
170 @NotThreadSafe
171 class DependencyResolverRequestBuilder {
172
173 Session session;
174 RequestType requestType;
175 Project project;
176 Artifact rootArtifact;
177 DependencyCoordinates root;
178 List<DependencyCoordinates> dependencies = Collections.emptyList();
179 List<DependencyCoordinates> managedDependencies = Collections.emptyList();
180 boolean verbose;
181 PathScope pathScope;
182 Predicate<PathType> pathTypeFilter;
183 List<RemoteRepository> repositories;
184
185 DependencyResolverRequestBuilder() {}
186
187 @Nonnull
188 public DependencyResolverRequestBuilder session(@Nonnull Session session) {
189 this.session = session;
190 return this;
191 }
192
193 @Nonnull
194 public DependencyResolverRequestBuilder requestType(@Nonnull RequestType requestType) {
195 this.requestType = requestType;
196 return this;
197 }
198
199 @Nonnull
200 public DependencyResolverRequestBuilder project(@Nullable Project project) {
201 this.project = project;
202 return this;
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216 @Nonnull
217 public DependencyResolverRequestBuilder rootArtifact(@Nullable Artifact rootArtifact) {
218 this.rootArtifact = rootArtifact;
219 return this;
220 }
221
222
223
224
225
226 @Nonnull
227 public DependencyResolverRequestBuilder root(@Nonnull DependencyCoordinates root) {
228 this.root = root;
229 return this;
230 }
231
232
233
234
235
236
237
238
239
240 @Nonnull
241 public DependencyResolverRequestBuilder dependencies(@Nullable List<DependencyCoordinates> dependencies) {
242 this.dependencies = (dependencies != null) ? dependencies : Collections.emptyList();
243 return this;
244 }
245
246
247
248
249
250
251
252 @Nonnull
253 public DependencyResolverRequestBuilder dependency(@Nullable DependencyCoordinates dependency) {
254 if (dependency != null) {
255 if (this.dependencies.isEmpty()) {
256 this.dependencies = new ArrayList<>();
257 }
258 this.dependencies.add(dependency);
259 }
260 return this;
261 }
262
263
264
265
266
267
268
269
270
271 @Nonnull
272 public DependencyResolverRequestBuilder managedDependencies(
273 @Nullable List<DependencyCoordinates> managedDependencies) {
274 this.managedDependencies = (managedDependencies != null) ? managedDependencies : Collections.emptyList();
275 return this;
276 }
277
278
279
280
281
282
283
284
285 @Nonnull
286 public DependencyResolverRequestBuilder managedDependency(@Nullable DependencyCoordinates managedDependency) {
287 if (managedDependency != null) {
288 if (this.managedDependencies.isEmpty()) {
289 this.managedDependencies = new ArrayList<>();
290 }
291 this.managedDependencies.add(managedDependency);
292 }
293 return this;
294 }
295
296
297
298
299
300
301
302 @Nonnull
303 public DependencyResolverRequestBuilder verbose(boolean verbose) {
304 this.verbose = verbose;
305 return this;
306 }
307
308 @Nonnull
309 public DependencyResolverRequestBuilder pathScope(@Nullable PathScope pathScope) {
310 this.pathScope = pathScope;
311 return this;
312 }
313
314
315
316
317
318
319
320
321
322
323 @Nonnull
324 public DependencyResolverRequestBuilder pathTypeFilter(@Nonnull Predicate<PathType> pathTypeFilter) {
325 this.pathTypeFilter = pathTypeFilter;
326 return this;
327 }
328
329
330
331
332
333
334
335
336 @Nonnull
337 public DependencyResolverRequestBuilder pathTypeFilter(@Nonnull Collection<? extends PathType> desiredTypes) {
338 return pathTypeFilter(desiredTypes::contains);
339 }
340
341 @Nonnull
342 public DependencyResolverRequestBuilder repositories(@Nonnull List<RemoteRepository> repositories) {
343 this.repositories = repositories;
344 return this;
345 }
346
347 @Nonnull
348 public DependencyResolverRequest build() {
349 return new DefaultDependencyResolverRequest(
350 session,
351 requestType,
352 project,
353 rootArtifact,
354 root,
355 dependencies,
356 managedDependencies,
357 verbose,
358 pathScope,
359 pathTypeFilter,
360 repositories);
361 }
362
363 static class DefaultDependencyResolverRequest extends BaseRequest<Session>
364 implements DependencyResolverRequest {
365 private final RequestType requestType;
366 private final Project project;
367 private final Artifact rootArtifact;
368 private final DependencyCoordinates root;
369 private final Collection<DependencyCoordinates> dependencies;
370 private final Collection<DependencyCoordinates> managedDependencies;
371 private final boolean verbose;
372 private final PathScope pathScope;
373 private final Predicate<PathType> pathTypeFilter;
374 private final List<RemoteRepository> repositories;
375
376
377
378
379
380
381
382
383 @SuppressWarnings("checkstyle:ParameterNumber")
384 DefaultDependencyResolverRequest(
385 @Nonnull Session session,
386 @Nonnull RequestType requestType,
387 @Nullable Project project,
388 @Nullable Artifact rootArtifact,
389 @Nullable DependencyCoordinates root,
390 @Nonnull Collection<DependencyCoordinates> dependencies,
391 @Nonnull Collection<DependencyCoordinates> managedDependencies,
392 boolean verbose,
393 @Nullable PathScope pathScope,
394 @Nullable Predicate<PathType> pathTypeFilter,
395 @Nullable List<RemoteRepository> repositories) {
396 super(session);
397 this.requestType = nonNull(requestType, "requestType cannot be null");
398 this.project = project;
399 this.rootArtifact = rootArtifact;
400 this.root = root;
401 this.dependencies = unmodifiable(nonNull(dependencies, "dependencies cannot be null"));
402 this.managedDependencies =
403 unmodifiable(nonNull(managedDependencies, "managedDependencies cannot be null"));
404 this.verbose = verbose;
405 this.pathScope = nonNull(pathScope, "pathScope cannot be null");
406 this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true;
407 this.repositories = repositories;
408 if (verbose && requestType != RequestType.COLLECT) {
409 throw new IllegalArgumentException("verbose cannot only be true when collecting dependencies");
410 }
411 }
412
413 @Nonnull
414 @Override
415 public RequestType getRequestType() {
416 return requestType;
417 }
418
419 @Nonnull
420 @Override
421 public Optional<Project> getProject() {
422 return Optional.ofNullable(project);
423 }
424
425 @Nonnull
426 @Override
427 public Optional<Artifact> getRootArtifact() {
428 return Optional.ofNullable(rootArtifact);
429 }
430
431 @Nonnull
432 @Override
433 public Optional<DependencyCoordinates> getRoot() {
434 return Optional.ofNullable(root);
435 }
436
437 @Nonnull
438 @Override
439 public Collection<DependencyCoordinates> getDependencies() {
440 return dependencies;
441 }
442
443 @Nonnull
444 @Override
445 public Collection<DependencyCoordinates> getManagedDependencies() {
446 return managedDependencies;
447 }
448
449 @Override
450 public boolean getVerbose() {
451 return verbose;
452 }
453
454 @Override
455 public PathScope getPathScope() {
456 return pathScope;
457 }
458
459 @Override
460 public Predicate<PathType> getPathTypeFilter() {
461 return pathTypeFilter;
462 }
463
464 @Override
465 public List<RemoteRepository> getRepositories() {
466 return repositories;
467 }
468
469 @Nonnull
470 @Override
471 public String toString() {
472 return getRoot() + " -> " + getDependencies();
473 }
474 }
475 }
476 }