Skip to content

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Jul 17, 2025

Backport

This will backport the following commits from master to maven-4.0.x:

Questions ?

Please refer to the Backport tool documentation

…pache#10923)

This PR adds a comprehensive PathMatcherFactory service to Maven 4 API with
directory filtering optimization capabilities, addressing the need for
exclude-only pattern matching and performance optimizations.

## New API Features

### PathMatcherFactory Interface
- createPathMatcher(baseDirectory, includes, excludes, useDefaultExcludes)
- createPathMatcher(baseDirectory, includes, excludes) - convenience overload
- createExcludeOnlyMatcher(baseDirectory, excludes, useDefaultExcludes)
- createIncludeOnlyMatcher(baseDirectory, includes) - convenience method
- deriveDirectoryMatcher(fileMatcher) - directory filtering optimization

### DefaultPathMatcherFactory Implementation
- Full implementation of all PathMatcherFactory methods
- Delegates to PathSelector for actual pattern matching
- Provides directory optimization via PathSelector.couldHoldSelected()
- Fail-safe design returning INCLUDES_ALL for unknown matcher types

## PathSelector Enhancements

### Null Safety Improvements
- Added @nonnull annotation to constructor directory parameter
- Added Objects.requireNonNull() validation with descriptive error message
- Moved baseDirectory assignment to beginning for fail-fast behavior
- Updated JavaDoc to document NullPointerException behavior

### Directory Filtering Support
- Added canFilterDirectories() method to check optimization capability
- Made INCLUDES_ALL field package-private for factory reuse
- Enhanced couldHoldSelected() method accessibility

## Directory Filtering Optimization

The deriveDirectoryMatcher() method enables significant performance improvements
by allowing plugins to skip entire directory trees when they definitively
won't contain matching files. This preserves Maven 3's optimization behavior.

### Usage Example:

## Comprehensive Testing

### DefaultPathMatcherFactoryTest
- Tests all factory methods with various parameter combinations
- Verifies null parameter handling (NullPointerException)
- Tests directory matcher derivation functionality
- Includes edge cases and fail-safe behavior verification

### Backward Compatibility
- All existing PathSelector functionality preserved
- No breaking changes to existing APIs
- Enhanced error handling with better exception types

## Benefits

1. **Plugin Compatibility**: Enables maven-clean-plugin and other plugins
   to use exclude-only patterns efficiently
2. **Performance**: Directory filtering optimization preserves Maven 3 behavior
3. **Developer Experience**: Clean service interface with comprehensive JavaDoc
4. **Robustness**: Fail-fast null validation and defensive programming
5. **Future-Proof**: Extensible design for additional pattern matching needs

## Related Work

This implementation complements PR apache#10909 by @desruisseaux which addresses
PathSelector bug fixes. Both PRs can be merged independently and work
together to provide complete exclude-only functionality.

Addresses performance optimization suggestions and provides the missing
API methods needed by Maven plugins for efficient file filtering.

(cherry picked from commit 38e0a71)
@gnodet gnodet merged commit 591d5ad into apache:maven-4.0.x Jul 18, 2025
19 checks passed
@github-actions github-actions bot added this to the 4.0.0 milestone Jul 18, 2025
@gnodet gnodet added the enhancement New feature or request label Jul 18, 2025
@gnodet gnodet deleted the backport/maven-4.0.x/pr-10923 branch July 18, 2025 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport enhancement New feature or request mvn40

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant