Suggested Pages

Sunday, May 13, 2012

Maven Dependency Mediation vs Dependency Management

Transitive Dependencies

Maven manages transitive dependencies with a mechanism to discover and include dependencies automatically.
Maven first reads the dependencies inside the pom.xml of your project and then it navigates the pom.xml of those dependencies with no limit in the number of levels analyzed. Problems are only caused by cyclic dependency.
To manage this automatic mechanism of discovering transitive dependencies , you can use a specific area inside the pom.xml intended for dependencies.
These sections are: <dependencymanagement> and <dependencies>.

In the next part of this post we'll see the difference between two techniques used to control dependencies: Dependency Mediation and Dependency Management.

Dependency Mediation

Dependency Mediation determines which version of the dependencies will be used in a situation where, in the same project there are different versions of the same artifact. Maven 2.0 only supports the mechanism of the "nearest definition" which means it will be used only version dell'artifact whose dependency is closest to the project in the tree of dependencies. As an example, if the arrows indicate the dependencies between an artifact and the other:
  • A-> B-> C-> D (2.0)
  • A-> E-> D (1.0) WINNER
Wins the version 1.0 of D for the build of the artifact A, because the path from A to D is shorter passing through E rather than through B. If you want to change this behavior would simply add the dependence D (version 2.0) to A to force the use of D with that version.

Dependency Management

The Dependency Management provides:
  1. Consolidate and centralize the management of versions of the dependencies without the addition of dependencies that are inherited by all children. This is particularly useful when you have a number of projects (ie more than one) who inherits a common parent.
  2. Having control over the versions of artifacts used in the transitive dependencies
  3. Define a standard version of an artifact to be used on multiple projects
  4. Allows you to specify, for a certain project, the version of the artifact to be used when it is encountered in the transitive dependencies or independencies where the version has not been defined. Given a project A, it may include D as a dependency (in the Dependency Management) in order to check the version of D in the case it should be be used

Dependency Mediation vs Dependency Management

The Dependency Management has priority over Dependency Mediation , but only for transitive dependencies. If the dependence is not transitive but is direct, it's taken into account the direct one.

No comments :

Post a Comment

Suggested Pages