1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
28 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
29 import org.apache.maven.artifact.versioning.VersionRange;
30 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
31 import org.apache.maven.plugin.logging.Log;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39
40 public class BannedDependencies
41 extends AbstractBanDependencies
42 {
43
44
45
46
47
48
49 public List<String> excludes = null;
50
51
52
53
54
55
56
57
58 public List<String> includes = null;
59
60
61
62
63
64 protected Set<Artifact> checkDependencies( Set<Artifact> theDependencies, Log log )
65 throws EnforcerRuleException
66 {
67 Set<Artifact> excluded = checkDependencies( theDependencies, excludes );
68
69
70
71 if ( excluded != null )
72 {
73 Set<Artifact> included = checkDependencies( theDependencies, includes );
74 if ( included != null )
75 {
76 excluded.removeAll( included );
77 }
78 }
79 return excluded;
80
81 }
82
83
84
85
86
87
88
89
90
91 private Set<Artifact> checkDependencies( Set<Artifact> dependencies, List<String> thePatterns )
92 throws EnforcerRuleException
93 {
94 Set<Artifact> foundMatches = null;
95
96 if ( thePatterns != null && thePatterns.size() > 0 )
97 {
98
99 for ( String pattern : thePatterns )
100 {
101
102 String[] subStrings = pattern.split( ":" );
103 subStrings = StringUtils.stripAll( subStrings );
104
105 for ( Artifact artifact : dependencies )
106 {
107 if ( compareDependency( subStrings, artifact ) )
108 {
109
110 if ( foundMatches == null )
111 {
112 foundMatches = new HashSet<Artifact>();
113 }
114 foundMatches.add( artifact );
115 }
116 }
117 }
118 }
119 return foundMatches;
120 }
121
122
123
124
125
126
127
128
129
130
131 protected boolean compareDependency( String[] pattern, Artifact artifact )
132 throws EnforcerRuleException
133 {
134
135 boolean result = false;
136 if ( pattern.length > 0 )
137 {
138 result = pattern[0].equals( "*" ) || artifact.getGroupId().equals( pattern[0] );
139 }
140
141 if ( result && pattern.length > 1 )
142 {
143 result = pattern[1].equals( "*" ) || artifact.getArtifactId().equals( pattern[1] );
144 }
145
146 if ( result && pattern.length > 2 )
147 {
148
149 if ( pattern[2].equals( "*" ) || artifact.getVersion().equals( pattern[2] ) )
150 {
151 result = true;
152 }
153 else
154 {
155 try
156 {
157 result =
158 AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( pattern[2] ),
159 new DefaultArtifactVersion( artifact.getBaseVersion() ) );
160 }
161 catch ( InvalidVersionSpecificationException e )
162 {
163 throw new EnforcerRuleException( "Invalid Version Range: ", e );
164 }
165 }
166 }
167
168 if ( result && pattern.length > 3 )
169 {
170 String type = artifact.getType();
171 if ( type == null || type.equals( "" ) )
172 {
173 type = "jar";
174 }
175 result = pattern[3].equals( "*" ) || type.equals( pattern[3] );
176 }
177
178 if ( result && pattern.length > 4 )
179 {
180 String scope = artifact.getScope();
181 if ( scope == null || scope.equals( "" ) )
182 {
183 scope = "compile";
184 }
185 result = pattern[4].equals( "*" ) || scope.equals( pattern[4] );
186 }
187
188 return result;
189 }
190
191
192
193
194
195
196 public List<String> getExcludes()
197 {
198 return this.excludes;
199 }
200
201
202
203
204
205
206 public void setExcludes( List<String> theExcludes )
207 {
208 this.excludes = theExcludes;
209 }
210
211
212
213
214
215
216 public List<String> getIncludes()
217 {
218 return this.includes;
219 }
220
221
222
223
224
225
226 public void setIncludes( List<String> theIncludes )
227 {
228 this.includes = theIncludes;
229 }
230
231 }