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 && logger.isDebugEnabled() )
234 {
235 logger.debug( "The following scope filters were not used: " + buffer );
236 }
237 }
238 }
239
240
241
242
243
244
245 public boolean hasMissedCriteria()
246 {
247 return !nullScopeHit || !compileScopeHit || !runtimeScopeHit || !testScopeHit || !providedScopeHit
248 || !systemScopeHit;
249 }
250
251
252
253
254
255
256 public boolean isIncludeCompileScope()
257 {
258 return includeCompileScope;
259 }
260
261
262
263
264
265
266
267 public ScopeArtifactFilter setIncludeCompileScope( boolean pIncludeCompileScope )
268 {
269 this.includeCompileScope = pIncludeCompileScope;
270
271 return this;
272 }
273
274
275
276
277
278
279 public boolean isIncludeRuntimeScope()
280 {
281 return includeRuntimeScope;
282 }
283
284
285
286
287
288
289
290 public ScopeArtifactFilter setIncludeRuntimeScope( boolean pIncludeRuntimeScope )
291 {
292 this.includeRuntimeScope = pIncludeRuntimeScope;
293
294 return this;
295 }
296
297
298
299
300
301
302 public boolean isIncludeTestScope()
303 {
304 return includeTestScope;
305 }
306
307
308
309
310
311
312
313 public ScopeArtifactFilter setIncludeTestScope( boolean pIncludeTestScope )
314 {
315 this.includeTestScope = pIncludeTestScope;
316
317 return this;
318 }
319
320
321
322
323
324
325 public boolean isIncludeProvidedScope()
326 {
327 return includeProvidedScope;
328 }
329
330
331
332
333
334
335
336 public ScopeArtifactFilter setIncludeProvidedScope( boolean pIncludeProvidedScope )
337 {
338 this.includeProvidedScope = pIncludeProvidedScope;
339
340 return this;
341 }
342
343
344
345
346
347
348 public boolean isIncludeSystemScope()
349 {
350 return includeSystemScope;
351 }
352
353
354
355
356
357
358
359 public ScopeArtifactFilter setIncludeSystemScope( boolean pIncludeSystemScope )
360 {
361 this.includeSystemScope = pIncludeSystemScope;
362
363 return this;
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377
378 public ScopeArtifactFilter setIncludeCompileScopeWithImplications( boolean enabled )
379 {
380 includeSystemScope = enabled;
381 includeProvidedScope = enabled;
382 includeCompileScope = enabled;
383
384 return this;
385 }
386
387
388
389
390
391
392
393
394
395
396
397
398 public ScopeArtifactFilter setIncludeRuntimeScopeWithImplications( boolean enabled )
399 {
400 includeCompileScope = enabled;
401 includeRuntimeScope = enabled;
402
403 return this;
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420 public ScopeArtifactFilter setIncludeTestScopeWithImplications( boolean enabled )
421 {
422 includeSystemScope = enabled;
423 includeProvidedScope = enabled;
424 includeCompileScope = enabled;
425 includeRuntimeScope = enabled;
426 includeTestScope = enabled;
427
428 return this;
429 }
430
431
432
433
434
435
436
437 public ScopeArtifactFilter setIncludeNullScope( boolean enable )
438 {
439 includeNullScope = enable;
440
441 return this;
442 }
443
444
445
446
447
448
449 public ScopeArtifactFilter reset()
450 {
451 compileScopeHit = false;
452 runtimeScopeHit = false;
453 testScopeHit = false;
454 providedScopeHit = false;
455 systemScopeHit = false;
456 filteredArtifactIds.clear();
457
458 return this;
459 }
460 }