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.slf4j.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 final List<String> filteredArtifactIds = new ArrayList<>();
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
170
171 public String toString()
172 {
173 return "Scope filter [null-scope=" + includeNullScope + ", compile=" + includeCompileScope + ", runtime="
174 + includeRuntimeScope + ", test=" + includeTestScope + ", provided=" + includeProvidedScope + ", system="
175 + includeSystemScope + "]";
176 }
177
178
179 public void reportFilteredArtifacts( Logger logger )
180 {
181 if ( !filteredArtifactIds.isEmpty() && logger.isDebugEnabled() )
182 {
183 StringBuilder buffer = new StringBuilder( "The following artifacts were removed by this filter: " );
184
185 for ( String artifactId : filteredArtifactIds )
186 {
187 buffer.append( '\n' ).append( artifactId );
188 }
189
190 logger.debug( buffer.toString() );
191 }
192 }
193
194
195 public void reportMissedCriteria( Logger logger )
196 {
197 if ( logger.isDebugEnabled() )
198 {
199 StringBuilder buffer = new StringBuilder();
200
201 boolean report = false;
202 if ( !nullScopeHit )
203 {
204 buffer.append( "\no [Null Scope]" );
205 report = true;
206 }
207 if ( !compileScopeHit )
208 {
209 buffer.append( "\no Compile" );
210 report = true;
211 }
212 if ( !runtimeScopeHit )
213 {
214 buffer.append( "\no Runtime" );
215 report = true;
216 }
217 if ( !testScopeHit )
218 {
219 buffer.append( "\no Test" );
220 report = true;
221 }
222 if ( !providedScopeHit )
223 {
224 buffer.append( "\no Provided" );
225 report = true;
226 }
227 if ( !systemScopeHit )
228 {
229 buffer.append( "\no System" );
230 report = true;
231 }
232
233 if ( report )
234 {
235 logger.debug( "The following scope filters were not used: " + buffer.toString() );
236 }
237 }
238 }
239
240
241
242
243
244
245 public boolean hasMissedCriteria()
246 {
247 boolean report = false;
248
249 if ( !nullScopeHit )
250 {
251 report = true;
252 }
253 if ( !compileScopeHit )
254 {
255 report = true;
256 }
257 if ( !runtimeScopeHit )
258 {
259 report = true;
260 }
261 if ( !testScopeHit )
262 {
263 report = true;
264 }
265 if ( !providedScopeHit )
266 {
267 report = true;
268 }
269 if ( !systemScopeHit )
270 {
271 report = true;
272 }
273
274 return report;
275 }
276
277
278
279
280
281
282 public boolean isIncludeCompileScope()
283 {
284 return includeCompileScope;
285 }
286
287
288
289
290
291
292
293 public ScopeArtifactFilter setIncludeCompileScope( boolean pIncludeCompileScope )
294 {
295 this.includeCompileScope = pIncludeCompileScope;
296
297 return this;
298 }
299
300
301
302
303
304
305 public boolean isIncludeRuntimeScope()
306 {
307 return includeRuntimeScope;
308 }
309
310
311
312
313
314
315
316 public ScopeArtifactFilter setIncludeRuntimeScope( boolean pIncludeRuntimeScope )
317 {
318 this.includeRuntimeScope = pIncludeRuntimeScope;
319
320 return this;
321 }
322
323
324
325
326
327
328 public boolean isIncludeTestScope()
329 {
330 return includeTestScope;
331 }
332
333
334
335
336
337
338
339 public ScopeArtifactFilter setIncludeTestScope( boolean pIncludeTestScope )
340 {
341 this.includeTestScope = pIncludeTestScope;
342
343 return this;
344 }
345
346
347
348
349
350
351 public boolean isIncludeProvidedScope()
352 {
353 return includeProvidedScope;
354 }
355
356
357
358
359
360
361
362 public ScopeArtifactFilter setIncludeProvidedScope( boolean pIncludeProvidedScope )
363 {
364 this.includeProvidedScope = pIncludeProvidedScope;
365
366 return this;
367 }
368
369
370
371
372
373
374 public boolean isIncludeSystemScope()
375 {
376 return includeSystemScope;
377 }
378
379
380
381
382
383
384
385 public ScopeArtifactFilter setIncludeSystemScope( boolean pIncludeSystemScope )
386 {
387 this.includeSystemScope = pIncludeSystemScope;
388
389 return this;
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404 public ScopeArtifactFilter setIncludeCompileScopeWithImplications( boolean enabled )
405 {
406 includeSystemScope = enabled;
407 includeProvidedScope = enabled;
408 includeCompileScope = enabled;
409
410 return this;
411 }
412
413
414
415
416
417
418
419
420
421
422
423
424 public ScopeArtifactFilter setIncludeRuntimeScopeWithImplications( boolean enabled )
425 {
426 includeCompileScope = enabled;
427 includeRuntimeScope = enabled;
428
429 return this;
430 }
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446 public ScopeArtifactFilter setIncludeTestScopeWithImplications( boolean enabled )
447 {
448 includeSystemScope = enabled;
449 includeProvidedScope = enabled;
450 includeCompileScope = enabled;
451 includeRuntimeScope = enabled;
452 includeTestScope = enabled;
453
454 return this;
455 }
456
457
458
459
460
461
462
463 public ScopeArtifactFilter setIncludeNullScope( boolean enable )
464 {
465 includeNullScope = enable;
466
467 return this;
468 }
469
470
471
472
473
474
475 public ScopeArtifactFilter reset()
476 {
477 compileScopeHit = false;
478 runtimeScopeHit = false;
479 testScopeHit = false;
480 providedScopeHit = false;
481 systemScopeHit = false;
482 filteredArtifactIds.clear();
483
484 return this;
485 }
486 }