In Maven, dependency is another archive—JAR, ZIP, and so on—which your current project needs in order to compile, build, test, and/or to run. The dependencies are gathered in the pom.xml file, inside of a <dependencies> tag.
Maven does the dependency management using the concept of Repositories.
Keeping your project up-to-date with the correct versions of external JAR files can be a comprehensive task. Maven has built-in dependency management. You specify in the POM file what external libraries your project depends on, and which version, and then Maven downloads them for you and puts them in your local Maven repository.
Different types of dependencies:
- External Dependencies: An external dependency in Maven is a dependency (JAR file) which is not located in a Maven repository (neither local, central or remote repository).
- Snapshot Dependencies: Snapshot dependencies are dependencies (JAR files) which are under development. Instead of constantly updating the version numbers to get the latest version, you can depend on a snapshot version of the project.
- Transitive Dependencies: If your project depends on a dependency, say Dependency A, and Dependency A itself depends on another dependency, say Dependency X, then your project has a transitive dependency on Dependency X. Required transitive dependencies are automatically included in our project by Maven.
- Exclude Dependencies: Sometimes the direct dependencies of your project may clash with the transitive dependencies of the direct dependencies. With the dependency exclusion declaration in place, whatever version of the excluded dependency that the dependency containing the exclusion is using, will be ignored during Maven’s compilation of the project.
Steps to add a dependency in Maven project:
1) Go to mvnrepository.
2) Search for the dependency which you would like to add in your project. For example: selenium-java
3) Click on the first link from the search results. Ex – Selenium Java
4) Click on the latest version of the dependency. Ex – 3.141.59
5) Copy the dependency details and paste it in your project pom.xml.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Demo-Test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> </dependencies> </project>