Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven Warehouse


May 26, 2021 Maven


Table of contents


Maven - Warehouse

What is a Maven warehouse?

In Maven's terminology, a repository is a place, such as a directory, that stores all project jar files, library jar files, plug-ins, or any other project-specified files.

There are three types of Maven warehouses:

  • Local
  • Central
  • Remote

The local warehouse

Maven's local repository is a folder on the machine. It is created the first time you run any maven command.

Maven's local repository holds all the dependencies of your project (library jar, plugin jar, etc.). W hen you run a Maven build once, Maven automatically downloads all dependent jar files to the local repository. It avoids referencing dependent files stored on remote machines every time you build.

Maven local repositories are created by default in the %USER_HOME% directory. To modify the default location, define another path in Maven's settings file in the %M2_HOME%?conf directory.xml the settings file.

<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>C:/MyLocalRepository</localRepository>
</settings>

When you run the Maven command, Maven downloads the dependent files into the path you specify.

Central warehouse

Maven Central Warehouse is a warehouse provided by the Maven community and contains a number of commonly used libraries.

Key concepts of a central warehouse:

  • The warehouse is managed by the Maven community.
  • No configuration is required.
  • You need to be over the network to access it.

To browse the contents of the central repository, the maven community provides a URL: http://search.maven.org/#browse. Using this repository, developers can search for all available code libraries.

Remote warehouse

If Maven also can't find the dependent library files in the central repository, it stops the build process and outputs an error message to the console. To avoid this, Maven provides the concept of a remote repository, which is a developer's own custom repository that contains the required code base or jar files used in other projects.

For example, using the following POM .xml, Maven will download from the remote repository the dependent (not available in the central repository) files that are declared in the 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.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.companyname.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>companyname.lib1</id>
         <url>http://download.companyname.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.companyname.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>

Maven relies on the search order

When we execute the Maven build command, Maven starts looking for dependent libraries in the following order:

  • Step 1 - Search in the local repository, step 2 if not found, and do something else if found.
  • Step 2 - Search in the central repository, if not found, and one or more remote repositories are already set up, then step 4 is performed, and if found, the download to the local repository has been referenced in the future.
  • Step 3 - If the remote repository is not set, Maven will simply stall processing and throw an error (the dependent file cannot be found).
  • Step 4 - Search for dependent files in one or more remote repositories, and if found, download to the local repository has been referenced in the future, otherwise Maven will stop processing and throw errors (the dependent files cannot be found).