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