Skip to content

Commit 85a49a0

Browse files
desruisseauxPankraz76
authored and
Vincent Potucek
committed
[MNG-8686] Add SourceRoot.matcher(boolean) method (apache#2236)
The matcher returned by that method combines the effects of all includes and excludes. When using the Maven syntax, escape the special characters [ ] { } \ before to delegate to the glob syntax. Optimization: omit excludes that are unnecessary because they will never match a file accepted by includes. This is especially useful when the default excludes are added, because there is a lot of them. --------- Co-authored-by: VIP <8830888+Pankraz76@users.noreply.github.com>
1 parent 1c1a395 commit 85a49a0

File tree

4 files changed

+760
-11
lines changed

4 files changed

+760
-11
lines changed

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

+25-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.maven.api;
2020

2121
import java.nio.file.Path;
22+
import java.nio.file.PathMatcher;
23+
import java.util.Collection;
2224
import java.util.List;
2325
import java.util.Optional;
2426

@@ -45,30 +47,47 @@ default Path directory() {
4547
}
4648

4749
/**
48-
* {@return the list of pattern matchers for the files to include}.
50+
* {@return the list of patterns for the files to include}.
4951
* The path separator is {@code /} on all platforms, including Windows.
50-
* The patterns are used to match paths relative to the {@code directory}.
51-
* The prefix before the {@code :} character, if present, is the syntax.
52-
* If no syntax is specified, the default is a Maven-specific variation
53-
* of the {@code "glob"} pattern.
52+
* The prefix before the {@code :} character, if present and longer than 1 character, is the syntax.
53+
* If no syntax is specified, or if its length is 1 character (interpreted as a Windows drive),
54+
* the default is a Maven-specific variation of the {@code "glob"} pattern.
5455
*
5556
* <p>
5657
* The default implementation returns an empty list, which means to apply a language-dependent pattern.
5758
* For example, for the Java language, the pattern includes all files with the {@code .java} suffix.
59+
*
60+
* @see java.nio.file.FileSystem#getPathMatcher(String)
5861
*/
5962
default List<String> includes() {
6063
return List.of();
6164
}
6265

6366
/**
64-
* {@return the list of pattern matchers for the files to exclude}.
67+
* {@return the list of patterns for the files to exclude}.
6568
* The exclusions are applied after the inclusions.
6669
* The default implementation returns an empty list.
6770
*/
6871
default List<String> excludes() {
6972
return List.of();
7073
}
7174

75+
/**
76+
* {@return a matcher combining the include and exclude patterns}.
77+
* If the user did not specify any includes, the given {@code defaultIncludes} are used.
78+
* These defaults depend on the plugin.
79+
* For example, the default include of the Java compiler plugin is <code>"**&sol;*.java"</code>.
80+
*
81+
* <p>If the user did not specify any excludes, the default is often files generated
82+
* by Source Code Management (<abbr>SCM</abbr>) software or by the operating system.
83+
* Examples: <code>"**&sol;.gitignore"</code>, <code>"**&sol;.DS_Store"</code>.</p>
84+
*
85+
* @param defaultIncludes the default includes if unspecified by the user
86+
* @param useDefaultExcludes whether to add the default set of patterns to exclude,
87+
* mostly Source Code Management (<abbr>SCM</abbr>) files
88+
*/
89+
PathMatcher matcher(Collection<String> defaultIncludes, boolean useDefaultExcludes);
90+
7291
/**
7392
* {@return in which context the source files will be used}.
7493
* Not to be confused with dependency scope.

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

+23-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.maven.impl;
2020

2121
import java.nio.file.Path;
22+
import java.nio.file.PathMatcher;
23+
import java.util.Collection;
2224
import java.util.List;
2325
import java.util.Objects;
2426
import java.util.Optional;
@@ -140,9 +142,9 @@ public DefaultSourceRoot(final ProjectScope scope, final Language language, fina
140142
* @param scope scope of source code (main or test)
141143
* @param language language of the source code
142144
* @param directory directory of the source code
143-
* @param includes list of patterns for the files to include, or {@code null} if unspecified
144-
* @param excludes list of patterns for the files to exclude, or {@code null} if unspecified
145-
* */
145+
* @param includes patterns for the files to include, or {@code null} or empty if unspecified
146+
* @param excludes patterns for the files to exclude, or {@code null} or empty if nothing to exclude
147+
*/
146148
public DefaultSourceRoot(
147149
final ProjectScope scope,
148150
final Language language,
@@ -183,7 +185,7 @@ public Path directory() {
183185
}
184186

185187
/**
186-
* {@return the list of pattern matchers for the files to include}.
188+
* {@return the patterns for the files to include}.
187189
*/
188190
@Override
189191
@SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because unmodifiable
@@ -192,14 +194,30 @@ public List<String> includes() {
192194
}
193195

194196
/**
195-
* {@return the list of pattern matchers for the files to exclude}.
197+
* {@return the patterns for the files to exclude}.
196198
*/
197199
@Override
198200
@SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because unmodifiable
199201
public List<String> excludes() {
200202
return excludes;
201203
}
202204

205+
/**
206+
* {@return a matcher combining the include and exclude patterns}.
207+
*
208+
* @param defaultIncludes the default includes if unspecified by the user
209+
* @param useDefaultExcludes whether to add the default set of patterns to exclude,
210+
* mostly Source Code Management (<abbr>SCM</abbr>) files
211+
*/
212+
@Override
213+
public PathMatcher matcher(Collection<String> defaultIncludes, boolean useDefaultExcludes) {
214+
Collection<String> actual = includes();
215+
if (actual == null || actual.isEmpty()) {
216+
actual = defaultIncludes;
217+
}
218+
return new PathSelector(directory(), actual, excludes(), useDefaultExcludes).simplify();
219+
}
220+
203221
/**
204222
* {@return in which context the source files will be used}.
205223
*/

0 commit comments

Comments
 (0)