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