Skip to content

Commit 10d7d79

Browse files
committed
Add a SourceRoot.matcher(boolean) method.
The matcher returned by that method combines the effects of all includes and excludes.
1 parent 08a7643 commit 10d7d79

File tree

4 files changed

+611
-7
lines changed

4 files changed

+611
-7
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.maven.api;
2020

2121
import java.nio.file.Path;
22+
import java.nio.file.PathMatcher;
2223
import java.util.List;
2324
import java.util.Optional;
2425

@@ -45,10 +46,11 @@ default Path directory() {
4546
}
4647

4748
/**
48-
* {@return the list of pattern matchers for the files to include}.
49+
* {@return the list of patterns for the files to include}.
4950
* The path separator is {@code /} on all platforms, including Windows.
50-
* If the pattern does not start with {@code regex:} or {@code glob:}, the default variation of
51-
* the {@code glob} pattern will be used.
51+
* The prefix before the {@code :} character, if present and longer than 1 character, is the syntax.
52+
* If no syntax is specified, or if its length is 1 character (interpreted as a Windows drive),
53+
* the default is a Maven-specific variation of the {@code "glob"} pattern.
5254
*
5355
* <p>
5456
* The default implementation returns an empty list, which means to apply a language-dependent pattern.
@@ -59,14 +61,24 @@ default List<String> includes() {
5961
}
6062

6163
/**
62-
* {@return the list of pattern matchers for the files to exclude}.
64+
* {@return the list of patterns for the files to exclude}.
6365
* The exclusions are applied after the inclusions.
6466
* The default implementation returns an empty list.
6567
*/
6668
default List<String> excludes() {
6769
return List.of();
6870
}
6971

72+
/**
73+
* {@return a matcher combining the include and exclude patterns}.
74+
* The matcher is absent if the includes/excludes lists are empty
75+
* and {@code useDefaultExcludes} is {@code false}.
76+
*
77+
* @param useDefaultExcludes whether to add the default set of patterns to exclude
78+
* (mostly <abbr>SCM</abbr> files)
79+
*/
80+
Optional<PathMatcher> matcher(boolean useDefaultExcludes);
81+
7082
/**
7183
* {@return in which context the source files will be used}.
7284
* Not to be confused with dependency scope.

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.maven.impl;
2020

2121
import java.nio.file.Path;
22+
import java.nio.file.PathMatcher;
2223
import java.util.List;
2324
import java.util.Objects;
2425
import java.util.Optional;
@@ -41,6 +42,8 @@ public final class DefaultSourceRoot implements SourceRoot {
4142

4243
private final List<String> excludes;
4344

45+
private transient PathMatcher matcher, matcherWithDefaults;
46+
4447
private final ProjectScope scope;
4548

4649
private final Language language;
@@ -142,7 +145,7 @@ public DefaultSourceRoot(final ProjectScope scope, final Language language, fina
142145
* @param directory directory of the source code
143146
* @param includes list of patterns for the files to include, or {@code null} if unspecified
144147
* @param excludes list of patterns for the files to exclude, or {@code null} if unspecified
145-
* */
148+
*/
146149
public DefaultSourceRoot(
147150
final ProjectScope scope,
148151
final Language language,
@@ -183,7 +186,7 @@ public Path directory() {
183186
}
184187

185188
/**
186-
* {@return the list of pattern matchers for the files to include}.
189+
* {@return the list of patterns for the files to include}.
187190
*/
188191
@Override
189192
@SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because unmodifiable
@@ -192,14 +195,36 @@ public List<String> includes() {
192195
}
193196

194197
/**
195-
* {@return the list of pattern matchers for the files to exclude}.
198+
* {@return the list of patterns for the files to exclude}.
196199
*/
197200
@Override
198201
@SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because unmodifiable
199202
public List<String> excludes() {
200203
return excludes;
201204
}
202205

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+
203228
/**
204229
* {@return in which context the source files will be used}.
205230
*/

0 commit comments

Comments
 (0)