Show List

Dependency management with Maven

Dependency management is a key feature of Maven that simplifies the management of project dependencies. In Maven, a dependency is a Java library or module that a project requires in order to compile and run. Maven handles dependency management by automatically downloading and managing project dependencies from remote repositories.

To specify a dependency in a Maven project, you add a <dependency> element to the project's POM file, specifying the group ID, artifact ID, and version of the dependency. For example, to include the Apache Commons Lang library in your project, you would add the following to your POM file:

php
Copy code
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>

When you build your project with Maven, it will automatically download the Commons Lang library from a remote repository (such as Maven Central) and add it to your project's classpath.

Maven also supports dependency scopes, which control the visibility of dependencies in different parts of the build process. For example, the "compile" scope indicates that a dependency is required for both compilation and execution, while the "test" scope indicates that a dependency is only required for running tests.

Here's an example of a dependency with a "test" scope:

php
Copy code
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>

This specifies that the JUnit library is only required for running tests, and should not be included in the final packaged artifact.


    Leave a Comment


  • captcha text