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.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
55
56
57
58
59
60
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
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());
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
106
107
108
109
110
111
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
134
135
136
137
138
139
140
141
142 @Override
143 public SessionBuilder get() {
144 SessionBuilder builder = repositorySystem.createSessionBuilder();
145 configureSessionBuilder(builder);
146 return builder;
147 }
148 }