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.repository.internal;
20  
21  import java.util.function.Supplier;
22  
23  import org.apache.maven.repository.internal.artifact.FatArtifactTraverser;
24  import org.apache.maven.repository.internal.scopes.MavenDependencyContextRefiner;
25  import org.apache.maven.repository.internal.scopes.MavenScopeDeriver;
26  import org.apache.maven.repository.internal.scopes.MavenScopeSelector;
27  import org.apache.maven.repository.internal.scopes.MavenSystemScopeHandler;
28  import org.apache.maven.repository.internal.type.DefaultTypeProvider;
29  import org.eclipse.aether.RepositorySystem;
30  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
31  import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
32  import org.eclipse.aether.SystemScopeHandler;
33  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
34  import org.eclipse.aether.collection.DependencyGraphTransformer;
35  import org.eclipse.aether.collection.DependencyManager;
36  import org.eclipse.aether.collection.DependencySelector;
37  import org.eclipse.aether.collection.DependencyTraverser;
38  import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
39  import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
40  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
41  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
42  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
43  import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
44  import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
45  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
46  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
47  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
48  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
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   *
60   * @since 4.0.0
61   */
62  public class MavenSessionBuilderSupplier implements Supplier<SessionBuilder> {
63      protected final RepositorySystem repositorySystem;
64  
65      public MavenSessionBuilderSupplier(RepositorySystem repositorySystem) {
66          this.repositorySystem = requireNonNull(repositorySystem);
67      }
68  
69      /**
70       * Package protected constructor, only for use with {@link MavenRepositorySystemUtils}.
71       */
72      @Deprecated
73      MavenSessionBuilderSupplier() {
74          this.repositorySystem = null;
75      }
76  
77      protected DependencyTraverser getDependencyTraverser() {
78          return new FatArtifactTraverser();
79      }
80  
81      protected SystemScopeHandler getSystemScopeHandler() {
82          return new MavenSystemScopeHandler();
83      }
84  
85      protected DependencyManager getDependencyManager() {
86          return new ClassicDependencyManager(true, getSystemScopeHandler()); // same default as in Maven4
87      }
88  
89      protected DependencySelector getDependencySelector() {
90          return new AndDependencySelector(
91                  new ScopeDependencySelector("test", "provided"),
92                  new OptionalDependencySelector(),
93                  new ExclusionDependencySelector());
94      }
95  
96      protected DependencyGraphTransformer getDependencyGraphTransformer() {
97          return new ChainedDependencyGraphTransformer(
98                  new ConflictResolver(
99                          new NearestVersionSelector(), new MavenScopeSelector(),
100                         new SimpleOptionalitySelector(), new MavenScopeDeriver()),
101                 new MavenDependencyContextRefiner());
102     }
103 
104     /**
105      * This method produces "surrogate" type registry that is static: it aims users that want to use
106      * Maven-Resolver without involving Maven Core and related things.
107      * <p>
108      * This type registry is NOT used by Maven Core: Maven replaces it during Session creation with a type registry
109      * that supports extending it (i.e. via Maven Extensions).
110      * <p>
111      * Important: this "static" list of types should be in-sync with core provided types.
112      */
113     protected ArtifactTypeRegistry getArtifactTypeRegistry() {
114         DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
115         new DefaultTypeProvider().types().forEach(stereotypes::add);
116         return stereotypes;
117     }
118 
119     protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
120         return new SimpleArtifactDescriptorPolicy(true, true);
121     }
122 
123     protected void configureSessionBuilder(SessionBuilder session) {
124         session.setDependencyTraverser(getDependencyTraverser());
125         session.setDependencyManager(getDependencyManager());
126         session.setDependencySelector(getDependencySelector());
127         session.setDependencyGraphTransformer(getDependencyGraphTransformer());
128         session.setArtifactTypeRegistry(getArtifactTypeRegistry());
129         session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
130     }
131 
132     /**
133      * Creates a new Maven-like repository system session by initializing the session with values typical for
134      * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
135      * graphs, most other settings remain at their generic default value. Use the various setters to further configure
136      * the session with authentication, mirror, proxy and other information required for your environment. At least,
137      * local repository manager needs to be configured to make session be able to create session instance.
138      *
139      * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
140      * be set on builder to make it able to create session instances.
141      */
142     @Override
143     public SessionBuilder get() {
144         SessionBuilder builder = repositorySystem.createSessionBuilder();
145         configureSessionBuilder(builder);
146         return builder;
147     }
148 }