Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Maven Repositories and its Types

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

We have a plethora of technology in the field of development. Every one of them has its own importance. If you are a developer, you must have heard about Apache Maven. Do you know Apache Maven has become one of the top choices of developers? How is it better than all other techs, then? Well, it is because of its repo.

maven repos and its types

This article will discuss the Maven repository and its types. The types of Maven include the local repo, central repo and remote repo. We will also see how Maven searches for repos and how the repo manager works. So without any further ado, let’s get started!! 

What is Maven?

Maven is a platform-independent software development tool. It is used for managing dependencies between projects. It creates POM files that can be used by many JVM-based tools such as Eclipse, STS, and Bitbucket. It requires POM files in the Java ecosystem.

what is maven

Where is Maven used?

Maven is generally used where JVM-based software is built. We can find Maven while building and integrating scripts and deploying pipelines.

It is used by teams of different sizes worldwide to compile software into JAR files. It also provides reports and docs and automates various other processes.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Maven Repository

Maven is simply a collection of cached jars. For any project, Maven stores the project's JAR files, library JAR files, plugins, and other project-specific assets. Maven uses POM files to store meta-data.

maven repo

Maven repos are of three types:

  • Maven local repo
  • Maven central repo
  • Maven remote repo

Maven Local Repository

Maven local repo refers to a directory on your computer. A local repo is generated when you run any maven command for the first time. On your system, Maven local repo keeps all dependency library jars, plugin jars, and so on. When Maven downloads dependency jars, the jar files are saved in the local Maven repository.

If the version stated in the dependent portion of the pom.xml file already exists in the local Maven repository, it is used directly. Otherwise, Maven will download a newer version.

Maven generates the local repo by default in the %USER HOME% directory.

We can find the Maven local repo folder in the Maven settings.xml file, which is located in the %M2_HOME%conf directory. If you cannot find it, you can create it.

creating local_repo file

XML files will be imported from various websites, such as maven.apache.org (Apache Maven Website) and w3.org(World Wide Web Consortium).

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>C:/MyLocalRepository</localRepository>
</settings>
local repo.xml

After running the above command, the repo folder will be configured. Afterwards, maven will download dependencies to the specified path.

Maven Central Repository

The Apache Maven community creates the Maven central repo. It includes a large number of regularly used libraries. Maven searches in this central repo by default for any dependencies that are required but are not found in your local repo.

The path of the Maven central repo is http://repo1.maven.org/maven2/ 

You can search for dependencies in Marvel central repo search at https://search.maven.org/

The Maven central repo search webpage looks like this:

maven central repo search

Maven Remote Repository

We may need to set up a Maven repository within a company or project development team to host our own libraries. The company manages the Maven remote repo, which is located outside the developer's workstation.

remote repo

The following pom.xml defines dependencies as well as the URL of the remote repo.

<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>com.codingninjas.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.codingninjas.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>codingninjas.lib1</id>
         <url>http://download.codingninjas.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.codingninjas.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>
remote repo pom file

How Maven Searches for Dependencies

When maven begins performing the build commands, it begins searching for dependencies.

maven search dependencies

It is done as follows:

  • It searches the local repos for any set of dependencies. If it is discovered, the execution proceeds. It searches the central repo if the configured dependencies cannot be located in the local repo.
     
  • If the required dependencies are identified in the central repo, they are downloaded to the local repo for future use and reference. If no matches are discovered, Maven begins scanning the remote repos.
     
  • If no remote repo is configured, maven will throw an exception stating that it could not locate the dependencies and will stop processing. If any dependencies are discovered, they are downloaded to the local repo for future reference and use.

Maven Repository Manager

A Maven repository manager is a server program that is dedicated to managing repos. A repository manager is regarded as an essential best practice for any serious Maven usage.

maven repo manager

Some properties of Repository Manager are:

  • One of the proxy servers for public Maven repos is Repository Manager.
     
  • The repos can now be used as a destination for Maven project outputs.
     
  • Repository Manager lowers the complexity of downloading remote repos. It then results in less time consumption and improved build performance.

 

Must Read Apache Server

Frequently Asked Questions

What are some features of Maven?

Maven has robust reporting for errors and integrity. It ensures uniform application across all projects. The capacity to establish projects using guidelines. It is also compatible with previous versions of its are some features of Maven.

How important is POM?

The project object model, or POM, is a critical component of Maven's operation. The pom file in the current directory is checked when performing a Maven objective or task.

What is the purpose of Maven plugins?

Maven plugins generate JAR files, WAR files, source code compilation, and unit testing.

What locations do Maven dependencies are kept in?

Maven has a local repository for each JAR, dependency file, and other things it downloads. All the artifacts are kept on the local computer in the Maven local repository, a folder.

What does a snapshot in Maven mean?

A snapshot is a specific version of a project that reflects its present level of development. Maven retrieves a project snapshot from the remote repository for each build.

Conclusion

This article briefly discussed the Maven repository and its types. The types of Maven include the local repo, central repo and remote repo. We also saw how Maven searches for repos and how the repo manager works. Thus concluding our discussion of the Maven repository and its types.

We hope this blog has helped you with the Maven repository and its types. If you like to learn more, you can check out our articles: 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptAWS and many more! If you wish to test your competency in coding, check out the mock test series and take part in the contests hosted on Coding Ninjas Studio! 

If you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

Nevertheless, consider our paid courses to give your career an edge over others!

Happy Learning!

Topics covered
1.
Introduction
2.
What is Maven?
3.
Maven Repository
3.1.
Maven Local Repository
3.2.
Maven Central Repository
3.3.
Maven Remote Repository
4.
How Maven Searches for Dependencies
5.
Maven Repository Manager
6.
Frequently Asked Questions
6.1.
What are some features of Maven?
6.2.
How important is POM?
6.3.
What is the purpose of Maven plugins?
6.4.
What locations do Maven dependencies are kept in?
6.5.
What does a snapshot in Maven mean?
7.
Conclusion