Maven resolves wrong dependency version

2019-08-05 17:27发布

问题:

Our warfile contains not the expected SNAPSHOT-Version of a jarfile, it contains an older release version via another dependency.

Simplified dependency:tree

war-x.x.x-SNAPSHOT
\- jar1-x.x.x-SNAPSHOT
   +- jar2-x.x.x
   |  +- problemjar-x.x.x
   +- jar3-x.x.x-SNAPSHOT

jar3-x.x.x-SNAPSHOT has a dependency "problemjar-x.x.y-SNAPSHOT" (newer version), but the war project build (and the dependency:tree) contains "problemjar-x.x.x" from "jar2-x.x.x".

For now, we "exclude" "problemjar-x.x.x" for "jar2-x.x.x".

But it would be nice to know the reason for this behaviour, IMHO the exclusion is just a workaround.

Notes:

  • The dependency "problemjar-x.x.y-SNAPSHOT" in "jar3-x.x.x-SNAPSHOT" is not "provided".
  • The Maven version is 3.2.5
  • The levels in the simplified dependency:tree above are correct, so the path to the older version is not shorter than the path to the newer SNAPSHOT version

edit: project structure

war and jar1 are children of one parent pom, the simplified dependency:tree is from the war project which has the jar1 project as a dependency. The others are normal/external dependencies, which applies to jar1 too as seen from the war project.

回答1:

Maven does not take the newest version from the dependency tree.

It takes the nearest version, and in your case this is the first it encounters.

If you want to force Maven to take a specific version, it is better to use <dependencyManagement> instead of exclusions.



回答2:

Make sure of the following:

1) Each child module should take it's version from it's parent, i.e. do not have a <version> tag only a <parent> tag that contains a version.

2) When you call the dependency from the same project always do it as follows:

<version>${project.version}</version>