-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Summary
The current PathSelector implementation in Maven 4 handles POSIX ** semantics (where ** matches 0 or more directories vs Java's 1 or more) by generating multiple patterns using the addPatternsWithOneDirRemoved method. This approach works but could be simplified and potentially optimized by leveraging Java's built-in brace expansion feature.
Current Implementation
The current approach in PathSelector.addPatternsWithOneDirRemoved() (lines 464+) generates multiple glob patterns by recursively removing ** patterns to simulate POSIX behavior:
// For pattern "src/**/test/**/*.java", generates:
// 1. "src/**/test/**/*.java" (original)
// 2. "src/**/test/*.java" (second ** removed)
// 3. "src/test/**/*.java" (first ** removed)
// 4. "src/test/*.java" (both ** removed)Proposed Improvement
Replace the combinatorial pattern generation with brace expansion:
// Transform: "src/**/test/**/*.java"
// Into: "src/{**/,}test/{**/,}*.java"
String posixPattern = globPart.replaceAll("\\*\\*/", "{**/,}");Java's PathMatcher with glob syntax natively supports brace expansion and will automatically generate all the same combinations, but potentially more efficiently.
Benefits
- Simpler Code: Reduces ~40 lines of complex recursive logic to ~3 lines
- Better Performance: Leverages Java's optimized brace expansion instead of multiple matchers
- More Maintainable: Easier to understand and debug
- Leverages Platform: Uses built-in capabilities instead of reimplementing them
Compatibility
This change would be:
- ✅ Functionally identical: Same matching behavior
- ✅ Backward compatible: No API changes
- ✅ Performance neutral or better: Single optimized matcher vs multiple matchers
Implementation
The change would be in the PathSelector.addPatternsWithOneDirRemoved() method, replacing the recursive pattern generation with a simple regex replacement that transforms **/ into {**/,}.
Related Work
- This builds on the excellent PathSelector improvements in Port the bug fixes identified when using that class in Maven clean and compiler plugin. #10909 and Add PathMatcherFactory service with directory filtering optimization #10923
- Similar pattern transformation approaches are used successfully in other build tools
- The brace expansion feature is well-established in Java's glob implementation
Testing
The existing comprehensive test suite in PathSelectorTest would validate that the behavior remains identical while the implementation becomes simpler.
This improvement would make Maven's PathSelector implementation more elegant while maintaining full compatibility and potentially improving performance.