Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Maven is a project management and comprehension tool. It is based on the concept of the project object model, also known as POM. Using the POM as a central piece of information, Maven can manage a project's build, reporting, and documentation.

A big part of Maven is dependency management. Most developers will interact with this feature of Maven while working on an application.

Maven's superior dependency management provides automatic updating as well as dependency closure. Another way companies use Maven for dependency management is by using a custom central repository. By doing so, developers can use their own dependencies on other projects within the company.

In this tutorial, we'll learn how to find Maven dependencies.

2. What Is a Maven Dependency

In the context of Maven, a dependency is simply a JAR file used by a Java application. Based on the POM file, Maven will download and add the JAR file to our Java path. Java will then be able to find and use the classes in the JAR file.

It is also important to note that, Maven has a local repository where it downloads all the dependencies. By default, this is located in the {user home folder}/.m2/repository.

3. POM File

The POM file uses the XML syntax, where everything is between tags.

By default, the POM file is populated only with our project information. In order to add dependencies that our project will use, we need to add the dependencies section:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baeldung</groupId>
    <artifactId>maven.dependency</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        ....
    </dependencies>
</project>

This is only needed when we manually edit the POM file.

4. Ways to Find Maven Dependencies

When working on a new project or adding a new feature, we may realize that we need to add a new dependency to our project. Let's take a simple example where we need to add the JDBC dependency.

Depending on our IDE and setup, there are different ways to find the needed details for the JDBC dependency.

4.1. IntelliJ

If we use IntelliJ IDEA, we can add a new dependency to the project's POM by following a few steps.

Firstly, we open the POM file, then press ALT+INSERT and then click on the DEPENDENCY option:IntelliJ Add Dependency Context Menu

Then, we can search for the needed dependency and click ADD:IntelliJ Dependency Search

4.2. Eclipse

Eclipse IDE has a similar way to IntelliJ for adding a new dependency.

We need to right-click on the POM file in the package explorer or after opening the file, and then we go to Maven -> Add dependency option:

Eclipse Maven Context MenuThen, we can search for the needed dependency and click OK:Eclipse Dependency Search

4.3. Internet Search

If we are ok with manually editing the POM file, then we can simply search on search.maven.org or Google to find all the details of the dependency.

When going to search.maven.org, we can simply type the dependency in the search bar, and we'll find plenty of versions of the dependency. Unless there are other restrictions in our project, we should use the latest stable version:

In our POM file, in the dependency section, simply paste the dependency details found.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.21</version>
</dependency>

There are also plenty of other websites similar to search.maven.org that we can use and explore.

5. Conclusion

In this tutorial, we saw the different ways to add a Maven dependency to our project.

We also learned how to edit the POM file in order to get Maven to download and use the dependency added.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Maven footer banner
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!