Can I configure Maven to automatically use the "newest" version of a dependency instead of the "nearest" when resolving version conflicts?
No, you cannot configure Maven's dependency mediation strategy to anything other than nearest.
Adding configurable dependency mediation strategies has been proposed before, but was ultimately abandoned because the proposal involved changing the POM XSD, which has not happened in years.
Why does Maven use the nearest strategy as a default?
The strategy of nearest is favored by Maven for two reasons:
- Easy overriding of individual conflicts: For any particular conflicting
dependency, you can specify its version within your own POM, and that version becomes the nearest.
- Reproducible builds: Version ranges anywhere in your dependency graph can cause builds to not be reproducible. A mediation strategy of "newest" would magnify the negative impact of version ranges on build reproducibility.
But I really want a different dependency mediation strategy. What can I do?
These are your best options:
- Make a Maven extension: Usage of the "nearest" strategy is specified by
NearestVersionSelector
in MavenRepositorySystemUtils
. You could create your own Maven extension that defines your own VersionSelector
that implements the strategy of your choice, then in the afterSessionStart
method of your extension, replace the session's DependencyGraphTransformer
with one that uses your custom VersionSelector
.
- Migrate to another build tool: Obviously.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…