Maven Tutorial
Maven in IDE
In this tutorial, you'll learn how to use the dependencyManagement
section in a Maven project to centralize and manage dependencies across multiple projects or modules. This feature is particularly useful when working with multi-module projects, as it allows you to maintain consistent dependency versions throughout your project.
Prerequisites
mvn -v
in your command prompt or terminal, and the version information should be displayed.1. Understanding dependencyManagement
dependencyManagement
is a section in the pom.xml
file that allows you to declare and manage dependencies in a centralized manner. When you declare a dependency in the dependencyManagement
section, you're not actually adding it to your project. Instead, you're specifying the versions and other properties of the dependencies, which can be referenced in your project's dependencies
section or inherited by child modules.
2. Add dependencyManagement to your pom.xml
To add a dependencyManagement
section to your pom.xml
, insert the following code snippet inside the <project>
element:
<dependencyManagement> <dependencies> <!-- Declare dependencies here --> </dependencies> </dependencyManagement>
3. Declare dependencies in dependencyManagement
To declare a dependency in dependencyManagement
, you need to know its Maven coordinates (i.e., groupId
, artifactId
, and version
). Add the dependency as a child of the <dependencies>
element inside dependencyManagement
:
<dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> </dependency> </dependencies> </dependencyManagement>
You can add multiple dependencies in the dependencyManagement
section.
4. Reference dependencies in your project or modules
Once you have declared a dependency in the dependencyManagement
section, you can reference it in your project's dependencies
section or in the dependencies
section of your child modules:
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> </dependency> </dependencies>
Notice that you don't have to specify the version, as it is inherited from the dependencyManagement
section. If you need to override the version in a specific module, you can still specify a different version in the dependencies
section of that module.
5. Managing dependency scope and exclusions
In addition to managing dependency versions, you can also manage the scope and exclusions of your dependencies in the dependencyManagement
section. For example:
<dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> <scope>runtime</scope> <exclusions> <exclusion> <groupId>com.example</groupId> <artifactId>unnecessary-library</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement>
In this example, the example-library
dependency has a scope of runtime
, and an exclusion rule is added to remove the unnecessary-library
dependency.
In conclusion, using dependencyManagement
in your Maven project allows you to centralize and manage dependency versions, scopes, and exclusions across your entire project.
Maven dependencyManagement in project configuration:
dependencyManagement
in Maven allows centralized version control for project dependencies.<project> <dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> </dependency> <!-- Add other dependencies --> </dependencies> </dependencyManagement> </project>
Overriding versions in Maven with dependencyManagement:
dependencyManagement
.<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <!-- Version overridden at the project level --> <version>2.0.0</version> </dependency> </dependencies>
Using property expressions in Maven dependencyManagement:
<properties> <example.version>1.0.0</example.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>${example.version}</version> </dependency> </dependencies> </dependencyManagement>