Updated April 12, 2023
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.
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
- Unique: Time-based version number.
- Non-unique: default self-overriding naming pattern.
- 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
We hope that this EDUCBA information on “Maven Local Repository” was beneficial to you. You can view EDUCBA’s recommended articles for more information.