1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.collection
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.List
;
17 import org
.eclipse
.aether
.RepositorySystem
;
18 import org
.eclipse
.aether
.RepositorySystemSession
;
19 import org
.eclipse
.aether
.RequestTrace
;
20 import org
.eclipse
.aether
.artifact
.Artifact
;
21 import org
.eclipse
.aether
.graph
.Dependency
;
22 import org
.eclipse
.aether
.repository
.RemoteRepository
;
25 * A request to collect the transitive dependencies and to build a dependency graph from them. There are three ways to
26 * create a dependency graph. First, only the root dependency can be given. Second, a root dependency and direct
27 * dependencies can be specified in which case the specified direct dependencies are merged with the direct dependencies
28 * retrieved from the artifact descriptor of the root dependency. And last, only direct dependencies can be specified in
29 * which case the root node of the resulting graph has no associated dependency.
31 * @see RepositorySystem#collectDependencies(RepositorySystemSession, CollectRequest)
33 public final class CollectRequest
36 private Artifact rootArtifact
;
38 private Dependency root
;
40 private List
<Dependency
> dependencies
= Collections
.emptyList();
42 private List
<Dependency
> managedDependencies
= Collections
.emptyList();
44 private List
<RemoteRepository
> repositories
= Collections
.emptyList();
46 private String context
= "";
48 private RequestTrace trace
;
51 * Creates an uninitialized request.
53 public CollectRequest()
55 // enables default constructor
59 * Creates a request with the specified properties.
61 * @param root The root dependency whose transitive dependencies should be collected, may be {@code null}.
62 * @param repositories The repositories to use for the collection, may be {@code null}.
64 public CollectRequest( Dependency root
, List
<RemoteRepository
> repositories
)
67 setRepositories( repositories
);
71 * Creates a new request with the specified properties.
73 * @param root The root dependency whose transitive dependencies should be collected, may be {@code null}.
74 * @param dependencies The direct dependencies to merge with the direct dependencies from the root dependency's
75 * artifact descriptor.
76 * @param repositories The repositories to use for the collection, may be {@code null}.
78 public CollectRequest( Dependency root
, List
<Dependency
> dependencies
, List
<RemoteRepository
> repositories
)
81 setDependencies( dependencies
);
82 setRepositories( repositories
);
86 * Creates a new request with the specified properties.
88 * @param dependencies The direct dependencies of some imaginary root, may be {@code null}.
89 * @param managedDependencies The dependency management information to apply to the transitive dependencies, may be
91 * @param repositories The repositories to use for the collection, may be {@code null}.
93 public CollectRequest( List
<Dependency
> dependencies
, List
<Dependency
> managedDependencies
,
94 List
<RemoteRepository
> repositories
)
96 setDependencies( dependencies
);
97 setManagedDependencies( managedDependencies
);
98 setRepositories( repositories
);
102 * Gets the root artifact for the dependency graph.
104 * @return The root artifact for the dependency graph or {@code null} if none.
106 public Artifact
getRootArtifact()
112 * Sets the root artifact for the dependency graph. This must not be confused with {@link #setRoot(Dependency)}: The
113 * root <em>dependency</em>, like any other specified dependency, will be subject to dependency
114 * collection/resolution, i.e. should have an artifact descriptor and a corresponding artifact file. The root
115 * <em>artifact</em> on the other hand is only used as a label for the root node of the graph in case no root
116 * dependency was specified. As such, the configured root artifact is ignored if {@link #getRoot()} does not return
119 * @param rootArtifact The root artifact for the dependency graph, may be {@code null}.
120 * @return This request for chaining, never {@code null}.
122 public CollectRequest
setRootArtifact( Artifact rootArtifact
)
124 this.rootArtifact
= rootArtifact
;
129 * Gets the root dependency of the graph.
131 * @return The root dependency of the graph or {@code null} if none.
133 public Dependency
getRoot()
139 * Sets the root dependency of the graph.
141 * @param root The root dependency of the graph, may be {@code null}.
142 * @return This request for chaining, never {@code null}.
144 public CollectRequest
setRoot( Dependency root
)
151 * Gets the direct dependencies.
153 * @return The direct dependencies, never {@code null}.
155 public List
<Dependency
> getDependencies()
161 * Sets the direct dependencies. If both a root dependency and direct dependencies are given in the request, the
162 * direct dependencies from the request will be merged with the direct dependencies from the root dependency's
163 * artifact descriptor, giving higher priority to the dependencies from the request.
165 * @param dependencies The direct dependencies, may be {@code null}.
166 * @return This request for chaining, never {@code null}.
168 public CollectRequest
setDependencies( List
<Dependency
> dependencies
)
170 if ( dependencies
== null )
172 this.dependencies
= Collections
.emptyList();
176 this.dependencies
= dependencies
;
182 * Adds the specified direct dependency.
184 * @param dependency The dependency to add, may be {@code null}.
185 * @return This request for chaining, never {@code null}.
187 public CollectRequest
addDependency( Dependency dependency
)
189 if ( dependency
!= null )
191 if ( this.dependencies
.isEmpty() )
193 this.dependencies
= new ArrayList
<Dependency
>();
195 this.dependencies
.add( dependency
);
201 * Gets the dependency management to apply to transitive dependencies.
203 * @return The dependency management to apply to transitive dependencies, never {@code null}.
205 public List
<Dependency
> getManagedDependencies()
207 return managedDependencies
;
211 * Sets the dependency management to apply to transitive dependencies. To clarify, this management does not apply to
212 * the direct dependencies of the root node.
214 * @param managedDependencies The dependency management, may be {@code null}.
215 * @return This request for chaining, never {@code null}.
217 public CollectRequest
setManagedDependencies( List
<Dependency
> managedDependencies
)
219 if ( managedDependencies
== null )
221 this.managedDependencies
= Collections
.emptyList();
225 this.managedDependencies
= managedDependencies
;
231 * Adds the specified managed dependency.
233 * @param managedDependency The managed dependency to add, may be {@code null}.
234 * @return This request for chaining, never {@code null}.
236 public CollectRequest
addManagedDependency( Dependency managedDependency
)
238 if ( managedDependency
!= null )
240 if ( this.managedDependencies
.isEmpty() )
242 this.managedDependencies
= new ArrayList
<Dependency
>();
244 this.managedDependencies
.add( managedDependency
);
250 * Gets the repositories to use for the collection.
252 * @return The repositories to use for the collection, never {@code null}.
254 public List
<RemoteRepository
> getRepositories()
260 * Sets the repositories to use for the collection.
262 * @param repositories The repositories to use for the collection, may be {@code null}.
263 * @return This request for chaining, never {@code null}.
265 public CollectRequest
setRepositories( List
<RemoteRepository
> repositories
)
267 if ( repositories
== null )
269 this.repositories
= Collections
.emptyList();
273 this.repositories
= repositories
;
279 * Adds the specified repository for collection.
281 * @param repository The repository to collect dependency information from, may be {@code null}.
282 * @return This request for chaining, never {@code null}.
284 public CollectRequest
addRepository( RemoteRepository repository
)
286 if ( repository
!= null )
288 if ( this.repositories
.isEmpty() )
290 this.repositories
= new ArrayList
<RemoteRepository
>();
292 this.repositories
.add( repository
);
298 * Gets the context in which this request is made.
300 * @return The context, never {@code null}.
302 public String
getRequestContext()
308 * Sets the context in which this request is made.
310 * @param context The context, may be {@code null}.
311 * @return This request for chaining, never {@code null}.
313 public CollectRequest
setRequestContext( String context
)
315 this.context
= ( context
!= null ) ? context
: "";
320 * Gets the trace information that describes the higher level request/operation in which this request is issued.
322 * @return The trace information about the higher level operation or {@code null} if none.
324 public RequestTrace
getTrace()
330 * Sets the trace information that describes the higher level request/operation in which this request is issued.
332 * @param trace The trace information about the higher level operation, may be {@code null}.
333 * @return This request for chaining, never {@code null}.
335 public CollectRequest
setTrace( RequestTrace trace
)
342 public String
toString()
344 return getRoot() + " -> " + getDependencies() + " < " + getRepositories();