1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.LinkedHashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import org.apache.maven.resolver.internal.ant.types.Authentication;
30 import org.apache.maven.resolver.internal.ant.types.Dependency;
31 import org.apache.maven.resolver.internal.ant.types.Exclusion;
32 import org.apache.maven.resolver.internal.ant.types.Proxy;
33 import org.apache.maven.resolver.internal.ant.types.RemoteRepositories;
34 import org.apache.maven.resolver.internal.ant.types.RemoteRepository;
35 import org.apache.tools.ant.Project;
36 import org.eclipse.aether.RepositorySystem;
37 import org.eclipse.aether.RepositorySystemSession;
38 import org.eclipse.aether.artifact.ArtifactProperties;
39 import org.eclipse.aether.artifact.ArtifactType;
40 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
41 import org.eclipse.aether.artifact.DefaultArtifact;
42 import org.eclipse.aether.artifact.DefaultArtifactType;
43 import org.eclipse.aether.repository.RepositoryPolicy;
44 import org.eclipse.aether.util.repository.AuthenticationBuilder;
45
46
47
48
49 class ConverterUtils {
50
51 private static org.eclipse.aether.artifact.Artifact toArtifact(Dependency dependency, ArtifactTypeRegistry types) {
52 ArtifactType type = types.get(dependency.getType());
53 if (type == null) {
54 type = new DefaultArtifactType(dependency.getType());
55 }
56
57 Map<String, String> props = null;
58 if ("system".equals(dependency.getScope()) && dependency.getSystemPath() != null) {
59 props = Collections.singletonMap(
60 ArtifactProperties.LOCAL_PATH, dependency.getSystemPath().getPath());
61 }
62
63 return new DefaultArtifact(
64 dependency.getGroupId(),
65 dependency.getArtifactId(),
66 dependency.getClassifier(),
67 null,
68 dependency.getVersion(),
69 props,
70 type);
71 }
72
73 public static org.eclipse.aether.repository.Authentication toAuthentication(Authentication auth) {
74 if (auth == null) {
75 return null;
76 }
77 AuthenticationBuilder authBuilder = new AuthenticationBuilder();
78 authBuilder.addUsername(auth.getUsername()).addPassword(auth.getPassword());
79 authBuilder.addPrivateKey(auth.getPrivateKeyFile(), auth.getPassphrase());
80 return authBuilder.build();
81 }
82
83 public static org.eclipse.aether.graph.Dependency toDependency(
84 Dependency dependency, List<Exclusion> exclusions, RepositorySystemSession session) {
85 String scope = dependency.getScope();
86 return new org.eclipse.aether.graph.Dependency(
87 toArtifact(dependency, session.getArtifactTypeRegistry()),
88 scope == null || scope.trim().isEmpty() ? "compile" : scope,
89 false,
90 toExclusions(dependency.getExclusions(), exclusions));
91 }
92
93 public static org.eclipse.aether.graph.Dependency toManagedDependency(
94 Dependency dependency, List<Exclusion> exclusions, RepositorySystemSession session) {
95 return new org.eclipse.aether.graph.Dependency(
96 toArtifact(dependency, session.getArtifactTypeRegistry()),
97 dependency.getScope(),
98 false,
99 toExclusions(dependency.getExclusions(), exclusions));
100 }
101
102
103
104
105
106 public static org.eclipse.aether.repository.RemoteRepository toDistRepository(
107 RemoteRepository repo, RepositorySystemSession session) {
108 org.eclipse.aether.repository.RemoteRepository result = toRepository(repo);
109 org.eclipse.aether.repository.RemoteRepository.Builder builder =
110 new org.eclipse.aether.repository.RemoteRepository.Builder(result);
111 builder.setAuthentication(session.getAuthenticationSelector().getAuthentication(result));
112 builder.setProxy(session.getProxySelector().getProxy(result));
113 return builder.build();
114 }
115
116 private static org.eclipse.aether.graph.Exclusion toExclusion(Exclusion exclusion) {
117 return new org.eclipse.aether.graph.Exclusion(
118 exclusion.getGroupId(), exclusion.getArtifactId(), exclusion.getClassifier(), exclusion.getExtension());
119 }
120
121 private static Collection<org.eclipse.aether.graph.Exclusion> toExclusions(
122 Collection<Exclusion> exclusions1, Collection<Exclusion> exclusions2) {
123 Collection<org.eclipse.aether.graph.Exclusion> results = new LinkedHashSet<>();
124 if (exclusions1 != null) {
125 for (Exclusion exclusion : exclusions1) {
126 results.add(toExclusion(exclusion));
127 }
128 }
129 if (exclusions2 != null) {
130 for (Exclusion exclusion : exclusions2) {
131 results.add(toExclusion(exclusion));
132 }
133 }
134 return results;
135 }
136
137 private static RepositoryPolicy toPolicy(
138 RemoteRepository.Policy policy, boolean enabled, String updates, String checksums) {
139 if (policy != null) {
140 enabled = policy.isEnabled();
141 if (policy.getChecksums() != null) {
142 checksums = policy.getChecksums();
143 }
144 if (policy.getUpdates() != null) {
145 updates = policy.getUpdates();
146 }
147 }
148 return new RepositoryPolicy(enabled, updates, checksums);
149 }
150
151
152
153
154 public static Properties addProperties(Properties props, Map<?, ?> map) {
155 if (props == null) {
156 props = new Properties();
157 }
158 for (Map.Entry<?, ?> entry : map.entrySet()) {
159 if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
160 props.put(entry.getKey(), entry.getValue());
161 }
162 }
163 return props;
164 }
165
166 public static org.eclipse.aether.repository.Proxy toProxy(Proxy proxy) {
167 if (proxy == null) {
168 return null;
169 }
170 return new org.eclipse.aether.repository.Proxy(
171 proxy.getType(), proxy.getHost(), proxy.getPort(), toAuthentication(proxy.getAuthentication()));
172 }
173
174 private static org.eclipse.aether.repository.RemoteRepository toRepository(RemoteRepository repo) {
175 org.eclipse.aether.repository.RemoteRepository.Builder builder =
176 new org.eclipse.aether.repository.RemoteRepository.Builder(repo.getId(), repo.getType(), repo.getUrl());
177 builder.setSnapshotPolicy(
178 toPolicy(repo.getSnapshotPolicy(), repo.isSnapshots(), repo.getUpdates(), repo.getChecksums()));
179 builder.setReleasePolicy(
180 toPolicy(repo.getReleasePolicy(), repo.isReleases(), repo.getUpdates(), repo.getChecksums()));
181 builder.setAuthentication(toAuthentication(repo.getAuthentication()));
182 return builder.build();
183 }
184
185 public static List<org.eclipse.aether.repository.RemoteRepository> toRepositories(
186 Project project,
187 RepositorySystem repositorySystem,
188 RepositorySystemSession session,
189 RemoteRepositories repos) {
190 List<RemoteRepository> repositories;
191
192 if (repos != null) {
193 repositories = repos.getRepositories();
194 } else {
195 repositories = new ArrayList<>();
196 }
197
198 List<org.eclipse.aether.repository.RemoteRepository> results = new ArrayList<>();
199 for (RemoteRepository repo : repositories) {
200 results.add(toRepository(repo));
201 }
202
203 results = repositorySystem.newResolutionRepositories(session, results);
204
205 return results;
206 }
207 }