1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Optional;
30 import java.util.stream.Collectors;
31
32 import org.apache.maven.artifact.handler.ArtifactHandler;
33 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
34 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
37 import org.apache.maven.impl.resolver.artifact.MavenArtifactProperties;
38 import org.eclipse.aether.DefaultRepositorySystemSession;
39 import org.eclipse.aether.RepositorySystem;
40 import org.eclipse.aether.RepositorySystemSession;
41 import org.eclipse.aether.artifact.Artifact;
42 import org.eclipse.aether.artifact.ArtifactProperties;
43 import org.eclipse.aether.artifact.ArtifactType;
44 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
45 import org.eclipse.aether.artifact.DefaultArtifact;
46 import org.eclipse.aether.artifact.DefaultArtifactType;
47 import org.eclipse.aether.graph.Dependency;
48 import org.eclipse.aether.graph.DependencyFilter;
49 import org.eclipse.aether.graph.DependencyNode;
50 import org.eclipse.aether.graph.Exclusion;
51 import org.eclipse.aether.repository.Authentication;
52 import org.eclipse.aether.repository.LocalRepository;
53 import org.eclipse.aether.repository.LocalRepositoryManager;
54 import org.eclipse.aether.repository.Proxy;
55 import org.eclipse.aether.repository.RemoteRepository;
56 import org.eclipse.aether.repository.RepositoryPolicy;
57 import org.eclipse.aether.repository.WorkspaceReader;
58 import org.eclipse.aether.repository.WorkspaceRepository;
59 import org.eclipse.aether.util.repository.AuthenticationBuilder;
60
61
62
63
64
65
66 public class RepositoryUtils {
67
68 private static String nullify(String string) {
69 return (string == null || string.isEmpty()) ? null : string;
70 }
71
72 public static org.apache.maven.artifact.Artifact toArtifact(Dependency dependency) {
73 if (dependency == null) {
74 return null;
75 }
76
77 org.apache.maven.artifact.Artifact result = toArtifact(dependency.getArtifact());
78 result.setScope(dependency.getScope());
79 result.setOptional(dependency.isOptional());
80
81 return result;
82 }
83
84 public static org.apache.maven.artifact.Artifact toArtifact(Artifact artifact) {
85 if (artifact == null) {
86 return null;
87 }
88
89 ArtifactHandler handler = newHandler(artifact);
90
91
92
93
94
95 org.apache.maven.artifact.Artifact result = new org.apache.maven.artifact.DefaultArtifact(
96 artifact.getGroupId(),
97 artifact.getArtifactId(),
98 artifact.getVersion(),
99 null,
100 artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension()),
101 nullify(artifact.getClassifier()),
102 handler);
103
104 result.setFile(artifact.getFile());
105 result.setResolved(artifact.getFile() != null);
106
107 List<String> trail = new ArrayList<>(1);
108 trail.add(result.getId());
109 result.setDependencyTrail(trail);
110
111 return result;
112 }
113
114 public static void toArtifacts(
115 Collection<org.apache.maven.artifact.Artifact> artifacts,
116 Collection<? extends DependencyNode> nodes,
117 List<String> trail,
118 DependencyFilter filter) {
119 for (DependencyNode node : nodes) {
120 org.apache.maven.artifact.Artifact artifact = toArtifact(node.getDependency());
121
122 List<String> nodeTrail = new ArrayList<>(trail.size() + 1);
123 nodeTrail.addAll(trail);
124 nodeTrail.add(artifact.getId());
125
126 if (filter == null || filter.accept(node, Collections.emptyList())) {
127 artifact.setDependencyTrail(nodeTrail);
128 artifacts.add(artifact);
129 }
130
131 toArtifacts(artifacts, node.getChildren(), nodeTrail, filter);
132 }
133 }
134
135 public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
136 if (artifact == null) {
137 return null;
138 }
139
140 String version = artifact.getVersion();
141 if (version == null && artifact.getVersionRange() != null) {
142 version = artifact.getVersionRange().toString();
143 }
144
145 Map<String, String> props = null;
146 if (org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
147 String localPath = (artifact.getFile() != null) ? artifact.getFile().getPath() : "";
148 props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, localPath);
149 }
150
151 Artifact result = new DefaultArtifact(
152 artifact.getGroupId(),
153 artifact.getArtifactId(),
154 artifact.getClassifier(),
155 artifact.getArtifactHandler().getExtension(),
156 version,
157 props,
158 newArtifactType(artifact.getType(), artifact.getArtifactHandler()));
159 result = result.setFile(artifact.getFile());
160
161 return result;
162 }
163
164 public static Dependency toDependency(
165 org.apache.maven.artifact.Artifact artifact, Collection<org.apache.maven.model.Exclusion> exclusions) {
166 if (artifact == null) {
167 return null;
168 }
169
170 Artifact result = toArtifact(artifact);
171
172 List<Exclusion> excl = Optional.ofNullable(exclusions).orElse(Collections.emptyList()).stream()
173 .map(RepositoryUtils::toExclusion)
174 .collect(Collectors.toList());
175 return new Dependency(result, artifact.getScope(), artifact.isOptional(), excl);
176 }
177
178 public static List<RemoteRepository> toRepos(List<ArtifactRepository> repos) {
179 return Optional.ofNullable(repos).orElse(Collections.emptyList()).stream()
180 .map(RepositoryUtils::toRepo)
181 .collect(Collectors.toList());
182 }
183
184 public static RemoteRepository toRepo(ArtifactRepository repo) {
185 RemoteRepository result = null;
186 if (repo != null) {
187 RemoteRepository.Builder builder =
188 new RemoteRepository.Builder(repo.getId(), getLayout(repo), repo.getUrl());
189 builder.setSnapshotPolicy(toPolicy(repo.getSnapshots()));
190 builder.setReleasePolicy(toPolicy(repo.getReleases()));
191 builder.setAuthentication(toAuthentication(repo.getAuthentication()));
192 builder.setProxy(toProxy(repo.getProxy()));
193 builder.setMirroredRepositories(toRepos(repo.getMirroredRepositories()));
194 builder.setBlocked(repo.isBlocked());
195 result = builder.build();
196 }
197 return result;
198 }
199
200 public static String getLayout(ArtifactRepository repo) {
201 try {
202 return repo.getLayout().getId();
203 } catch (LinkageError e) {
204
205
206
207 String className = repo.getLayout().getClass().getSimpleName();
208 if (className.endsWith("RepositoryLayout")) {
209 String layout = className.substring(0, className.length() - "RepositoryLayout".length());
210 if (!layout.isEmpty()) {
211 layout = Character.toLowerCase(layout.charAt(0)) + layout.substring(1);
212 return layout;
213 }
214 }
215 return "";
216 }
217 }
218
219 private static RepositoryPolicy toPolicy(ArtifactRepositoryPolicy policy) {
220 RepositoryPolicy result = null;
221 if (policy != null) {
222 result = new RepositoryPolicy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy());
223 }
224 return result;
225 }
226
227 private static Authentication toAuthentication(org.apache.maven.artifact.repository.Authentication auth) {
228 Authentication result = null;
229 if (auth != null) {
230 AuthenticationBuilder authBuilder = new AuthenticationBuilder();
231 authBuilder.addUsername(auth.getUsername()).addPassword(auth.getPassword());
232 authBuilder.addPrivateKey(auth.getPrivateKey(), auth.getPassphrase());
233 result = authBuilder.build();
234 }
235 return result;
236 }
237
238 private static Proxy toProxy(org.apache.maven.repository.Proxy proxy) {
239 Proxy result = null;
240 if (proxy != null) {
241 AuthenticationBuilder authBuilder = new AuthenticationBuilder();
242 authBuilder.addUsername(proxy.getUserName()).addPassword(proxy.getPassword());
243 result = new Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), authBuilder.build());
244 }
245 return result;
246 }
247
248 public static ArtifactHandler newHandler(Artifact artifact) {
249 String type = artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension());
250 return new DefaultArtifactHandler(
251 type,
252 artifact.getExtension(),
253 null,
254 null,
255 null,
256 Boolean.parseBoolean(artifact.getProperty(MavenArtifactProperties.INCLUDES_DEPENDENCIES, "")),
257 artifact.getProperty(ArtifactProperties.LANGUAGE, null),
258 Boolean.parseBoolean(artifact.getProperty(MavenArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
259 }
260
261 public static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
262 return handler != null
263 ? new DefaultArtifactType(
264 id,
265 handler.getExtension(),
266 handler.getClassifier(),
267 handler.getLanguage(),
268 handler.isAddedToClasspath(),
269 handler.isIncludesDependencies())
270 : null;
271 }
272
273 public static Dependency toDependency(
274 org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes) {
275 ArtifactType stereotype = stereotypes.get(dependency.getType());
276 if (stereotype == null) {
277 stereotype = new DefaultArtifactType(dependency.getType());
278 }
279
280 boolean system = dependency.getSystemPath() != null
281 && !dependency.getSystemPath().isEmpty();
282
283 Map<String, String> props = null;
284 if (system) {
285 props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
286 }
287
288 Artifact artifact = new DefaultArtifact(
289 dependency.getGroupId(),
290 dependency.getArtifactId(),
291 dependency.getClassifier(),
292 null,
293 dependency.getVersion(),
294 props,
295 stereotype);
296
297 List<Exclusion> exclusions = dependency.getExclusions().stream()
298 .map(RepositoryUtils::toExclusion)
299 .collect(Collectors.toList());
300
301 return new Dependency(
302 artifact,
303 dependency.getScope(),
304 dependency.getOptional() != null ? dependency.isOptional() : null,
305 exclusions);
306 }
307
308 private static Exclusion toExclusion(org.apache.maven.model.Exclusion exclusion) {
309 return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
310 }
311
312
313
314
315 @Deprecated
316 public static ArtifactTypeRegistry newArtifactTypeRegistry(ArtifactHandlerManager handlerManager) {
317 return typeId -> newArtifactType(typeId, handlerManager.getArtifactHandler(typeId));
318 }
319
320 public static Collection<Artifact> toArtifacts(Collection<org.apache.maven.artifact.Artifact> artifactsToConvert) {
321 return artifactsToConvert.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toList());
322 }
323
324 public static WorkspaceRepository getWorkspace(RepositorySystemSession session) {
325 WorkspaceReader reader = session.getWorkspaceReader();
326 return (reader != null) ? reader.getRepository() : null;
327 }
328
329 public static boolean repositoriesEquals(List<RemoteRepository> r1, List<RemoteRepository> r2) {
330 if (r1.size() != r2.size()) {
331 return false;
332 }
333
334 for (Iterator<RemoteRepository> it1 = r1.iterator(), it2 = r2.iterator(); it1.hasNext(); ) {
335 if (!repositoryEquals(it1.next(), it2.next())) {
336 return false;
337 }
338 }
339
340 return true;
341 }
342
343 public static int repositoriesHashCode(List<RemoteRepository> repositories) {
344 int result = 17;
345 for (RemoteRepository repository : repositories) {
346 result = 31 * result + repositoryHashCode(repository);
347 }
348 return result;
349 }
350
351 public static RepositorySystemSession overlay(
352 ArtifactRepository repository, RepositorySystemSession session, RepositorySystem system) {
353 if (repository == null || repository.getBasedir() == null) {
354 return session;
355 }
356
357 DefaultRepositorySystemSession newSession;
358 if (session != null) {
359 LocalRepositoryManager lrm = session.getLocalRepositoryManager();
360 if (lrm != null && lrm.getRepository().getBasedir().equals(new File(repository.getBasedir()))) {
361 return session;
362 }
363 newSession = new DefaultRepositorySystemSession(session);
364 } else {
365 newSession = new DefaultRepositorySystemSession(h -> false);
366 }
367
368 final LocalRepositoryManager llrm =
369 system.newLocalRepositoryManager(newSession, new LocalRepository(repository.getBasedir()));
370 newSession.setLocalRepositoryManager(llrm);
371 return newSession;
372 }
373
374 private static int repositoryHashCode(RemoteRepository repository) {
375 int result = 17;
376 Object obj = repository.getUrl();
377 result = 31 * result + (obj != null ? obj.hashCode() : 0);
378 return result;
379 }
380
381 private static boolean policyEquals(RepositoryPolicy p1, RepositoryPolicy p2) {
382 if (p1 == p2) {
383 return true;
384 }
385
386 return p1.isEnabled() == p2.isEnabled() && Objects.equals(p1.getChecksumPolicy(), p2.getChecksumPolicy());
387 }
388
389 private static boolean repositoryEquals(RemoteRepository r1, RemoteRepository r2) {
390 if (r1 == r2) {
391 return true;
392 }
393
394 return Objects.equals(r1.getId(), r2.getId())
395 && Objects.equals(r1.getUrl(), r2.getUrl())
396 && policyEquals(r1.getPolicy(false), r2.getPolicy(false))
397 && policyEquals(r1.getPolicy(true), r2.getPolicy(true));
398 }
399 }