Middle
What is the purpose of the dependency management section in Maven?
sobes.tech AI
Answer from AI
The <dependencyManagement> section in Maven is used for centralized management of dependency versions across a project hierarchy.
Main reasons for its use:
- Version consistency: Ensures that all modules inheriting from the parent
pom.xmlwith the<dependencyManagement>section will use the same version of a dependency. This prevents version conflicts and simplifies updates. - Elimination of duplication: By defining a dependency version once in the parent POM, child modules can simply specify
<groupId>and<artifactId>, without repeating<version>. - Simplified management: Changing the dependency version in one place (the parent POM) propagates to all child modules using that dependency.
- Control over transitive dependencies: Helps manage versions of transitive dependencies that may be brought in explicitly declared dependencies.
It's important to understand the difference between <dependencies> and <dependencyManagement>:
<dependencies>: Declares dependencies that are necessary for the module. These dependencies will be included during the build.<dependencyManagement>: Declares potential dependencies and their versions, but does not automatically include them in the build. Modules must explicitly declare the dependency in their<dependencies>section to use it.
Example of usage in a parent POM:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Example of usage in a child POM:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!-- Version will be taken from the parent's dependencyManagement -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<!-- Version and scope will be taken from the parent's dependencyManagement -->
</dependency>
</dependencies>