From 1326dbe60a4ae7077b252787ae704389d4c8f277 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 4 Apr 2025 08:40:55 +0200 Subject: [PATCH 1/3] [MNG-8673] SourceRoot includes and excludes should be String --- .../java/org/apache/maven/api/SourceRoot.java | 5 +- .../apache/maven/project/MavenProject.java | 4 +- .../apache/maven/impl/DefaultSourceRoot.java | 56 ++++--------------- 3 files changed, 14 insertions(+), 51 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java b/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java index 0b77dbcec513..5ef63caad5aa 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java @@ -19,7 +19,6 @@ package org.apache.maven.api; import java.nio.file.Path; -import java.nio.file.PathMatcher; import java.util.List; import java.util.Optional; @@ -50,7 +49,7 @@ default Path directory() { * The default implementation returns an empty list, which means to apply a language-dependent pattern. * For example, for the Java language, the pattern includes all files with the {@code .java} suffix. */ - default List includes() { + default List includes() { return List.of(); } @@ -59,7 +58,7 @@ default List includes() { * The exclusions are applied after the inclusions. * The default implementation returns an empty list. */ - default List excludes() { + default List excludes() { return List.of(); } diff --git a/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 5e7e48059fc3..758b9936e8c0 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -822,8 +822,8 @@ public boolean add(Resource resource) { private static Resource toResource(SourceRoot sourceRoot) { return new Resource(org.apache.maven.api.model.Resource.newBuilder() .directory(sourceRoot.directory().toString()) - .includes(sourceRoot.includes().stream().map(Object::toString).toList()) - .excludes(sourceRoot.excludes().stream().map(Object::toString).toList()) + .includes(sourceRoot.includes()) + .excludes(sourceRoot.excludes()) .filtering(Boolean.toString(sourceRoot.stringFiltering())) .build()); } diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java index cc24ee6089d6..2962d4ddb6ae 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java @@ -18,9 +18,7 @@ */ package org.apache.maven.impl; -import java.nio.file.FileSystem; import java.nio.file.Path; -import java.nio.file.PathMatcher; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -39,9 +37,9 @@ public final class DefaultSourceRoot implements SourceRoot { private final Path directory; - private final List includes; + private final List includes; - private final List excludes; + private final List excludes; private final ProjectScope scope; @@ -65,9 +63,8 @@ public final class DefaultSourceRoot implements SourceRoot { * @param source a source element from the model */ public DefaultSourceRoot(final Session session, final Path baseDir, final Source source) { - FileSystem fs = baseDir.getFileSystem(); - includes = matchers(fs, source.getIncludes()); - excludes = matchers(fs, source.getExcludes()); + includes = source.getIncludes(); + excludes = source.getExcludes(); stringFiltering = source.isStringFiltering(); enabled = source.isEnabled(); moduleName = nonBlank(source.getModule()); @@ -106,9 +103,8 @@ public DefaultSourceRoot(final Path baseDir, ProjectScope scope, Resource resour throw new IllegalArgumentException("Source declaration without directory value."); } directory = baseDir.resolve(value).normalize(); - FileSystem fs = directory.getFileSystem(); - includes = matchers(fs, resource.getIncludes()); - excludes = matchers(fs, resource.getExcludes()); + includes = resource.getIncludes(); + excludes = resource.getExcludes(); stringFiltering = Boolean.parseBoolean(resource.getFiltering()); enabled = true; moduleName = null; @@ -149,8 +145,8 @@ public DefaultSourceRoot( final ProjectScope scope, final Language language, final Path directory, - List includes, - List excludes) { + List includes, + List excludes) { this.scope = Objects.requireNonNull(scope); this.language = language; this.directory = Objects.requireNonNull(directory); @@ -176,38 +172,6 @@ private static String nonBlank(String value) { return value; } - /** - * Creates a path matcher for each pattern. - * The path separator is {@code /} on all platforms, including Windows. - * The prefix before the {@code :} character is the syntax. - * If no syntax is specified, {@code "glob"} is assumed. - * - * @param fs the file system of the root directory - * @param patterns the patterns for which to create path matcher - * @return a path matcher for each pattern - */ - private static List matchers(FileSystem fs, List patterns) { - final var matchers = new PathMatcher[patterns.size()]; - for (int i = 0; i < matchers.length; i++) { - String rawPattern = patterns.get(i); - String pattern = rawPattern.contains(":") ? rawPattern : "glob:" + rawPattern; - matchers[i] = new PathMatcher() { - final PathMatcher delegate = fs.getPathMatcher(pattern); - - @Override - public boolean matches(Path path) { - return delegate.matches(path); - } - - @Override - public String toString() { - return rawPattern; - } - }; - } - return List.of(matchers); - } - /** * {@return the root directory where the sources are stored}. */ @@ -221,7 +185,7 @@ public Path directory() { */ @Override @SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because unmodifiable - public List includes() { + public List includes() { return includes; } @@ -230,7 +194,7 @@ public List includes() { */ @Override @SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because unmodifiable - public List excludes() { + public List excludes() { return excludes; } From 08a764315d6b6cd812f5b4f064f6797f06726489 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 4 Apr 2025 11:10:52 +0200 Subject: [PATCH 2/3] Improve javadoc --- .../src/main/java/org/apache/maven/api/SourceRoot.java | 5 +++++ .../main/java/org/apache/maven/impl/DefaultSourceRoot.java | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java b/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java index 5ef63caad5aa..b30bfdb0bb73 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java @@ -46,6 +46,11 @@ default Path directory() { /** * {@return the list of pattern matchers for the files to include}. + * The path separator is {@code /} on all platforms, including Windows. + * If the pattern does not start with {@code regex:} or {@code glob:}, the default variation of + * the {@code glob} pattern will be used. + * + *

* The default implementation returns an empty list, which means to apply a language-dependent pattern. * For example, for the Java language, the pattern includes all files with the {@code .java} suffix. */ diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java index 2962d4ddb6ae..0c5f9e54e3ce 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java @@ -140,7 +140,9 @@ public DefaultSourceRoot(final ProjectScope scope, final Language language, fina * @param scope scope of source code (main or test) * @param language language of the source code * @param directory directory of the source code - */ + * @param includes list of patterns for the files to include, or {@code null} if unspecified + * @param excludes list of patterns for the files to exclude, or {@code null} if unspecified + * */ public DefaultSourceRoot( final ProjectScope scope, final Language language, From be17bf9c97d5daf2583e2572b02fff1741f21ab0 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 4 Apr 2025 11:14:32 +0200 Subject: [PATCH 3/3] Specify that patterns are matching relative paths (so no root) --- .../src/main/java/org/apache/maven/api/SourceRoot.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java b/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java index b30bfdb0bb73..8db3be1c2863 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java @@ -47,8 +47,10 @@ default Path directory() { /** * {@return the list of pattern matchers for the files to include}. * The path separator is {@code /} on all platforms, including Windows. - * If the pattern does not start with {@code regex:} or {@code glob:}, the default variation of - * the {@code glob} pattern will be used. + * The patterns are used to match paths relative to the {@code directory}. + * The prefix before the {@code :} character, if present, is the syntax. + * If no syntax is specified, the default is a Maven-specific variation + * of the {@code "glob"} pattern. * *

* The default implementation returns an empty list, which means to apply a language-dependent pattern.