Maven 简明教程
Maven - Repositories
What is a Maven Repository?
在 Maven 术语中,存储库是一个存储所有项目 jar、库 jar、插件或任何其他项目特定构件的目录,Maven 可以在其中轻松使用它们。
In Maven terminology, a repository is a directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily.
Maven 存储库有三种类型。以下说明将介绍这三种类型的相关概念。
Maven repository are of three types. The following illustration will give an idea regarding these three types.
-
local
-
central
-
remote
Local Repository
Maven 本地存储库是计算机上的一个文件夹位置。首次运行任何 maven 命令时将创建它。
Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time.
Maven 本地存储库保存项目的所有依赖项(库 jar、插件 jar 等)。运行 Maven 构建时,Maven 会自动将所有依赖项 jar 下载到本地存储库中。这有助于避免每次构建项目时引用存储在远程计算机上的依赖项。
Maven local repository keeps your project’s all dependencies (library jars, plugin jars etc.). When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build.
默认情况下,Maven会在 %USER_HOME% 目录中创建 Maven 本地存储库。要覆盖默认位置,请在 %M2_HOME%\conf 目录中提供的 Maven settings.xml 文件中指定另一路径。
Maven local repository by default get created by Maven in %USER_HOME% directory. To override the default location, mention another path in Maven settings.xml file available at %M2_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>C:/MyLocalRepository</localRepository>
</settings>
运行 Maven 命令时,Maven 会将依赖项下载到自定义路径。
When you run Maven command, Maven will download dependencies to your custom path.
Central Repository
Maven 中央存储库是由 Maven 社区提供的存储库。它包含大量常用库。
Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.
当 Maven 在本地存储库中找不到任何依赖项时,它会使用以下 URL 在中央存储库中开始搜索: https://repo1.maven.org/maven2/
When Maven does not find any dependency in local repository, it starts searching in central repository using following URL − https://repo1.maven.org/maven2/
中央存储库的关键概念如下:
Key concepts of Central repository are as follows −
-
This repository is managed by Maven community.
-
It is not required to be configured.
-
It requires internet access to be searched.
为了浏览中央 Maven 存储库的内容,Maven 社区提供了 URL − https://search.maven.org/#browse 。使用此库,开发人员可以搜索中央存储库中所有可用的库。
To browse the content of central maven repository, maven community has provided a URL − https://search.maven.org/#browse. Using this library, a developer can search all the available libraries in central repository.
Remote Repository
有时,Maven 也找不到中央存储库中提到的依赖项。然后它会停止构建过程,并向控制台输出错误消息。为了防止这种情况,Maven 提供了 Remote Repository 的概念,它是开发人员自己的自定义存储库,其中包含所需的库或其他项目 jar。
Sometimes, Maven does not find a mentioned dependency in central repository as well. It then stops the build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository, which is developer’s own custom repository containing required libraries or other project jars.
例如,使用下面提到的 POM.xml,Maven 将从在同一 pom.xml 中提到的远程存储库中下载依赖项(在中央存储库中不可用)。
For example, using below mentioned POM.xml, Maven will download dependency (not available in central repository) from Remote Repositories mentioned in the same 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 Dependency Search Sequence
当我们执行 Maven 构建命令时,Maven 将按以下顺序开始查找依赖库 −
When we execute Maven build commands, Maven starts looking for dependency libraries in the following sequence −
-
Step 1 − Search dependency in local repository, if not found, move to step 2 else perform the further processing.
-
Step 2 − Search dependency in central repository, if not found and remote repository/repositories is/are mentioned then move to step 4. Else it is downloaded to local repository for future reference.
-
Step 3 − If a remote repository has not been mentioned, Maven simply stops the processing and throws error (Unable to find dependency).
-
Step 4 − Search dependency in remote repository or repositories, if found then it is downloaded to local repository for future reference. Otherwise, Maven stops processing and throws error (Unable to find dependency).