Configurable Dependency Version Resolution Strategy
- Dominant language
- Java
- Stars
- 5.3k
- Forks
- 3.1k
- Avg merge
- 20h 42m
- Merged PRs (30d)
- 297
Description
## Summary
Currently, Maven's resolver treats direct dependencies (declared in a POM) as absolute version requirements that always override transitive dependencies, even when transitive deps require higher versions. This requires extensive use of `` blocks to control resolution. This RFE proposes a configurable mechanism to allow more flexible version resolution while maintaining user control.
## Motivation
- **Current Pain Point**: Developers must explicitly manage many transitive dependencies through `` to avoid version conflicts
- **Flexibility Need**: Different projects have different requirements—some need strict version control, others prefer automatic upgrades to satisfy transitive requirements
- **Control Concern**: Any change to default resolution behavior risks breaking builds unexpectedly (e.g., in reactor builds where in-development versions could be downgraded)
## Proposed Solution
Introduce an optional `directDependencyStrategy` attribute at both project and dependency levels to control how direct dependencies participate in version conflict resolution:
```xml
com.example
lib-a
1.0
```
Or at the individual dependency level:
```xml
com.example
lib-b
2.0
fixed
```
## Strategy Options
- **`fixed` (default)**: Direct dependency version is absolute; always wins version conflicts (current behavior)
- **`flexible`**: Allow the configured version resolver to apply its normal conflict resolution rules (e.g., highest version, nearest) even for direct dependencies, permitting transitive dependencies to override this direct dependency
## Configuration
Strategy default can be controlled at multiple levels (lowest precedence to highest):
1. **Maven Configuration**: `maven.resolver.directDependencyStrategy` property
```xml
flexible
```
2. **Project Level**: `` in POM
3. **Dependency Level**: `` within individual `` block
## Consumer Behavior
When a direct dependency with non-default strategy is consumed as a transitive dependency:
- The `directDependencyStrategy` attribute should be removed/not applied
- Resolution follows normal transitive dependency rules using the active conflict resolver
- Consumer POM controls conflict resolution via their own direct dependency strategies
## Safeguards
- **Build Validation**: Maven warns if unexpected downgrades occur during resolution when using `flexible` strategy
- **Reactor Awareness**: In-development versions in reactor builds always take precedence over deployed versions
- **Explicit Override**: Individual dependencies can override project-level strategy setting
## Benefits
- **Less Configuration**: Developers need fewer `` entries
- **Explicit Intent**: Clear declaration of whether a version is fixed or flexible
- **Gradual Adoption**: Backward compatible; defaults preserve current behavior
- **Flexibility**: Works with any conflict resolver implementation (highest, nearest, etc.)
- **Platform Compatibility**: Works alongside platform/BOM-based dependency management
## Implementation Notes
- Extends `ConfigurableVersionSelector` to check strategy configuration before applying fixed direct dependency preference
- Requires POM model updates to support `directDependencyStrategy` attribute
- Consumer POM transformations should strip `directDependencyStrategy` attributes during publishing
- Strategy resolution follows hierarchy: dependency-level > project-level > Maven property > hardcoded default
Contributor guide
Research direction
Start by reading ConfigurableVersionSelector and the Maven POM model areas mentioned in the issue, then trace consumer POM transformations and the existing conflict-resolution behavior. Done means supporting the stated strategy hierarchy, preserving fixed as the default, handling reactor precedence, and stripping strategy attributes from consumed POMs, with validation for unexpected downgrades.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100