Maven Repository Introduction

What is Maven Repository ?

To resolve the dependency of a project Maven requires multiple JARs (Framework specific Jars or Project Jars), plugins or any other project specific artifacts are stored and can be used by Maven easily. Maven will look for these Jars in certain locations, these locations are called Repository (Maven Specific Terminology).


Maven has 3 different type of repository.

  • Local Repository
  • Central Repository
  • Remote Repository

What is Maven Repository ?

To resolve the dependency of a project Maven requires multiple JARs (Framework specific Jars or Project Jars), plugins or any other project specific artifacts are stored and can be used by Maven easily. Maven will look for these Jars in certain locations, these locations are called Repository (Maven Specific Terminology).


Maven has 3 different type of repository.

  • Local Repository
  • Central Repository
  • Remote Repository






Local Repository :

Maven local repository is a folder on local system which is used to store project's dependencies (Jars). Whenever you try to build a project Maven will try to download all the required Jars in Local Repository.

By default, Maven local repository is default to .m2 folder

  1. In Windows - C:\Documents and Settings\{your-username}\.m2
  2. In Unix/Mac OS X - ~/.m2

How to change Maven Local Repository:

To change the location of Default Local Repository you need to change the maven's "settings.xml", which will be located @ %M2_HOME%\conf.
update localRepository to your desired path like D:/maven_repo.

<settings>
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
 
<localRepository>D:/maven_repo</localRepository>



Central Repository :

When you build a Maven's project, Maven will check your pom.xml file, to identify which dependency to download. First, Maven will get the dependency from your local repository Maven local repository, if not found, then get it from the default Maven central repository -http://repo1.maven.org/maven2/

Remote Repository :

Sometime, Maven does not find a mentioned dependency in central repository as well then it stopped build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository which is developer's own custom repository containing required libraries or other project jars.
For example, using below mentioned POM.xml,Maven will download dependency (not available in central repository) from Remote Repositories mentioned in the same pom.xml.

<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.javawebtutor.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.javawebtutor.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>javawebtutor.lib1</id>
         <url>http://javawebtutor.com/maven2/lib1</url>
      </repository>
      <repository>
         <id>javawebtutor.lib2</id>
         <url>http://javawebtutor.com/maven2/lib1</url>
      </repository>
   </repositories>
</project>





comments powered by Disqus