1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.plugins.shade.mojo;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  
24  import org.apache.maven.artifact.Artifact;
25  
26  
27  
28  
29  class ArtifactSelector {
30  
31      private Collection<ArtifactId> includes;
32  
33      private Collection<ArtifactId> excludes;
34  
35      ArtifactSelector(Artifact projectArtifact, ArtifactSet artifactSet, String groupPrefix) {
36          this(
37                  (artifactSet != null) ? artifactSet.getIncludes() : null,
38                  (artifactSet != null) ? artifactSet.getExcludes() : null,
39                  groupPrefix);
40  
41          if (projectArtifact != null && !this.includes.isEmpty()) {
42              this.includes.add(new ArtifactId(projectArtifact));
43          }
44      }
45  
46      ArtifactSelector(Collection<String> includes, Collection<String> excludes, String groupPrefix) {
47          this.includes = toIds(includes);
48          this.excludes = toIds(excludes);
49  
50          if (groupPrefix != null && groupPrefix.length() > 0) {
51              this.includes.add(new ArtifactId(groupPrefix + "*", "*", "*", "*"));
52          }
53      }
54  
55      private static Collection<ArtifactId> toIds(Collection<String> patterns) {
56          Collection<ArtifactId> result = new HashSet<>();
57  
58          if (patterns != null) {
59              for (String pattern : patterns) {
60                  result.add(new ArtifactId(pattern));
61              }
62          }
63  
64          return result;
65      }
66  
67      public boolean isSelected(Artifact artifact) {
68          return artifact != null && isSelected(new ArtifactId(artifact));
69      }
70  
71      boolean isSelected(ArtifactId id) {
72          return (includes.isEmpty() || matches(includes, id)) && !matches(excludes, id);
73      }
74  
75      private boolean matches(Collection<ArtifactId> patterns, ArtifactId id) {
76          for (ArtifactId pattern : patterns) {
77              if (id.matches(pattern)) {
78                  return true;
79              }
80          }
81          return false;
82      }
83  }