1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact.resolver.filter;
20
21 import java.lang.reflect.Proxy;
22 import java.nio.file.FileSystems;
23 import java.nio.file.Path;
24 import java.nio.file.PathMatcher;
25 import java.util.List;
26 import java.util.function.Predicate;
27 import java.util.stream.Collectors;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.model.Exclusion;
31
32
33
34
35 public class ExclusionArtifactFilter implements ArtifactFilter {
36
37 private final List<Exclusion> exclusions;
38 private final List<Predicate<Artifact>> predicates;
39
40 public ExclusionArtifactFilter(List<Exclusion> exclusions) {
41 this.exclusions = exclusions;
42 this.predicates =
43 exclusions.stream().map(ExclusionArtifactFilter::toPredicate).collect(Collectors.toList());
44 }
45
46 @Override
47 public boolean include(Artifact artifact) {
48 return predicates.stream().noneMatch(p -> p.test(artifact));
49 }
50
51 private static Predicate<Artifact> toPredicate(Exclusion exclusion) {
52 PathMatcher groupId = FileSystems.getDefault().getPathMatcher("glob:" + exclusion.getGroupId());
53 PathMatcher artifactId = FileSystems.getDefault().getPathMatcher("glob:" + exclusion.getArtifactId());
54 Predicate<Artifact> predGroupId = a -> groupId.matches(createPathProxy(a.getGroupId()));
55 Predicate<Artifact> predArtifactId = a -> artifactId.matches(createPathProxy(a.getArtifactId()));
56 return predGroupId.and(predArtifactId);
57 }
58
59
60
61
62
63
64
65 private static Path createPathProxy(String value) {
66 return (Path) Proxy.newProxyInstance(
67 ExclusionArtifactFilter.class.getClassLoader(), new Class[] {Path.class}, (proxy1, method, args) -> {
68 if ("toString".equals(method.getName())) {
69 return value;
70 }
71 throw new UnsupportedOperationException();
72 });
73 }
74 }