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