In this tutorial, you'll learn how to manage dependencies in a Maven project by adding, updating, and removing them. Dependencies are external libraries or components your project requires to function correctly. Maven simplifies dependency management by automatically downloading and managing them for you.
Prerequisites
mvn -v in your command prompt or terminal, and the version information should be displayed.1. Add a dependency
To add a dependency, you need to know its Maven coordinates (i.e., groupId, artifactId, and version). These coordinates can be found on the project's website, source code repository, or a public Maven repository such as Maven Central.
Once you have the Maven coordinates, add the dependency to your project's pom.xml file, within the <dependencies> section:
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> </dependency> </dependencies>
Replace the groupId, artifactId, and version with the coordinates of the library you want to add.
2. Update a dependency
To update a dependency, change the version element in the <dependency> section in your pom.xml file. Maven will automatically download the new version when building your project.
For example, if you want to update the example-library to version 1.1.0, change the version element like this:
<dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.1.0</version> </dependency>
3. Remove a dependency
To remove a dependency from your project, simply delete the corresponding <dependency> section from the pom.xml file.
For example, to remove the example-library dependency, delete the following section:
<dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.1.0</version> </dependency>
4. Scope (optional)
Maven allows you to define the scope of a dependency, which determines when and how it's used in your project. The most commonly used scopes are compile, provided, runtime, test, and system.
compile (default): The dependency is required for compiling and running the project. It will be included in the final build artifact (e.g., JAR, WAR).provided: The dependency is needed for compilation, but not for runtime. It's assumed that the dependency will be provided by the runtime environment (e.g., a web application server).runtime: The dependency is not needed for compilation, but it's required at runtime.test: The dependency is only needed for compiling and running tests. It won't be included in the final build artifact.system: Similar to provided, but the dependency is expected to be available on the local system and is not fetched from a repository. The dependency's path must be specified using the <systemPath> element.How to declare dependencies in Maven:
pom.xml file to specify external libraries required for the project.<dependencies> section:<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> </dependency> </dependencies>
Managing version conflicts in Maven dependencies:
dependencyManagement section to enforce versions:<dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>conflict-artifact</artifactId> <version>2.0.0</version> </dependency> </dependencies> </dependencyManagement>
Scoping and packaging of Maven dependencies:
<dependencies> section:<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <scope>compile</scope> </dependency> </dependencies>
<build> section.Excluding specific dependencies in Maven:
<dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>unwanted-group</groupId> <artifactId>unwanted-artifact</artifactId> </exclusion> </exclusions> </dependency>
Working with system and provided scope in Maven:
system and provided scopes are used for system-specific or provided dependencies.<dependency> <groupId>com.example</groupId> <artifactId>system-artifact</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/system-artifact.jar</systemPath> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>provided-artifact</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> android-json mysql-json controllers stripe-payments persistent combinatorics webm qfiledialog jwk horizontal-scrolling