View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.impl;
20  
21  import java.nio.file.Path;
22  import java.nio.file.PathMatcher;
23  import java.util.Collection;
24  import java.util.Objects;
25  
26  import org.apache.maven.api.annotations.Nonnull;
27  import org.apache.maven.api.di.Named;
28  import org.apache.maven.api.di.Singleton;
29  import org.apache.maven.api.services.PathMatcherFactory;
30  
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * Default implementation of {@link PathMatcherFactory} that creates {@link PathSelector}
35   * instances for filtering files based on include/exclude patterns.
36   * <p>
37   * This implementation provides Maven's traditional include/exclude pattern behavior,
38   * compatible with Maven 3 plugins like maven-compiler-plugin and maven-clean-plugin.
39   *
40   * @since 4.0.0
41   */
42  @Named
43  @Singleton
44  public class DefaultPathMatcherFactory implements PathMatcherFactory {
45  
46      @Nonnull
47      @Override
48      public PathMatcher createPathMatcher(
49              @Nonnull Path baseDirectory,
50              Collection<String> includes,
51              Collection<String> excludes,
52              boolean useDefaultExcludes) {
53          requireNonNull(baseDirectory, "baseDirectory cannot be null");
54  
55          return PathSelector.of(baseDirectory, includes, excludes, useDefaultExcludes);
56      }
57  
58      @Nonnull
59      @Override
60      public PathMatcher createExcludeOnlyMatcher(
61              @Nonnull Path baseDirectory, Collection<String> excludes, boolean useDefaultExcludes) {
62          return createPathMatcher(baseDirectory, null, excludes, useDefaultExcludes);
63      }
64  
65      @Nonnull
66      @Override
67      public PathMatcher deriveDirectoryMatcher(@Nonnull PathMatcher fileMatcher) {
68          if (Objects.requireNonNull(fileMatcher) instanceof PathSelector selector) {
69              if (selector.canFilterDirectories()) {
70                  return selector::couldHoldSelected;
71              }
72          }
73          return PathSelector.INCLUDES_ALL;
74      }
75  
76      @Nonnull
77      @Override
78      public PathMatcher includesAll() {
79          return PathSelector.INCLUDES_ALL;
80      }
81  }