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