1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.shared.artifact.filter; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.apache.maven.artifact.Artifact; |
26 | |
import org.apache.maven.artifact.DefaultArtifact; |
27 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
28 | |
import org.codehaus.plexus.logging.Logger; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ScopeArtifactFilter |
40 | |
implements ArtifactFilter, StatisticsReportingArtifactFilter |
41 | |
{ |
42 | |
private boolean includeCompileScope; |
43 | |
|
44 | |
private boolean includeRuntimeScope; |
45 | |
|
46 | |
private boolean includeTestScope; |
47 | |
|
48 | |
private boolean includeProvidedScope; |
49 | |
|
50 | |
private boolean includeSystemScope; |
51 | |
|
52 | 48 | private boolean includeNullScope = true; |
53 | |
|
54 | 48 | private boolean nullScopeHit = false; |
55 | |
|
56 | 48 | private boolean compileScopeHit = false; |
57 | |
|
58 | 48 | private boolean runtimeScopeHit = false; |
59 | |
|
60 | 48 | private boolean testScopeHit = false; |
61 | |
|
62 | 48 | private boolean providedScopeHit = false; |
63 | |
|
64 | 48 | private boolean systemScopeHit = false; |
65 | |
|
66 | 48 | private List filteredArtifactIds = new ArrayList(); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public ScopeArtifactFilter() |
73 | |
{ |
74 | |
|
75 | 11 | this( null ); |
76 | 11 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public ScopeArtifactFilter( String scope ) |
84 | 48 | { |
85 | 48 | if ( DefaultArtifact.SCOPE_COMPILE.equals( scope ) ) |
86 | |
{ |
87 | 7 | setIncludeCompileScopeWithImplications( true ); |
88 | |
} |
89 | 41 | else if ( DefaultArtifact.SCOPE_RUNTIME.equals( scope ) ) |
90 | |
{ |
91 | 8 | setIncludeRuntimeScopeWithImplications( true ); |
92 | |
} |
93 | 33 | else if ( DefaultArtifact.SCOPE_TEST.equals( scope ) ) |
94 | |
{ |
95 | 7 | setIncludeTestScopeWithImplications( true ); |
96 | |
} |
97 | 26 | else if ( DefaultArtifact.SCOPE_PROVIDED.equals( scope ) ) |
98 | |
{ |
99 | 7 | setIncludeProvidedScope( true ); |
100 | |
} |
101 | 19 | else if ( DefaultArtifact.SCOPE_SYSTEM.equals( scope ) ) |
102 | |
{ |
103 | 7 | setIncludeSystemScope( true ); |
104 | |
} |
105 | 48 | } |
106 | |
|
107 | |
public boolean include( Artifact artifact ) |
108 | |
{ |
109 | 98 | boolean result = true; |
110 | |
|
111 | 98 | if ( artifact.getScope() == null ) |
112 | |
{ |
113 | 17 | nullScopeHit = true; |
114 | 17 | result = includeNullScope; |
115 | |
} |
116 | 81 | else if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) ) |
117 | |
{ |
118 | 16 | compileScopeHit = true; |
119 | 16 | result = includeCompileScope; |
120 | |
} |
121 | 65 | else if ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) ) |
122 | |
{ |
123 | 16 | runtimeScopeHit = true; |
124 | 16 | result = includeRuntimeScope; |
125 | |
} |
126 | 49 | else if ( Artifact.SCOPE_TEST.equals( artifact.getScope() ) ) |
127 | |
{ |
128 | 16 | testScopeHit = true; |
129 | 16 | result = includeTestScope; |
130 | |
} |
131 | 33 | else if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) ) |
132 | |
{ |
133 | 17 | providedScopeHit = true; |
134 | 17 | result = includeProvidedScope; |
135 | |
} |
136 | 16 | else if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) |
137 | |
{ |
138 | 16 | systemScopeHit = true; |
139 | 16 | result = includeSystemScope; |
140 | |
} |
141 | |
|
142 | 98 | if ( !result ) |
143 | |
{ |
144 | |
|
145 | |
|
146 | |
|
147 | |
String id; |
148 | 46 | if ( artifact.getVersionRange() != null ) |
149 | |
{ |
150 | 1 | id = artifact.getDependencyConflictId() + ":" + artifact.getVersionRange(); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 45 | id = artifact.getId(); |
155 | |
} |
156 | |
|
157 | 46 | filteredArtifactIds.add( id ); |
158 | |
} |
159 | |
|
160 | 98 | return result; |
161 | |
} |
162 | |
|
163 | |
public String toString() |
164 | |
{ |
165 | 0 | return "Scope filter [null-scope=" + includeNullScope + ", compile=" + includeCompileScope + ", runtime=" + includeRuntimeScope + ", test=" + includeTestScope |
166 | |
+ ", provided=" + includeProvidedScope + ", system=" + includeSystemScope + "]"; |
167 | |
} |
168 | |
|
169 | |
public void reportFilteredArtifacts( Logger logger ) |
170 | |
{ |
171 | 0 | if ( !filteredArtifactIds.isEmpty() && logger.isDebugEnabled() ) |
172 | |
{ |
173 | 0 | StringBuffer buffer = new StringBuffer( "The following artifacts were removed by this filter: " ); |
174 | |
|
175 | 0 | for ( Iterator it = filteredArtifactIds.iterator(); it.hasNext(); ) |
176 | |
{ |
177 | 0 | String artifactId = (String) it.next(); |
178 | |
|
179 | 0 | buffer.append( '\n' ).append( artifactId ); |
180 | 0 | } |
181 | |
|
182 | 0 | logger.debug( buffer.toString() ); |
183 | |
} |
184 | 0 | } |
185 | |
|
186 | |
public void reportMissedCriteria( Logger logger ) |
187 | |
{ |
188 | 0 | if ( logger.isDebugEnabled() ) |
189 | |
{ |
190 | 0 | StringBuffer buffer = new StringBuffer(); |
191 | |
|
192 | 0 | boolean report = false; |
193 | 0 | if ( !nullScopeHit ) |
194 | |
{ |
195 | 0 | buffer.append( "\no [Null Scope]" ); |
196 | 0 | report = true; |
197 | |
} |
198 | 0 | if ( !compileScopeHit ) |
199 | |
{ |
200 | 0 | buffer.append( "\no Compile" ); |
201 | 0 | report = true; |
202 | |
} |
203 | 0 | if ( !runtimeScopeHit ) |
204 | |
{ |
205 | 0 | buffer.append( "\no Runtime" ); |
206 | 0 | report = true; |
207 | |
} |
208 | 0 | if ( !testScopeHit ) |
209 | |
{ |
210 | 0 | buffer.append( "\no Test" ); |
211 | 0 | report = true; |
212 | |
} |
213 | 0 | if ( !providedScopeHit ) |
214 | |
{ |
215 | 0 | buffer.append( "\no Provided" ); |
216 | 0 | report = true; |
217 | |
} |
218 | 0 | if ( !systemScopeHit ) |
219 | |
{ |
220 | 0 | buffer.append( "\no System" ); |
221 | 0 | report = true; |
222 | |
} |
223 | |
|
224 | 0 | if ( report ) |
225 | |
{ |
226 | 0 | logger.debug( "The following scope filters were not used: " + buffer.toString() ); |
227 | |
} |
228 | |
} |
229 | 0 | } |
230 | |
|
231 | |
public boolean hasMissedCriteria() |
232 | |
{ |
233 | 0 | boolean report = false; |
234 | |
|
235 | 0 | if ( !nullScopeHit ) |
236 | |
{ |
237 | 0 | report = true; |
238 | |
} |
239 | 0 | if ( !compileScopeHit ) |
240 | |
{ |
241 | 0 | report = true; |
242 | |
} |
243 | 0 | if ( !runtimeScopeHit ) |
244 | |
{ |
245 | 0 | report = true; |
246 | |
} |
247 | 0 | if ( !testScopeHit ) |
248 | |
{ |
249 | 0 | report = true; |
250 | |
} |
251 | 0 | if ( !providedScopeHit ) |
252 | |
{ |
253 | 0 | report = true; |
254 | |
} |
255 | 0 | if ( !systemScopeHit ) |
256 | |
{ |
257 | 0 | report = true; |
258 | |
} |
259 | |
|
260 | 0 | return report; |
261 | |
} |
262 | |
|
263 | |
public boolean isIncludeCompileScope() |
264 | |
{ |
265 | 0 | return includeCompileScope; |
266 | |
} |
267 | |
|
268 | |
public ScopeArtifactFilter setIncludeCompileScope( boolean includeCompileScope ) |
269 | |
{ |
270 | 1 | this.includeCompileScope = includeCompileScope; |
271 | |
|
272 | 1 | return this; |
273 | |
} |
274 | |
|
275 | |
public boolean isIncludeRuntimeScope() |
276 | |
{ |
277 | 0 | return includeRuntimeScope; |
278 | |
} |
279 | |
|
280 | |
public ScopeArtifactFilter setIncludeRuntimeScope( boolean includeRuntimeScope ) |
281 | |
{ |
282 | 3 | this.includeRuntimeScope = includeRuntimeScope; |
283 | |
|
284 | 3 | return this; |
285 | |
} |
286 | |
|
287 | |
public boolean isIncludeTestScope() |
288 | |
{ |
289 | 0 | return includeTestScope; |
290 | |
} |
291 | |
|
292 | |
public ScopeArtifactFilter setIncludeTestScope( boolean includeTestScope ) |
293 | |
{ |
294 | 1 | this.includeTestScope = includeTestScope; |
295 | |
|
296 | 1 | return this; |
297 | |
} |
298 | |
|
299 | |
public boolean isIncludeProvidedScope() |
300 | |
{ |
301 | 0 | return includeProvidedScope; |
302 | |
} |
303 | |
|
304 | |
public ScopeArtifactFilter setIncludeProvidedScope( boolean includeProvidedScope ) |
305 | |
{ |
306 | 9 | this.includeProvidedScope = includeProvidedScope; |
307 | |
|
308 | 9 | return this; |
309 | |
} |
310 | |
|
311 | |
public boolean isIncludeSystemScope() |
312 | |
{ |
313 | 0 | return includeSystemScope; |
314 | |
} |
315 | |
|
316 | |
public ScopeArtifactFilter setIncludeSystemScope( boolean includeSystemScope ) |
317 | |
{ |
318 | 9 | this.includeSystemScope = includeSystemScope; |
319 | |
|
320 | 9 | return this; |
321 | |
} |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
public ScopeArtifactFilter setIncludeCompileScopeWithImplications( boolean enabled ) |
333 | |
{ |
334 | 8 | includeSystemScope = enabled; |
335 | 8 | includeProvidedScope = enabled; |
336 | 8 | includeCompileScope = enabled; |
337 | |
|
338 | 8 | return this; |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
public ScopeArtifactFilter setIncludeRuntimeScopeWithImplications( boolean enabled ) |
350 | |
{ |
351 | 9 | includeCompileScope = enabled; |
352 | 9 | includeRuntimeScope = enabled; |
353 | |
|
354 | 9 | return this; |
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
public ScopeArtifactFilter setIncludeTestScopeWithImplications( boolean enabled ) |
369 | |
{ |
370 | 8 | includeSystemScope = enabled; |
371 | 8 | includeProvidedScope = enabled; |
372 | 8 | includeCompileScope = enabled; |
373 | 8 | includeRuntimeScope = enabled; |
374 | 8 | includeTestScope = enabled; |
375 | |
|
376 | 8 | return this; |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
public ScopeArtifactFilter setIncludeNullScope( boolean enable ) |
383 | |
{ |
384 | 1 | includeNullScope = enable; |
385 | |
|
386 | 1 | return this; |
387 | |
} |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
public ScopeArtifactFilter reset() |
393 | |
{ |
394 | 0 | compileScopeHit = false; |
395 | 0 | runtimeScopeHit = false; |
396 | 0 | testScopeHit = false; |
397 | 0 | providedScopeHit = false; |
398 | 0 | systemScopeHit = false; |
399 | 0 | filteredArtifactIds.clear(); |
400 | |
|
401 | 0 | return this; |
402 | |
} |
403 | |
} |