Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Maven Repository

Maven Repository

In Maven's terminology, a repository is a place (place).

A Maven repository is a third-party library depended on in a project, and the location of this library is called a repository.

In Maven, any dependency, plugin, or project build output can be called an artifact.

Maven repositories help us manage artifacts (mainly JARs); it is the place where all JAR files (WAR, ZIP, POM, etc.) are placed.

Maven repository types:

Repository type Storage location Purpose Access priority
Local repository Under the user directory's.m2/repository Cache remotely downloaded dependencies 1
Remote repository Web server Provide centralized storage of dependencies and plugins 2
- Central repository repo.maven.apache.org The default repository maintained officially by Maven 2.1
- Private server repository Built internally within a company (such as Nexus) Host private dependencies, accelerate builds 2.2
- Other public repositories Such as Aliyun, JCenter, etc. Mirror or supplement the central repository 2.3

Local repository

Maven's local repository is not created after installing Maven; it is created only when the maven command is executed for the first time.

When running Maven, any artifacts needed by Maven are obtained directly from the local repository. If the local repository does not have them, it will first try to download the artifacts from a remote repository to the local repository, and then use the artifacts in the local repository.

By default, whether Linux or Windows, each user has a repository directory named .m2/repository/ under their own user directory.

Maven's local repository is created in the %USER_HOME% directory by default. To change the default location, define another path in Maven's settings.xml file in the %M2_HOME%\conf directory.

<

settings

<

localRepository

C:/MyLocalRepository

localRepository

settings

When you run Maven commands, Maven will download dependency files to the path you specified.

Clean the local repository:



mvn dependency:purge-local-repository

# or manually delete the ~/.m2/repository directory


Central repository

Maven's central repository is a repository provided by the Maven community, which contains a large number of commonly used libraries.

The central repository contains the vast majority of popular open-source Java artifacts, as well as source code, author information, SCM, information, license information, etc. Generally speaking, artifacts depended on by simple Java projects can all be downloaded here.

Key concepts of the central repository:

Central repository (no explicit configuration required) default address:https://repo.maven.apache.org/maven2/


Remote repository

Custom Repository Configuration

If Maven cannot find the dependency files in the central repository either, it will stop the build process and output an error message to the console. To avoid this situation, Maven provides the concept of remote repositories, which are repositories customized by developers themselves, containing the required code libraries or jar files used in other projects.

For example, using the following pom.xml, Maven will download from the remote repository the files declared as dependencies in the pom.xml (which cannot be obtained from the central repository).

Example

   

       

aliyun

       

Aliyun Maven

       

https://maven.aliyun.com/repository/public

       

           

true

       

       

           

false

       

   

Mirror repository (recommended)

Configure in settings.xml:

Example

   

       

aliyun

       

Aliyun Mirror

       

https://maven.aliyun.com/repository/public

       

central

   


Maven Aliyun (Aliyun) repository

Maven repositories are by default abroad, which is inevitably slow to use domestically, we can switch to Aliyun's repository.

Modify the settings.xml file in the conf folder under the maven root directory, and add the following content to the mirrors node:

<

mirror

<

id

aliyunmaven

id

<

mirrorOf

mirrorOf

<

name

Aliyun public repository

name

<

url

https://maven.aliyun.com/repository/public

url

mirror

If you want to use other proxy repositories, you can add the corresponding repository usage address in the node. Taking the use of the spring proxy repository as an example:

<

repository

<

id

spring

id

<

url

https://maven.aliyun.com/repository/spring

url

<

releases

<

enabled

true

enabled

releases

<

snapshots

<

enabled

true

enabled

snapshots

repository

In your pom.xml file add the file information you want to reference in the node:

<

dependency

<

groupId

[GROUP_ID]

groupId

<

artifactId

[ARTIFACT_ID]

artifactId

<

version

[VERSION]

version

dependency

Execute the pull command:

mvn install

gradle configuration guide

Add the following code to the build.gradle file:

allprojects { repositories { maven { url 'https://maven.aliyun.com/repository/public/' } mavenLocal() mavenCentral() } }

If you want to use other proxy repositories, taking the spring repository as an example, the code is as follows:

allProjects { repositories { maven { url 'https://maven.aliyun.com/repository/public/' } maven { url 'https://maven.aliyun.com/repository/spring/' } mavenLocal() mavenCentral() } }

Add the file information you want to reference:

dependencies { compile '[GROUP_ID]:[ARTIFACT_ID]:[VERSION]' }

Execute the following command to install dependencies:

gradle dependencies 

或 

./gradlew dependencies 


Private server repository

Repository types:

Repository type Purpose
hosted Store private artifacts
proxy Proxy remote repositories
group Aggregate multiple repositories

Publish to private server

Configure in pom.xml:



    

        nexus-releases

        http://your-nexus/repository/maven-releases

    

    

        nexus-snapshots

        http://your-nexus/repository/maven-snapshots

    



Configure authentication in settings.xml:



    

        nexus-releases

        deploy-user

        deploy-pwd

    



Publish command:



mvn deploy


Maven Dependency Search Order

When we execute a Maven build command, Maven starts searching for dependency libraries in the following order:

Common Commands

Command Purpose
mvn dependency:resolve Resolve dependencies
mvn dependency:tree View the dependency tree
mvn dependency:purge-local-repository Clean the local repository
mvn deploy Publish to a remote repository

Common Problem Solutions

**1. Dependency download failure symptom:** Could not transfer artifact...

Solution:

**2. Authentication failure symptom:** 401 Unauthorized

Solution:

3. Dependency conflict

** Troubleshooting:**

mvn dependency:tree -Dverbose

Solution: in unify the version