In Maven terminology, a warehouse is a place.
The Maven repository is the third-party library that the project depends on, and the location of this repository is called the repository.
In Maven, the output of any dependency, plug-in, or project build can be called a component.
The Maven repository helps us manage artifacts (mainly JAR), which is where all the JAR files (WAR,ZIP,POM, etc.) are placed.
There are three types of Maven repositories:
Local (local)
Central (central)
Remote (remote)
11.6.1. Local warehouse ¶
Maven’s local repository is not created after Maven is installed, it is created the first time the maven command is executed.
When running Maven, any artifacts required by Maven are obtained directly from the local repository. If the local warehouse does not, it will first try to download the artifacts from the remote warehouse to the local warehouse, and then use the artifacts from the local warehouse.
By default, regardless of Linux or Windows, each user has a repository directory with a path name of .m2 / repository/ under his or her own user directory.
The Maven local warehouse is created under the% USER_HOME% directory by default. To change the default location, define another path in the settings.xml file of Maven in the% M2roomHOME%conf directory.
<settingsxmlns="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 will download the dependent files to the path you specified.
11.6.2. Central warehouse ¶
The Maven Central Warehouse is a repository provided by the Maven community that contains a large number of commonly used libraries.
The central repository contains most of the popular open source Java artifacts, as well as source code, author information, SCM, information, license information, and so on. In general, simple Java projects rely on artifacts that can be downloaded here.
Key concepts of a central warehouse:
The warehouse is managed by the Maven community.
No configuration is required.
It needs to be accessed through the network.
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 all available code bases.
11.6.3. Remote warehouse ¶
If Maven also cannot find the dependent file in the central repository, it stops the build process and outputs the error message to the console. To avoid this, Maven provides the concept of a remote warehouse, which is customized by developers to include the required code base or other jar files used in the project.
For example, using the following pom.xml,Maven will download the dependent files declared in the pom.xml (not available in the central repository) from the remote repository.
<projectxmlns="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>
11.6.4. Maven depends on search order ¶
When we execute the Maven build command, Maven starts looking for dependent libraries in the following order:
步骤 1 -search in the local warehouse, if it is not found, perform step 2, and if found, perform other actions.
步骤 2 -search in the central warehouse, if it cannot be found, and one or more remote warehouses have been set up, perform step 4, and if found, download it to the local warehouse for future reference.
步骤 3 -if the remote repository is not set up, Maven will simply stagnate and throw an error (the dependent file cannot be found).
步骤 4 -search for dependent files in one or more remote repositories, and if found, download them to the local repository for future reference, otherwise Maven will stop processing and throw an error (dependent files cannot be found).
11.6.5. Maven Aliyun (Aliyun) warehouse ¶
Maven warehouse default in foreign countries, domestic use is inevitably very slow, we can replace it with Aliyun warehouse.
Modify the settings.xml file in the conf folder under the maven root directory, and on the mirrors node, add the following:
<mirror><id>aliyunmaven</id><mirrorOf>\*</mirrorOf><name>阿里云公共仓库</name><url>https://maven.aliyun.com/repository/public</url></mirror>
If you want to use other proxy repositories, you can add the corresponding warehouse usage address in the < repositories > < / repositories > node. Take the use of spring proxy warehouse 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>
Add the file information you want to reference in your pom.xml < denpendencies > < / denpendencies > node:
<dependency><groupId>[GROUP_ID]</groupId><artifactId>[ARTIFACT_ID]</artifactId><version>[VERSION]</version></dependency>
Execute the pull command:
mvn install
11.6.6. Gradle configuration Guid ¶
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 another proxy warehouse, take the spring warehouse 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