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