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.shared.dependency.graph;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Objects;
24  
25  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.dependency.graph.internal.DirectScopeDependencySelector;
28  import org.apache.maven.shared.dependency.graph.internal.VerboseJavaScopeSelector;
29  import org.eclipse.aether.collection.DependencyGraphTransformer;
30  import org.eclipse.aether.collection.DependencySelector;
31  import org.eclipse.aether.util.artifact.JavaScopes;
32  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
33  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
34  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
35  import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
36  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
37  import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
38  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
39  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
40  
41  /**
42   * <div>
43   * This class will carry various options used by
44   * {@link DependencyCollectorBuilder#collectDependencyGraph(DependencyCollectorRequest)}
45   * </div>
46   * <div>
47   * There is a set of default values such:
48   * </div>
49   * <div>
50   * DependencySelector
51   * <pre>
52   *  new AndDependencySelector(
53   *             new DirectScopeDependencySelector( JavaScopes.TEST ),
54   *             new DirectScopeDependencySelector( JavaScopes.PROVIDED ),
55   *             new OptionalDependencySelector(),
56   *             new ExclusionDependencySelector() );
57   * </pre>
58   * </div>
59   * <div>
60   * DependencyGraphTransformer
61   * <pre>
62   * new ConflictResolver(
63   *             new NearestVersionSelector(),
64   *             new VerboseJavaScopeSelector(),
65   *             new SimpleOptionalitySelector(),
66   *             new JavaScopeDeriver() );
67   * </pre>
68   * </div>
69   * <div>
70   * configProperties have 2 default values
71   * <pre>
72   *   ConflictResolver.CONFIG_PROP_VERBOSE, true
73   *   DependencyManagerUtils.CONFIG_PROP_VERBOSE, true
74   * </pre>
75   * <a href="https://maven.apache.org/resolver/configuration.html">Move Resolver configuration properties</a>.
76   * </div>
77   * @since 3.2.1
78   */
79  public class DependencyCollectorRequest {
80  
81      private final ProjectBuildingRequest buildingRequest;
82  
83      private ArtifactFilter filter;
84  
85      private Map<String, Object> configProperties = new HashMap<>();
86  
87      private DependencySelector dependencySelector = new AndDependencySelector(
88              new DirectScopeDependencySelector(JavaScopes.TEST),
89              new DirectScopeDependencySelector(JavaScopes.PROVIDED),
90              new OptionalDependencySelector(),
91              new ExclusionDependencySelector());
92  
93      private DependencyGraphTransformer dependencyGraphTransformer = new ConflictResolver(
94              new NearestVersionSelector(),
95              new VerboseJavaScopeSelector(),
96              new SimpleOptionalitySelector(),
97              new JavaScopeDeriver());
98  
99      public DependencyCollectorRequest(ProjectBuildingRequest buildingRequest) {
100         this(buildingRequest, null);
101     }
102 
103     public DependencyCollectorRequest(ProjectBuildingRequest buildingRequest, ArtifactFilter filter) {
104         Objects.requireNonNull(buildingRequest, "ProjectBuildingRequest cannot be null");
105         this.buildingRequest = buildingRequest;
106         this.filter = filter;
107         configProperties.put(ConflictResolver.CONFIG_PROP_VERBOSE, true);
108         configProperties.put(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
109     }
110 
111     public ProjectBuildingRequest getBuildingRequest() {
112         return buildingRequest;
113     }
114 
115     public ArtifactFilter getFilter() {
116         return filter;
117     }
118 
119     public DependencySelector getDependencySelector() {
120         return dependencySelector;
121     }
122 
123     public DependencyCollectorRequest dependencySelector(DependencySelector dependencySelector) {
124         this.dependencySelector = dependencySelector;
125         return this;
126     }
127 
128     public DependencyGraphTransformer getDependencyGraphTransformer() {
129         return dependencyGraphTransformer;
130     }
131 
132     public DependencyCollectorRequest dependencyGraphTransformer(
133             DependencyGraphTransformer dependencyGraphTransformer) {
134         this.dependencyGraphTransformer = dependencyGraphTransformer;
135         return this;
136     }
137 
138     public Map<String, Object> getConfigProperties() {
139         return this.configProperties;
140     }
141 
142     public void addConfigProperty(String key, Object value) {
143         this.configProperties.put(key, value);
144     }
145 
146     public void removeConfigProperty(String key) {
147         this.configProperties.remove(key);
148     }
149 }