19
19
package org .apache .maven .impl ;
20
20
21
21
import java .nio .file .Path ;
22
+ import java .nio .file .PathMatcher ;
22
23
import java .util .List ;
23
24
import java .util .Objects ;
24
25
import java .util .Optional ;
@@ -41,6 +42,8 @@ public final class DefaultSourceRoot implements SourceRoot {
41
42
42
43
private final List <String > excludes ;
43
44
45
+ private transient PathMatcher matcher , matcherWithDefaults ;
46
+
44
47
private final ProjectScope scope ;
45
48
46
49
private final Language language ;
@@ -142,7 +145,7 @@ public DefaultSourceRoot(final ProjectScope scope, final Language language, fina
142
145
* @param directory directory of the source code
143
146
* @param includes list of patterns for the files to include, or {@code null} if unspecified
144
147
* @param excludes list of patterns for the files to exclude, or {@code null} if unspecified
145
- * * /
148
+ */
146
149
public DefaultSourceRoot (
147
150
final ProjectScope scope ,
148
151
final Language language ,
@@ -183,7 +186,7 @@ public Path directory() {
183
186
}
184
187
185
188
/**
186
- * {@return the list of pattern matchers for the files to include}.
189
+ * {@return the list of patterns for the files to include}.
187
190
*/
188
191
@ Override
189
192
@ SuppressWarnings ("ReturnOfCollectionOrArrayField" ) // Safe because unmodifiable
@@ -192,14 +195,36 @@ public List<String> includes() {
192
195
}
193
196
194
197
/**
195
- * {@return the list of pattern matchers for the files to exclude}.
198
+ * {@return the list of patterns for the files to exclude}.
196
199
*/
197
200
@ Override
198
201
@ SuppressWarnings ("ReturnOfCollectionOrArrayField" ) // Safe because unmodifiable
199
202
public List <String > excludes () {
200
203
return excludes ;
201
204
}
202
205
206
+ /**
207
+ * {@return a matcher combining the include and exclude patterns}.
208
+ *
209
+ * @param useDefaultExcludes whether to add <abbr>SCM</abbr> files to set of exclude patterns
210
+ */
211
+ @ Override
212
+ public Optional <PathMatcher > matcher (boolean useDefaultExcludes ) {
213
+ PathMatcher cached = useDefaultExcludes ? matcherWithDefaults : matcher ;
214
+ if (cached != null ) {
215
+ return Optional .of (cached );
216
+ }
217
+ Optional <PathMatcher > selector =
218
+ new PathSelector (directory (), includes (), excludes (), useDefaultExcludes ).simplify ();
219
+ cached = selector .orElse (null );
220
+ if (useDefaultExcludes ) {
221
+ matcherWithDefaults = cached ;
222
+ } else {
223
+ matcher = cached ;
224
+ }
225
+ return selector ;
226
+ }
227
+
203
228
/**
204
229
* {@return in which context the source files will be used}.
205
230
*/
0 commit comments