Open In App

How Maven Searches for Dependencies for a Project?

Improve
Improve
Like Article
Like
Save
Share
Report

Maven is a build automation tool that is used for java projects for various activities like dependency management, building the project, documentation, unit testing, etc provided by Apache. One of the key activities of Maven is Dependency management because it’s one of the most difficult jobs for developers to manage hundreds of dependencies and artifacts for multiple projects. Maven manages and gets these dependencies from repositories. There are mainly three types of repositories.

Repositories Available in Maven

1. Local Repository

This repository is located on your local workstation, and it’s available on the C:\Users\<user_name>\.m2\repository for windows, the path may vary for other operating systems like MAC, Linux, etc. When we build a Maven project, all the required dependency jar files into this folder, and further reuse the same jars for other projects which requires the same dependencies. 

Settings.xml File:

XML




<?xml version="1.0" encoding="UTF-8"?>
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">
   <localRepository>${user.home}/.m2/repository</localRepository>
   <interactiveMode>true</interactiveMode>
   <offline>false</offline>
   ...
</settings>


2. Central Repository

This is a remote repository available over the internet, whenever a dependency or artifact is not found in the local repository maven triggers a request to this central repository and downloads the dependencies to the local repository. This repository is provided by the Apache Maven group. The url for this central repo is listed as https://repo.maven.apache.org/. There are multiple mirrors available for the central repo, one can make changes on the settings.xml file in maven to use a particular mirror that is officially listed. Some dependency uploads can also be made to the central repo after gaining some permissions.

Settings.xml File:

XML




<?xml version="1.0" encoding="UTF-8"?>
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">
   ...
   <mirrors>
      <mirror>
         <id>centeal repo</id>
         <name>repo_name</name>
         <url>maven2</url>
         <mirrorOf>https://repo.maven.apache.org/central</mirrorOf>
      </mirror>
   </mirrors>
   ...
</settings>


3. Internal Repository

These repositories are maintained by the organization where downloads from external sites are prohibited, the organizations can add the details of these internal repositories in the settings.xml file. These repositories are private to the respective organizations. The libraries which are missing on the central repository are added here. All the artifacts or project references that are developed by the organization exist here. The dependencies required are loaded from here and directly to the local repository. If maven can’t find the required dependency from any of the following sources, it throws an error stating “cannot resolve dependency”. To add a new internal remote URL

XML




<?xml version="1.0" encoding="UTF-8"?>
<repositories>
   <repository>
      <id>repo_id</id>
      <url>//for example geeksforgeeks.com</url>
   </repository>
</repositories>


 

Here the third local machine has restricted access to external sites, so it uses the internal repo to find the dependencies.



Last Updated : 19 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads