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