EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Maven Tutorial Maven Local Repository
Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML

Maven Local Repository

Maven Local Repository

Introduction to Maven Local Repository

Maven local repository local folder stored in client/user machine. The folder is created when the user executes any maven commands for the first time.

Maven’s local repository stores all plugins and dependency files. For example, Library jar, plugin jar files, etc., when the user executes the maven build command, the maven will automatically download all jar files into a local repository for future references. It helps the maven avoid the references to dependencies stored on the remote machines every time the project is built or the user executes the build command.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

By default, the local Repository is created in the %USER_HOME% directory.

Working on Local Repository

Sequence Diagram for Local Maven Repository

<image>

The above figure shows the sequence diagram of the local Repository and how maven searches for repositories. The number denotes the steps.

Initial Step: Maven searches in Local Repository for specified dependencies; if found, it will execute.

Step 1: Maven/Local Repository transfer control to Central Repository (if specified dependencies are not found).

Step 2: Specified dependencies are searched.

Step 3: If specific dependencies are found in the Central Repository, those dependencies are copied into the local Repository.

Step 4: Maven/Local Repository transfer control to the central Repository to search dependencies.

Step 5: Specific Dependencies are searched in the Central Repository.

Step 6: Control Transferred to Remote Repository (if not found in central Repository) for specified dependencies.

Step 7: Specific Dependencies are searched in Remote Repositories.

Steps 8 & 9:  If the specific dependency is found in the Remote Repository, copied into the local Repository.

Location of Local Repository

Usually, maven stores all local repositories into the/.m2 folder.

  • By default, maven creates a local repository under %UESR_HOME%
  • To define local repository settings are stored in the settings.xml file.
  • The default location of xml is {MAVEN_HOME}\conf\settings.xml.
  • The local repository location can be overridden by changing the path in settings.xml.

Updating or customizing the Local Repository

By default, the maven stores local repositories in a folder called…. /Repository. The default URL for local repository is ${user.home}/.m2/repository. This can be customized in settings.xml.settings.xml stored in the folder called /conf/settings.xml.

Settings.xml Example Code.

<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/.m2/repository(user path) </localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
…
….
</settings>

  • localRepository: specify the file path to create local repositories. The default value is: ${user.home}/.m2/repository.
  • interactiveMode: is true if you want to interact with the user for input, false if not.
  • Offline: mode is true if the build system operates in offline mode.

Maven command Lines for setting Repository.

The following command is used to add artifacts into the local Repository with some parameters explained below.

url URL location of the Repository
File Location of the jar file.
groupId User group Id.
ArtifactId User artifactId.
Packaging Respective packaging types, i.e., jar, war, ear
Version Version number

>mvn deploy:deploy-file -Durl=file:/repo path/ -Dfile=lib.jar
-DgroupId=com.educba -DartifactId=lib -Dpackaging=jar -Dversion=1.0

This command deploys the file into the local Repository. Parameters are compulsory.

POM.xml for adding the local Repository.

Super pom file.

<project>
<modelVersion>4.0.0</modelVersion>
<repositories>
<repository>
<id>local</id>
<name>Local Repository</name>
<url>https://repo.maven.apache.org/.......</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
…..
…..
</project>

Customized POM.XML file.

<repositories>
<repository>
<id>local.project</id>
<name>Example</name>
<url>………………………../repository</url>
</repository>
</repositories>

Adding dependency jar in POM.XML

<dependency>
<groupId>com.educba</groupId>
<artifactId>lib</artifactId>
<version>1.0</version>
</dependency>

Note:

  • LocalRepositoryNotAccessibleException

Maven throws this exception when the .m2 folder is missing; it cannot overwrite the m2 folder, etc.

Benefits of Local Repository

  • Fasten the clean build process while using local repositories.
  • Less manual intervention for the first-time build process.
  • The local Repository acts as a Single central reference for all dependent software libraries rather than several independent local libraries.
  • Reduced dependency version conflict.

Note on Additional Basic Settings w.r.t Repositories.

  • Maven Snapshot Version.
  • Artifactory allows central support on how snapshots are deployed into the Repository, regardless of user settings. Guarantee standard format for deploying snapshots within the organization.
  • Three possible options
  1. Unique: Time-based version number.
  2. Non-unique: default self-overriding naming pattern.
  3. Deployer: uses the deployer sent format.
  • Max Unique Snapshots.
    • Specifies maximum unique snapshots for the same artifact.
    • Once the max number is reached and a new snapshot is added, the older snapshot is removed automatically. 0 is the default value.
  • Handle Releases.
    • Artifactory allows the release of Artifacts in the Repository if this property is set.
  • Handle Snapshots
    • If this property is set, Artifactory allows snapshot Artifacts into the Repository.
  • Suppress POM consistency.
    • While deploying an Artifact to repository-Artifactory verifies that the value set for groupId: artifactId: version in pom file is consistent with the deployed path.
  • Select Property Sets.
    • Used to define property sets that will be available artifacts stored in the Repository.
  • Allow Content Browsing
    • If this property is set, users can view file contents from Artifactory.

Conclusion

Maven provides three Repositories- Local, Central, and Remote; among all the three repos, the Local Repository plays the most important role in maven. Local Repository acts as a central library within a maven environment or user local machine to store necessary libraries for future reference. Since it stores all dependency libraries downloaded from Central or Remote Repositories, it indirectly reduces the time consumption; every time, it is not necessary to search in other repositories for dependency libraries. In addition, the use of the local library increases the project’s overall execution/build process.

Recommended Articles

This is a guide to Maven Local Repository. Here we discuss the three Maven Local Repository, Local, Central, and Remote, along with the benefits. You may also have a look at the following articles to learn more –

  1. Maven Commands
  2. How to Install Maven
  3. Maven Plugins
  4. What is Maven
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more