1 package org.apache.maven.shared.artifact.filter.collection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.LinkedHashSet;
23 import java.util.Set;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
27 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
28
29 import static org.apache.maven.shared.artifact.filter.internal.Utils.isNotEmpty;
30
31
32
33
34
35
36 public class ScopeFilter
37 extends AbstractArtifactsFilter
38 {
39
40 private String includeScope;
41
42 private String excludeScope;
43
44
45
46
47
48
49
50 public ScopeFilter( String includeScope, String excludeScope )
51 {
52 this.includeScope = includeScope;
53 this.excludeScope = excludeScope;
54 }
55
56
57
58
59
60
61
62 public Set<Artifact> filter( Set<Artifact> artifacts )
63 throws ArtifactFilterException
64 {
65 Set<Artifact> results = artifacts;
66
67 if ( isNotEmpty( includeScope ) )
68 {
69 if ( !Artifact.SCOPE_COMPILE.equals( includeScope ) && !Artifact.SCOPE_TEST.equals( includeScope )
70 && !Artifact.SCOPE_PROVIDED.equals( includeScope ) && !Artifact.SCOPE_RUNTIME.equals( includeScope )
71 && !Artifact.SCOPE_SYSTEM.equals( includeScope ) )
72 {
73 throw new ArtifactFilterException( "Invalid Scope in includeScope: " + includeScope );
74 }
75
76 results = new LinkedHashSet<>();
77
78 if ( Artifact.SCOPE_PROVIDED.equals( includeScope ) || Artifact.SCOPE_SYSTEM.equals( includeScope ) )
79 {
80 results = includeSingleScope( artifacts, includeScope );
81 }
82 else
83 {
84 ArtifactFilter saf = new ScopeArtifactFilter( includeScope );
85
86 for ( Artifact artifact : artifacts )
87 {
88 if ( saf.include( artifact ) )
89 {
90 results.add( artifact );
91 }
92 }
93 }
94 }
95 else if ( isNotEmpty( excludeScope ) )
96 {
97 if ( !Artifact.SCOPE_COMPILE.equals( excludeScope ) && !Artifact.SCOPE_TEST.equals( excludeScope )
98 && !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_RUNTIME.equals( excludeScope )
99 && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
100 {
101 throw new ArtifactFilterException( "Invalid Scope in excludeScope: " + excludeScope );
102 }
103 results = new LinkedHashSet<>();
104
105
106
107 if ( Artifact.SCOPE_TEST.equals( excludeScope ) )
108 {
109 throw new ArtifactFilterException( " Can't exclude Test scope, this will exclude everything." );
110 }
111 else if ( !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
112 {
113 ArtifactFilter saf = new ScopeArtifactFilter( excludeScope );
114
115 for ( Artifact artifact : artifacts )
116 {
117 if ( !saf.include( artifact ) )
118 {
119 results.add( artifact );
120 }
121 }
122 }
123 else
124 {
125 results = excludeSingleScope( artifacts, excludeScope );
126 }
127 }
128
129 return results;
130 }
131
132 private Set<Artifact> includeSingleScope( Set<Artifact> artifacts, String scope )
133 {
134 Set<Artifact> results = new LinkedHashSet<>();
135 for ( Artifact artifact : artifacts )
136 {
137 if ( scope.equals( artifact.getScope() ) )
138 {
139 results.add( artifact );
140 }
141 }
142 return results;
143 }
144
145 private Set<Artifact> excludeSingleScope( Set<Artifact> artifacts, String scope )
146 {
147 Set<Artifact> results = new LinkedHashSet<>();
148 for ( Artifact artifact : artifacts )
149 {
150 if ( !scope.equals( artifact.getScope() ) )
151 {
152 results.add( artifact );
153 }
154 }
155 return results;
156 }
157
158
159
160
161
162
163 public String getIncludeScope()
164 {
165 return this.includeScope;
166 }
167
168
169
170
171
172
173
174 public void setIncludeScope( String scope )
175 {
176 this.includeScope = scope;
177 }
178
179
180
181
182
183
184 public String getExcludeScope()
185 {
186 return this.excludeScope;
187 }
188
189
190
191
192
193
194
195 public void setExcludeScope( String scope )
196 {
197 this.excludeScope = scope;
198 }
199
200 }