Skip to content

Improve PathSelector to use brace expansion for POSIX ** semantics instead of combinatorial approach #11110

@gnodet

Description

@gnodet

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

  1. Simpler Code: Reduces ~40 lines of complex recursive logic to ~3 lines
  2. Better Performance: Leverages Java's optimized brace expansion instead of multiple matchers
  3. More Maintainable: Easier to understand and debug
  4. 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

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions