More on Dependencies
This lesson continues the discussion on dependencies and the various mechanisms available to resolve conflicting versions of a dependency.
Dependency conflict resolution
So, what happens when a project depends on two projects which in turn depend on different versions of the same project? How does Maven solve this conflict? Consider the following setup:
In the above setup, Project A depends on Project B and Project C. Both Project B and Project C in turn depend on the Google artifact for json parsing known as Gson. However, Project B depends on the newer version 2.8.6, while Project C depends on the older 1.7.2 version. Let’s do a hands-on exercise and see which version Maven picks.
# Change directory to Project9cd /Project9# Change directory to ProjectB and build it so that it is available in the# local Maven repo for ProjectA to use.cd ProjectBmvn install# Change directory to ProjectC and build it.cd ../ProjectCmvn install# Change directory to ProjectA and compute the dependency tree# You can also use resolvee goal to view the dependency resolution by Maven.cd ../ProjectAmvn dependency:tree -Dverbosemvn dependency:resolve
In the example above, Maven ends up choosing the version 2.8.6 for the Gson dependency. If we draw a tree of dependencies starting with the POM of Project A as the root, Maven will choose the dependency nearest from the root in case of conflicting dependency versions. Let’s see the dependency tree of Project A as printed out by Maven.
Get hands-on with 1400+ tech skills courses.