Maven Coordinates and Repositories

In this post, we are going to discuss the Maven Coordinates and Repositories in detail.

Maven Coordinates or Identifiers

Maven Coordinates defines a set of identifiers that uniquely defines a project, a dependency, or a plugin in a Maven POM. Let’s look at the below pom.xml file that we created in one of our previous posts.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.website.codekru</groupId>
  <artifactId>DemoProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>DemoProject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Here, the highlighted part is the Maven coordinates of this project which is used to uniquely identify the project. Using Maven coordinates, we can pinpoint the project and relate it to another project, either as a dependency, or a plugin, or a parent project reference. Maven coordinates are written in the below format where each identifier is separated by the colon ( : ).

groupId:artifactId:packaging:version

So, our project’s maven coordinates will be the org.website.codekru:DemoProject:jar:1.0-SNAPSHOT.

  • groupId: The normal convention for the group identifiers is that they begin with the reverse domain name of the organization or company. So, for the codekru.com website, it would be com.codekru
  • artificatId: It is the unique identifier within groupId that represents a single project
  • version: While developing a project or releasing a project onto the production servers, we normally assign versions to each of our releases. So, for a single project, we will have different versions corresponding to our releases but for a single release, it would be unique. The projects that are undergoing active development normally uses a specific identifier that marks a version as SNAPSHOT
  • packaging: By default, the packaging of a project is jar. We can change it to war also. A project with packaging jar produces a jar archive file while a project with packaging war produces a web application

These four identifiers are the keys to locating a project in the ocean of Maven projects.

Maven Repositories

A Maven Repository is a directory where all of the project jars, plugins, etc. reside. You also might have noticed that when we run the maven project for the first time, then it download some of the dependencies mentioned in the pom.xml file of your project. So, Maven downloads these jars or plugins from a repository. Now, there are three types of repositories.

  • Local Repository
  • Central Repository
  • Remote Repository

Maven will search the dependencies in the following order.

maven search repository order

  • First, maven will try to search the dependency in the local repository
  • And if it is not present there, then it will go and try to download the dependency from its central repository and put it in your local repository
  • But if it’s not found in the central repository either, then it will try to find the jar in the remote repository. Only after this step, if the dependency is still not found, then maven will throw an error and abort the build process.
Local Repository

Maven’s local repository is a folder present in our local machines. It is created when we run any maven command for the first time. Its location will be dependent on the machine.

  • Like for Windows, your local repository is in the  %USER_HOME%\.m2\repository directory
  • And for Linux, your local repository is in the ~/.m2/repository directory
maven local repository path

We can change the local repository location by overriding the default path in the setting.xml present in the %MAVEN_HOME%\conf\ directory.

<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>/path/to/local/repo</localRepository>

</settings>
Central Repository

If dependencies are not found in your local repository, then Maven will search them in the central repository. The central repository is managed by the Maven community and needs an internet connection to pull up any Maven dependency.

Maven dependencies are stored at the following URL: https://repo1.maven.org/maven2/. An artifact with the coordinates org.apache.commons:commons-io:1.3.2 is available under the directory org/apache/commons/commons-io/1.3.2/ in a file named commons-io-1.3.2.jar.

The standard convention to store an artifact in the Maven Repository relative to the root directory is:

/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>

Maven downloads and stores these artifacts in our local repository and once Maven has downloaded an artifact from the central repository, then it never needs to download it again, as Maven will always look for an artifact in the local repository before looking anywhere else.

We can also browse various artifacts using the URL: https://search.maven.org/#browse.

Remote Repository

If Maven is unable to find the dependency in the central repository, then Maven will search the dependency in the Remote repository if provided by the developer. Otherwise, Maven will throw an error and stop the build process.

Remote repositories location will be enclosed within <repositories> tag while mentioning each repository location under a <repository> tag with the <id>,<name> and <url> of the repository to fetch the dependencies.

<project>
...
  <repositories>
    <repository>
      <id>repo1</id>
      <name>your custom repo</name>
      <url>http://urlToYourRemoteRepo</url>
    </repository>
    <repository>
      <id>repo2</id>
      <name>your custom repo</name>
      <url>http://urlToYourRemoteRepo2</url>
    </repository>
  </repositories>
...
</project>

Remote repositories help organizations to make their dependencies that will only be used by the organization.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *