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 2.0.0
59   *
60   * @deprecated (To be removed as it was introduced in 2.0.0-alpha-2!) This class is wrong, as it uses Resolver 1.x
61   * bits that do interpret dependency scopes. The proper session supplier should be provided by consumer project
62   * (Maven) that also defines the dependency scopes and their meaning and semantics, as session need to be equipped
63   * with these bits. Session is very much dependent on the consumer project.
64   */
65  @Deprecated
66  public class SessionBuilderSupplier implements Supplier<SessionBuilder> {
67      protected final RepositorySystem repositorySystem;
68  
69      public SessionBuilderSupplier(RepositorySystem repositorySystem) {
70          this.repositorySystem = requireNonNull(repositorySystem);
71      }
72  
73      protected void configureSessionBuilder(SessionBuilder session) {
74          session.setDependencyTraverser(getDependencyTraverser());
75          session.setDependencyManager(getDependencyManager());
76          session.setDependencySelector(getDependencySelector());
77          session.setDependencyGraphTransformer(getDependencyGraphTransformer());
78          session.setArtifactTypeRegistry(getArtifactTypeRegistry());
79          session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
80      }
81  
82      protected DependencyTraverser getDependencyTraverser() {
83          return new FatArtifactTraverser();
84      }
85  
86      protected DependencyManager getDependencyManager() {
87          return new ClassicDependencyManager();
88      }
89  
90      protected DependencySelector getDependencySelector() {
91          return new AndDependencySelector(
92                  new ScopeDependencySelector("test", "provided"),
93                  new OptionalDependencySelector(),
94                  new ExclusionDependencySelector());
95      }
96  
97      protected DependencyGraphTransformer getDependencyGraphTransformer() {
98          return new ChainedDependencyGraphTransformer(
99                  new ConflictResolver(
100                         new NearestVersionSelector(), new JavaScopeSelector(),
101                         new SimpleOptionalitySelector(), new JavaScopeDeriver()),
102                 new JavaDependencyContextRefiner());
103     }
104 
105     protected ArtifactTypeRegistry getArtifactTypeRegistry() {
106         DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
107         stereotypes.add(new DefaultArtifactType("pom"));
108         stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
109         stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
110         stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
111         stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
112         stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
113         stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
114         stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
115         stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
116         stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
117         stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
118         stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
119         return stereotypes;
120     }
121 
122     protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
123         return new SimpleArtifactDescriptorPolicy(true, true);
124     }
125 
126     /**
127      * Creates a new Maven-like repository system session by initializing the session with values typical for
128      * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
129      * graphs, most other settings remain at their generic default value. Use the various setters to further configure
130      * the session with authentication, mirror, proxy and other information required for your environment. At least,
131      * local repository manager needs to be configured to make session be able to create session instance.
132      *
133      * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
134      * be set on builder to make it able to create session instances.
135      */
136     @Override
137     public SessionBuilder get() {
138         SessionBuilder builder = repositorySystem.createSessionBuilder();
139         configureSessionBuilder(builder);
140         return builder;
141     }
142 }