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.eclipse.aether.supplier;
20  
21  import java.util.Arrays;
22  import java.util.function.Supplier;
23  
24  import org.eclipse.aether.RepositorySystem;
25  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
26  import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
27  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
28  import org.eclipse.aether.artifact.DefaultArtifactType;
29  import org.eclipse.aether.collection.DependencyGraphTransformer;
30  import org.eclipse.aether.collection.DependencyManager;
31  import org.eclipse.aether.collection.DependencySelector;
32  import org.eclipse.aether.collection.DependencyTraverser;
33  import org.eclipse.aether.internal.impl.scope.OptionalDependencySelector;
34  import org.eclipse.aether.internal.impl.scope.ScopeDependencySelector;
35  import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
36  import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
37  import org.eclipse.aether.util.artifact.JavaScopes;
38  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
39  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
40  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
41  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
42  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
43  import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
44  import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
45  import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
46  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
47  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
48  import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
49  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
50  
51  import static java.util.Objects.requireNonNull;
52  
53  /**
54   * A simple {@link Supplier} of {@link SessionBuilder} instances, that on each call supplies newly
55   * constructed instance. To create session out of builder, use {@link SessionBuilder#build()}. For proper closing
56   * of sessions, use {@link CloseableSession#close()} method on built instance(s).
57   * <p>
58   * Extend this class and override methods to customize, if needed.
59   * <p>
60   * Resolver created as this is NOT using {@link org.eclipse.aether.scope.ScopeManager}!
61   *
62   * @since 2.0.0
63   */
64  public class SessionBuilderSupplier implements Supplier<SessionBuilder> {
65      protected final RepositorySystem repositorySystem;
66  
67      public SessionBuilderSupplier(RepositorySystem repositorySystem) {
68          this.repositorySystem = requireNonNull(repositorySystem);
69      }
70  
71      protected void configureSessionBuilder(SessionBuilder session) {
72          session.setDependencyTraverser(getDependencyTraverser());
73          session.setDependencyManager(getDependencyManager());
74          session.setDependencySelector(getDependencySelector());
75          session.setDependencyGraphTransformer(getDependencyGraphTransformer());
76          session.setArtifactTypeRegistry(getArtifactTypeRegistry());
77          session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
78      }
79  
80      protected DependencyTraverser getDependencyTraverser() {
81          return new FatArtifactTraverser();
82      }
83  
84      protected DependencyManager getDependencyManager() {
85          return new ClassicDependencyManager(false, null);
86      }
87  
88      protected DependencySelector getDependencySelector() {
89          return new AndDependencySelector(
90                  ScopeDependencySelector.legacy(null, Arrays.asList(JavaScopes.TEST, JavaScopes.PROVIDED)),
91                  OptionalDependencySelector.fromDirect(),
92                  new ExclusionDependencySelector());
93      }
94  
95      protected DependencyGraphTransformer getDependencyGraphTransformer() {
96          return new ChainedDependencyGraphTransformer(
97                  new ConflictResolver(
98                          new NearestVersionSelector(), new JavaScopeSelector(),
99                          new SimpleOptionalitySelector(), new JavaScopeDeriver()),
100                 new JavaDependencyContextRefiner());
101     }
102 
103     protected ArtifactTypeRegistry getArtifactTypeRegistry() {
104         DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
105         stereotypes.add(new DefaultArtifactType("pom"));
106         stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
107         stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
108         stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
109         stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
110         stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
111         stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
112         stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
113         stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
114         stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
115         stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
116         stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
117         return stereotypes;
118     }
119 
120     protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
121         return new SimpleArtifactDescriptorPolicy(true, true);
122     }
123 
124     /**
125      * Creates a new Maven-like repository system session by initializing the session with values typical for
126      * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
127      * graphs, most other settings remain at their generic default value. Use the various setters to further configure
128      * the session with authentication, mirror, proxy and other information required for your environment. At least,
129      * local repository manager needs to be configured to make session be able to create session instance.
130      *
131      * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
132      * be set on builder to make it able to create session instances.
133      */
134     @Override
135     public SessionBuilder get() {
136         SessionBuilder builder = repositorySystem.createSessionBuilder();
137         configureSessionBuilder(builder);
138         return builder;
139     }
140 }