1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.impl;
20
21 import java.nio.file.FileSystem;
22 import java.nio.file.Path;
23 import java.nio.file.PathMatcher;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Optional;
27
28 import org.apache.maven.api.Language;
29 import org.apache.maven.api.ProjectScope;
30 import org.apache.maven.api.Session;
31 import org.apache.maven.api.SourceRoot;
32 import org.apache.maven.api.Version;
33 import org.apache.maven.api.model.Resource;
34 import org.apache.maven.api.model.Source;
35
36
37
38
39 public final class DefaultSourceRoot implements SourceRoot {
40 private final Path directory;
41
42 private final List<PathMatcher> includes;
43
44 private final List<PathMatcher> excludes;
45
46 private final ProjectScope scope;
47
48 private final Language language;
49
50 private final String moduleName;
51
52 private final Version targetVersion;
53
54 private final Path targetPath;
55
56 private final boolean stringFiltering;
57
58 private final boolean enabled;
59
60
61
62
63
64
65
66
67 public DefaultSourceRoot(final Session session, final Path baseDir, final Source source) {
68 String value = nonBlank(source.getDirectory());
69 if (value == null) {
70 throw new IllegalArgumentException("Source declaration without directory value.");
71 }
72 directory = baseDir.resolve(value);
73 FileSystem fs = directory.getFileSystem();
74 includes = matchers(fs, source.getIncludes());
75 excludes = matchers(fs, source.getExcludes());
76 stringFiltering = source.isStringFiltering();
77 enabled = source.isEnabled();
78 moduleName = nonBlank(source.getModule());
79
80 value = nonBlank(source.getScope());
81 scope = (value != null) ? session.requireProjectScope(value) : ProjectScope.MAIN;
82
83 value = nonBlank(source.getLang());
84 language = (value != null) ? session.requireLanguage(value) : Language.JAVA_FAMILY;
85
86 value = nonBlank(source.getTargetVersion());
87 targetVersion = (value != null) ? session.parseVersion(value) : null;
88
89 value = nonBlank(source.getTargetPath());
90 targetPath = (value != null) ? baseDir.resolve(value) : null;
91 }
92
93
94
95
96
97
98
99
100
101 public DefaultSourceRoot(final Path baseDir, ProjectScope scope, Resource resource) {
102 String value = nonBlank(resource.getDirectory());
103 if (value == null) {
104 throw new IllegalArgumentException("Source declaration without directory value.");
105 }
106 directory = baseDir.resolve(value).normalize();
107 FileSystem fs = directory.getFileSystem();
108 includes = matchers(fs, resource.getIncludes());
109 excludes = matchers(fs, resource.getExcludes());
110 stringFiltering = Boolean.parseBoolean(resource.getFiltering());
111 enabled = true;
112 moduleName = null;
113 this.scope = scope;
114 language = Language.RESOURCES;
115 targetVersion = null;
116 targetPath = null;
117 }
118
119
120
121
122
123
124
125
126 public DefaultSourceRoot(final ProjectScope scope, final Language language, final Path directory) {
127 this.scope = Objects.requireNonNull(scope);
128 this.language = Objects.requireNonNull(language);
129 this.directory = Objects.requireNonNull(directory);
130 includes = List.of();
131 excludes = List.of();
132 moduleName = null;
133 targetVersion = null;
134 targetPath = null;
135 stringFiltering = false;
136 enabled = true;
137 }
138
139
140
141
142
143
144
145
146 public DefaultSourceRoot(
147 final ProjectScope scope,
148 final Language language,
149 final Path directory,
150 List<PathMatcher> includes,
151 List<PathMatcher> excludes) {
152 this.scope = Objects.requireNonNull(scope);
153 this.language = language;
154 this.directory = Objects.requireNonNull(directory);
155 this.includes = includes != null ? List.copyOf(includes) : List.of();
156 this.excludes = excludes != null ? List.copyOf(excludes) : List.of();
157 moduleName = null;
158 targetVersion = null;
159 targetPath = null;
160 stringFiltering = false;
161 enabled = true;
162 }
163
164
165
166
167 private static String nonBlank(String value) {
168 if (value != null) {
169 value = value.trim();
170 if (value.isBlank()) {
171 value = null;
172 }
173 }
174 return value;
175 }
176
177
178
179
180
181
182
183
184
185
186
187 private static List<PathMatcher> matchers(FileSystem fs, List<String> patterns) {
188 final var matchers = new PathMatcher[patterns.size()];
189 for (int i = 0; i < matchers.length; i++) {
190 String rawPattern = patterns.get(i);
191 String pattern = rawPattern.contains(":") ? rawPattern : "glob:" + rawPattern;
192 matchers[i] = new PathMatcher() {
193 final PathMatcher delegate = fs.getPathMatcher(pattern);
194
195 @Override
196 public boolean matches(Path path) {
197 return delegate.matches(path);
198 }
199
200 @Override
201 public String toString() {
202 return rawPattern;
203 }
204 };
205 }
206 return List.of(matchers);
207 }
208
209
210
211
212 @Override
213 public Path directory() {
214 return directory;
215 }
216
217
218
219
220 @Override
221 @SuppressWarnings("ReturnOfCollectionOrArrayField")
222 public List<PathMatcher> includes() {
223 return includes;
224 }
225
226
227
228
229 @Override
230 @SuppressWarnings("ReturnOfCollectionOrArrayField")
231 public List<PathMatcher> excludes() {
232 return excludes;
233 }
234
235
236
237
238 @Override
239 public ProjectScope scope() {
240 return scope;
241 }
242
243
244
245
246 @Override
247 public Language language() {
248 return language;
249 }
250
251
252
253
254 @Override
255 public Optional<String> module() {
256 return Optional.ofNullable(moduleName);
257 }
258
259
260
261
262 @Override
263 public Optional<Version> targetVersion() {
264 return Optional.ofNullable(targetVersion);
265 }
266
267
268
269
270 @Override
271 public Optional<Path> targetPath() {
272 return Optional.ofNullable(targetPath);
273 }
274
275
276
277
278 @Override
279 public boolean stringFiltering() {
280 return stringFiltering;
281 }
282
283
284
285
286 @Override
287 public boolean enabled() {
288 return enabled;
289 }
290
291
292
293
294 @Override
295 public int hashCode() {
296 return Objects.hash(
297 directory,
298 includes,
299 excludes,
300 scope,
301 language,
302 moduleName,
303 targetVersion,
304 targetPath,
305 stringFiltering,
306 enabled);
307 }
308
309
310
311
312
313
314 @Override
315 public boolean equals(Object obj) {
316 if (this == obj) {
317 return true;
318 }
319 if (obj instanceof DefaultSourceRoot other) {
320 return directory.equals(other.directory)
321 && includes.equals(other.includes)
322 && excludes.equals(other.excludes)
323 && Objects.equals(scope, other.scope)
324 && Objects.equals(language, other.language)
325 && Objects.equals(moduleName, other.moduleName)
326 && Objects.equals(targetVersion, other.targetVersion)
327 && stringFiltering == other.stringFiltering
328 && enabled == other.enabled;
329 }
330 return false;
331 }
332 }