Maven set dependency mediation strategy to newest rather than nearest

流过昼夜 提交于 2019-11-27 17:57:48

问题


Can I configure Maven to choose the "newest" dependency on a conflict, rather than the "nearest"?

The "newest" is the default in Ivy and other sensible dependency managers, see http://ant.apache.org/ivy/history/2.2.0/settings/conflict-managers.html

I find the "nearest" strategy is rarely what I want.

I'm using Maven 3.3.3, but I can switch versions if necessary.

I know how to override Maven's choice on individual conflicts, but I'd prefer to change the default so that I don't have to detect and fix each conflict one at a time.

(See the Maven docs on "Dependency mediation")


回答1:


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:

  1. 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.
  2. 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:

  1. 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.
  2. Migrate to another build tool: Obviously.



回答2:


You can also use the "requireUpperBoundDeps" rule for the Maven "enforcer" plugin, which will not directly implement a "newest wins" conflict resolution policy, but will enforce that the end result is the same. You will need to manually add transitive dependency <exclusions> or <dependencyManagement> rules to your POM to choose the newest dependency in each conflict, but at least you will then have confidence that the end result is "newest wins".

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
      <execution>
        <id>enforce</id>
        <configuration>
          <rules>
            <requireUpperBoundDeps />
          </rules>
        </configuration>
        <goals>
          <goal>enforce</goal>
        </goals>
      </execution>
    </executions>
  </plugin>


来源:https://stackoverflow.com/questions/34201120/maven-set-dependency-mediation-strategy-to-newest-rather-than-nearest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!