/
DependencyCollectorRequest.java
310 lines (273 loc) · 11.7 KB
/
DependencyCollectorRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.apache.maven.api.Artifact;
import org.apache.maven.api.DependencyCoordinate;
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Immutable;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;
import static org.apache.maven.api.services.BaseRequest.nonNull;
/**
* A request to collect the transitive dependencies and to build a dependency graph from them. There are three ways to
* create a dependency graph. First, only the root dependency can be given. Second, a root dependency and direct
* dependencies can be specified in which case the specified direct dependencies are merged with the direct dependencies
* retrieved from the artifact descriptor of the root dependency. And last, only direct dependencies can be specified in
* which case the root node of the resulting graph has no associated dependency.
*
* @since 4.0.0
* @see DependencyCollector#collect(DependencyCollectorRequest)
*/
@Experimental
@Immutable
public interface DependencyCollectorRequest {
@Nonnull
Session getSession();
Optional<Project> getProject();
@Nonnull
Optional<Artifact> getRootArtifact();
@Nonnull
Optional<DependencyCoordinate> getRoot();
@Nonnull
Collection<DependencyCoordinate> getDependencies();
@Nonnull
Collection<DependencyCoordinate> getManagedDependencies();
boolean getVerbose();
@Nonnull
static DependencyCollectorRequest build(@Nonnull Session session, Artifact root) {
return builder()
.session(nonNull(session, "session cannot be null"))
.rootArtifact(nonNull(root, "root cannot be null"))
.build();
}
@Nonnull
static DependencyCollectorRequest build(@Nonnull Session session, @Nonnull DependencyCoordinate root) {
return builder()
.session(nonNull(session, "session cannot be null"))
.root(nonNull(root, "root cannot be null"))
.build();
}
@Nonnull
static DependencyCollectorRequest build(@Nonnull Session session, @Nonnull Project project) {
return builder()
.session(nonNull(session, "session cannot be null"))
.project(nonNull(project, "project cannot be null"))
.build();
}
@Nonnull
static DependencyCollectorRequestBuilder builder() {
return new DependencyCollectorRequestBuilder();
}
@NotThreadSafe
class DependencyCollectorRequestBuilder {
Session session;
Project project;
Artifact rootArtifact;
DependencyCoordinate root;
List<DependencyCoordinate> dependencies = Collections.emptyList();
List<DependencyCoordinate> managedDependencies = Collections.emptyList();
boolean verbose;
DependencyCollectorRequestBuilder() {}
@Nonnull
public DependencyCollectorRequestBuilder session(@Nonnull Session session) {
this.session = session;
return this;
}
@Nonnull
public DependencyCollectorRequestBuilder project(@Nullable Project project) {
this.project = project;
return this;
}
/**
* Sets the root artifact for the dependency graph.
* This must not be confused with {@link #root(DependencyCoordinate)}: The root <em>dependency</em>, like any
* other specified dependency, will be subject to dependency collection/resolution, i.e. should have an artifact
* descriptor and a corresponding artifact file. The root <em>artifact</em> on the other hand is only used
* as a label for the root node of the graph in case no root dependency was specified. As such, the configured
* root artifact is ignored if {@link #root(DependencyCoordinate)} has been set.
*
* @param rootArtifact the root artifact for the dependency graph, may be {@code null}
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder rootArtifact(@Nullable Artifact rootArtifact) {
this.rootArtifact = rootArtifact;
return this;
}
/**
* @param root The root dependency
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder root(@Nonnull DependencyCoordinate root) {
this.root = root;
return this;
}
/**
* Sets the direct dependencies. If both a root dependency and direct dependencies are given in the request, the
* direct dependencies from the request will be merged with the direct dependencies from the root dependency's
* artifact descriptor, giving higher priority to the dependencies from the request.
*
* @param dependencies the direct dependencies, may be {@code null}
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder dependencies(@Nullable List<DependencyCoordinate> dependencies) {
this.dependencies = (dependencies != null) ? dependencies : Collections.emptyList();
return this;
}
/**
* Adds the specified direct dependency.
*
* @param dependency the dependency to add, may be {@code null}
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder dependency(@Nullable DependencyCoordinate dependency) {
if (dependency != null) {
if (this.dependencies.isEmpty()) {
this.dependencies = new ArrayList<>();
}
this.dependencies.add(dependency);
}
return this;
}
/**
* Sets the dependency management to apply to transitive dependencies. To clarify, this management does not
* apply to
* the direct dependencies of the root node.
*
* @param managedDependencies the dependency management, may be {@code null}
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder managedDependencies(
@Nullable List<DependencyCoordinate> managedDependencies) {
this.managedDependencies = (managedDependencies != null) ? managedDependencies : Collections.emptyList();
return this;
}
/**
* Adds the specified managed dependency.
*
* @param managedDependency The managed dependency to add, may be {@code null} in which case the call
* will have no effect.
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder managedDependency(@Nullable DependencyCoordinate managedDependency) {
if (managedDependency != null) {
if (this.managedDependencies.isEmpty()) {
this.managedDependencies = new ArrayList<>();
}
this.managedDependencies.add(managedDependency);
}
return this;
}
/**
* Specifies that the collection should be verbose.
*
* @param verbose whether the collection should be verbose or not
* @return this request for chaining, never {@code null}
*/
@Nonnull
public DependencyCollectorRequestBuilder verbose(boolean verbose) {
this.verbose = verbose;
return this;
}
@Nonnull
public DependencyCollectorRequest build() {
return new DefaultDependencyCollectorRequest(
session, project, rootArtifact, root, dependencies, managedDependencies, verbose);
}
static class DefaultDependencyCollectorRequest extends BaseRequest implements DependencyCollectorRequest {
private final Project project;
private final Artifact rootArtifact;
private final DependencyCoordinate root;
private final Collection<DependencyCoordinate> dependencies;
private final Collection<DependencyCoordinate> managedDependencies;
private final boolean verbose;
/**
* Creates a request with the specified properties.
*
* @param session {@link Session}
* @param rootArtifact The root dependency whose transitive dependencies should be collected, may be {@code
* null}.
*/
DefaultDependencyCollectorRequest(
@Nonnull Session session,
@Nullable Project project,
@Nullable Artifact rootArtifact,
@Nullable DependencyCoordinate root,
@Nonnull Collection<DependencyCoordinate> dependencies,
@Nonnull Collection<DependencyCoordinate> managedDependencies,
boolean verbose) {
super(session);
this.project = project;
this.rootArtifact = rootArtifact;
this.root = root;
this.dependencies = unmodifiable(nonNull(dependencies, "dependencies cannot be null"));
this.managedDependencies =
unmodifiable(nonNull(managedDependencies, "managedDependencies cannot be null"));
this.verbose = verbose;
}
@Nonnull
@Override
public Optional<Project> getProject() {
return Optional.ofNullable(project);
}
@Nonnull
@Override
public Optional<Artifact> getRootArtifact() {
return Optional.ofNullable(rootArtifact);
}
@Nonnull
@Override
public Optional<DependencyCoordinate> getRoot() {
return Optional.ofNullable(root);
}
@Nonnull
@Override
public Collection<DependencyCoordinate> getDependencies() {
return dependencies;
}
@Nonnull
@Override
public Collection<DependencyCoordinate> getManagedDependencies() {
return managedDependencies;
}
@Override
public boolean getVerbose() {
return verbose;
}
@Nonnull
@Override
public String toString() {
return getRoot() + " -> " + getDependencies();
}
}
}
}