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.Arrays;
22  import java.util.function.Supplier;
23  
24  import org.apache.maven.api.DependencyScope;
25  import org.apache.maven.repository.internal.artifact.FatArtifactTraverser;
26  import org.apache.maven.repository.internal.scopes.Maven4ScopeManagerConfiguration;
27  import org.apache.maven.repository.internal.type.DefaultTypeProvider;
28  import org.eclipse.aether.RepositorySystem;
29  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
30  import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
31  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
32  import org.eclipse.aether.collection.DependencyGraphTransformer;
33  import org.eclipse.aether.collection.DependencyManager;
34  import org.eclipse.aether.collection.DependencySelector;
35  import org.eclipse.aether.collection.DependencyTraverser;
36  import org.eclipse.aether.impl.scope.InternalScopeManager;
37  import org.eclipse.aether.internal.impl.scope.ManagedDependencyContextRefiner;
38  import org.eclipse.aether.internal.impl.scope.ManagedScopeDeriver;
39  import org.eclipse.aether.internal.impl.scope.ManagedScopeSelector;
40  import org.eclipse.aether.internal.impl.scope.OptionalDependencySelector;
41  import org.eclipse.aether.internal.impl.scope.ScopeDependencySelector;
42  import org.eclipse.aether.internal.impl.scope.ScopeManagerImpl;
43  import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
44  import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
45  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
46  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
47  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
48  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
49  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
50  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
51  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
52  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
53  
54  import static java.util.Objects.requireNonNull;
55  
56  /**
57   * A simple {@link Supplier} of {@link SessionBuilder} instances, that on each call supplies newly
58   * constructed instance. To create session out of builder, use {@link SessionBuilder#build()}. For proper closing
59   * of sessions, use {@link CloseableSession#close()} method on built instance(s).
60   * <p>
61   * Extend this class and override methods to customize, if needed.
62   *
63   * @since 4.0.0
64   * @deprecated since 4.0.0, use {@code maven-api-impl} jar instead
65   */
66  @Deprecated(since = "4.0.0")
67  public class MavenSessionBuilderSupplier implements Supplier<SessionBuilder> {
68      protected final RepositorySystem repositorySystem;
69      protected final InternalScopeManager scopeManager;
70  
71      public MavenSessionBuilderSupplier(RepositorySystem repositorySystem) {
72          this.repositorySystem = requireNonNull(repositorySystem);
73          this.scopeManager = new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE);
74      }
75  
76      /**
77       * Package protected constructor, only for use with {@link MavenRepositorySystemUtils}.
78       */
79      @Deprecated
80      MavenSessionBuilderSupplier() {
81          this.repositorySystem = null;
82          this.scopeManager = new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE);
83      }
84  
85      protected InternalScopeManager getScopeManager() {
86          return scopeManager;
87      }
88  
89      protected DependencyTraverser getDependencyTraverser() {
90          return new FatArtifactTraverser();
91      }
92  
93      protected DependencyManager getDependencyManager() {
94          return getDependencyManager(true); // same default as in Maven4
95      }
96  
97      public DependencyManager getDependencyManager(boolean transitive) {
98          return new ClassicDependencyManager(transitive, getScopeManager());
99      }
100 
101     protected DependencySelector getDependencySelector() {
102         return new AndDependencySelector(
103                 ScopeDependencySelector.legacy(
104                         null, Arrays.asList(DependencyScope.TEST.id(), DependencyScope.PROVIDED.id())),
105                 OptionalDependencySelector.fromDirect(),
106                 new ExclusionDependencySelector());
107     }
108 
109     protected DependencyGraphTransformer getDependencyGraphTransformer() {
110         return new ChainedDependencyGraphTransformer(
111                 new ConflictResolver(
112                         new NearestVersionSelector(), new ManagedScopeSelector(getScopeManager()),
113                         new SimpleOptionalitySelector(), new ManagedScopeDeriver(getScopeManager())),
114                 new ManagedDependencyContextRefiner(getScopeManager()));
115     }
116 
117     /**
118      * This method produces "surrogate" type registry that is static: it aims users that want to use
119      * Maven-Resolver without involving Maven Core and related things.
120      * <p>
121      * This type registry is NOT used by Maven Core: Maven replaces it during Session creation with a type registry
122      * that supports extending it (i.e. via Maven Extensions).
123      * <p>
124      * Important: this "static" list of types should be in-sync with core provided types.
125      */
126     protected ArtifactTypeRegistry getArtifactTypeRegistry() {
127         DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
128         new DefaultTypeProvider().types().forEach(stereotypes::add);
129         return stereotypes;
130     }
131 
132     protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
133         return new SimpleArtifactDescriptorPolicy(true, true);
134     }
135 
136     protected void configureSessionBuilder(SessionBuilder session) {
137         session.setDependencyTraverser(getDependencyTraverser());
138         session.setDependencyManager(getDependencyManager());
139         session.setDependencySelector(getDependencySelector());
140         session.setDependencyGraphTransformer(getDependencyGraphTransformer());
141         session.setArtifactTypeRegistry(getArtifactTypeRegistry());
142         session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
143         session.setScopeManager(getScopeManager());
144     }
145 
146     /**
147      * Creates a new Maven-like repository system session by initializing the session with values typical for
148      * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
149      * graphs, most other settings remain at their generic default value. Use the various setters to further configure
150      * the session with authentication, mirror, proxy and other information required for your environment. At least,
151      * local repository manager needs to be configured to make session be able to create session instance.
152      *
153      * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
154      * be set on builder to make it able to create session instances.
155      */
156     @Override
157     public SessionBuilder get() {
158         requireNonNull(repositorySystem, "repositorySystem");
159         SessionBuilder builder = repositorySystem.createSessionBuilder();
160         configureSessionBuilder(builder);
161         return builder;
162     }
163 }