1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
59
60
61
62
63
64
65 public class MavenSessionBuilderSupplier implements Supplier<SessionBuilder> {
66 protected final RepositorySystem repositorySystem;
67 protected final InternalScopeManager scopeManager;
68
69 public MavenSessionBuilderSupplier(RepositorySystem repositorySystem) {
70 this.repositorySystem = requireNonNull(repositorySystem);
71 this.scopeManager = new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE);
72 }
73
74
75
76
77 @Deprecated
78 MavenSessionBuilderSupplier() {
79 this.repositorySystem = null;
80 this.scopeManager = new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE);
81 }
82
83 protected InternalScopeManager getScopeManager() {
84 return scopeManager;
85 }
86
87 protected DependencyTraverser getDependencyTraverser() {
88 return new FatArtifactTraverser();
89 }
90
91 protected DependencyManager getDependencyManager() {
92 return getDependencyManager(true);
93 }
94
95 public DependencyManager getDependencyManager(boolean transitive) {
96 return new ClassicDependencyManager(transitive, getScopeManager());
97 }
98
99 protected DependencySelector getDependencySelector() {
100 return new AndDependencySelector(
101 ScopeDependencySelector.legacy(
102 null, Arrays.asList(DependencyScope.TEST.id(), DependencyScope.PROVIDED.id())),
103 OptionalDependencySelector.fromDirect(),
104 new ExclusionDependencySelector());
105 }
106
107 protected DependencyGraphTransformer getDependencyGraphTransformer() {
108 return new ChainedDependencyGraphTransformer(
109 new ConflictResolver(
110 new NearestVersionSelector(), new ManagedScopeSelector(getScopeManager()),
111 new SimpleOptionalitySelector(), new ManagedScopeDeriver(getScopeManager())),
112 new ManagedDependencyContextRefiner(getScopeManager()));
113 }
114
115
116
117
118
119
120
121
122
123
124 protected ArtifactTypeRegistry getArtifactTypeRegistry() {
125 DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
126 new DefaultTypeProvider().types().forEach(stereotypes::add);
127 return stereotypes;
128 }
129
130 protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
131 return new SimpleArtifactDescriptorPolicy(true, true);
132 }
133
134 protected void configureSessionBuilder(SessionBuilder session) {
135 session.setDependencyTraverser(getDependencyTraverser());
136 session.setDependencyManager(getDependencyManager());
137 session.setDependencySelector(getDependencySelector());
138 session.setDependencyGraphTransformer(getDependencyGraphTransformer());
139 session.setArtifactTypeRegistry(getArtifactTypeRegistry());
140 session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
141 session.setScopeManager(getScopeManager());
142 }
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public SessionBuilder get() {
156 requireNonNull(repositorySystem, "repositorySystem");
157 SessionBuilder builder = repositorySystem.createSessionBuilder();
158 configureSessionBuilder(builder);
159 return builder;
160 }
161 }