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