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