3. Maven Repository
A maven repository is a directory of packaged JAR file with pom.xml file. Maven searches for dependencies in the repositories. There are 3 types of maven repository:
- Local Repository
- Central Repository
- Remote Repository
Maven searches for the dependencies in the following order:
Local repository then Central repository then Remote repository.
If dependency is not found in these repositories, maven stops processing and throws an error.
3.1 Maven Local Repository
Maven local repository is located in your local system. It is created by the maven when you run any maven command.
By default, maven local repository is %USER_HOME%/.m2 directory. For example: C:\Users\Administrator.SYSTEM-18\.m2
The location of your maven local repository can be changed by adding or modifying a localRepository element to your settings.xml file. The default settings.xml file contains the following entry, describing this.
<!-- localRepository | The path to the local repository maven will use to store artifacts. | Default: ~/.m2/repository <localRepository>/path/to/local/repo</localRepository> -->
I’d like my local repository to be located at C:\m2repo, so I’ll add the following entry to settings.xml:
Now let’s test it and see if it works. To do this, I’ll run a maven command at the command prompt. I’ll do a “maven clean”. This command will fail since I’m not running it in a directory with a pom.xml file, but that’s fine. We’re just running a maven command which requires the download of some files (including a jar file) from the central maven repository to our local maven repository.
After running this command, we can check and make sure that the localRepository setting worked. If we open a Windows Explorer window and navigate to our C:\dev directory, we can see that an m2repo directory has been created. If we inspect the contents of m2repo, we can see that it indeed contains maven repository contents, as expected.
3.2 Maven Central Repository
Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.
When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/
Key concepts of Central repository
- This repository is managed by Maven community.
- It is not required to be configured.
- It requires internet access to be searched.
To browse the content of central maven repository, maven community has provided a URL: http://search.maven.org/#browse. Using this library, a developer can search all the available libraries in central repository.
3.3 Maven Remote Repository
A remote repository is a repository on a web server from which Maven can download dependencies, just like the central repository. A remote repository can be located anywhere on the internet, or inside a local network.
A remote repository is often used for hosting projects internal to your organization, which are shared by multiple projects. For instance, a common security project might be used across multiple internal projects. This security project should not be accessible to the outside world, and should thus not be hosted in the public, central Maven repository. Instead it can be hosted in an internal remote repository.
Dependencies found in a remote repository are also downloaded and put into your local repository by Maven.
You can configure a remote repository in the POM file. Put the following XML elements right after the <dependencies> element:
<repositories> <repository> < id>jenkov.code</id> <url>http://maven.jenkov.com/maven2/lib</url> </repository></repositories>