Maven Repository and its different types(Part-2)

Previous article, getting-started-with-maven (part-1), mainly focused on the introductory part of maven. In this article, we will get to know Maven Repository and its different types and how they work. Let’s get started.

During project development, we need various artifacts say dependencies, libraries, plugins etc. To manage such dependencies across various terminal in an organization is a tedious task and requires lot of effort and time. Such dependencies should be managed at one place to avoid any future problem in project development. Maven repository is an answer to all the problems related to dependency management.

Remember, in maven all the configurations related to the artifacts are done in pom.xml. We will see about Project Object Model (POM) later in this tutorial series.

We keep hearing the word dependency. What is dependency in maven? A dependency in maven is a type of artifact that our project needs to have in maven build lifecycle. An artifact is a file, usually a JAR, that gets deployed to a Maven repository. All dependencies related information should be configured in your project’s pom.xml file.

dependency tag in pom.xml

Dependency tag in pom.xml in maven

Each dependency is declared with the help of <dependency> tag under <dependencies> as shown above. A dependency has a group ID (usually a reversed domain name, like com.example.foo), an artifact ID (just a name), and a version string. The three together uniquely identify the artifact.

groupId – Generally company name

artifactId – Project name/jar name/plugin name

version – syn:<ver>-SNAPSHOT/RELEASE/FINAL/BETA

Now that we know about dependency, the question is how these dependencies are managed. There is the concept called repository in maven where all build artifacts are managed centrally, locally and remotely.

Maven Repository and its different types

What is Repository in Maven?

According to Maven Official Documentation, “A repository in Maven is used to hold build artifacts and dependencies of varying types.” A maven repository is a place i.e. directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily.

Types of Repository in Maven

Following are the types of repository in maven.

There are three types of repository in maven. They are

  1. Local Repository
  2. Central Repository
  3. Remote Repository

All these repository types are briefly described below.

  1. Local Repository: The maven local repository is a local folder which is user specific and used to store all your project’s dependencies. Generally it contains jars, plugins, current project related packaging and etc. Before maven attempts to download particular artifacts from either local repository or central repository, it checks in the local repository. This is usually located at c:\users\<usernmae>\.m2. This local repository will be created first time automatically for any maven command apart from mvn –version. For me, it is created in the following folder.

How to change Local Maven Repository default location.

You can always change the name and location of local maven repository to desired location with desired name in your system. Location can be changed through  <maven_home>\conf\settings.xml file by updating <localRepository> tag as follows.

<localRepository>d:\\maven</localRepository>

  1. Central Repository: Generally central repository is maintained in the internet and managed by Apache Maven Community. 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. URL for central repository is http://repo1.maven.org/maven2/

Mostly open source projects artifacts are hosted in the central repository. You can search for any dependencies and other artifacts from http://mvnrepository.com/ . Search for your desired dependency and copy it and paste it in the pox.xml of your project

3. Remote Repository: Remote repository is specific to company or vendor containing their own libraries and other artifacts. At the time of resolving dependencies, maven first searches it in the local repository. If maven does not find the mentioned dependency in the central repository. If not found in central repository as well then it stops the build process and missing artifacts related error message is shown in the console. . To prevent such situation, Maven provides concept of Remote Repository. To use artifacts from remote repository, <repository> tag should be configured in project’s pom.xml as shown in the following image.

What is Repository in Maven

3 thoughts on “Maven Repository and its different types(Part-2)”

  1. Pingback: How to install maven in eclipse - code-adda
  2. Trackback: How to install maven in eclipse - code-adda
  3. Pingback: repository utilization in maven - code-adda
  4. Trackback: repository utilization in maven - code-adda
  5. Mohit Sharma says:

    This is so helpful,Thanks Chandra.Please write on Spring and deployment of web projects on server,Thanks in advance.

Leave a Reply