Maven下载依赖的顺序及配置文件小结
作者:罗罗的1024
本文主要介绍了Maven下载依赖的顺序及配置文件小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在 Maven 中,当下载依赖项时,存在多个仓库时会按照以下优先级顺序进行搜索:
- 本地仓库:Maven 会首先在本地的 Maven 仓库中查找依赖项。
- 私有仓库(私服):如果在本地仓库中未找到依赖项,Maven 会按照项目的 pom.xml 文件中配置的 元素指定的顺序依次搜索私有仓库。私有仓库是你在项目中添加的自定义仓库,通常是用于存放项目内部开发的依赖项或者第三方库,它可以是一个本地服务器或者其他网络位置。
- 中央仓库(Maven Central Repository):Maven 中央仓库是 Maven 官方维护的集中存储库,包含了大量常用的开源 Java 项目的构件和元数据。如果项目的 pom.xml 文件没有指定其他远程仓库,并且本地仓库中也没有所需的依赖项,Maven 会自动搜索中央仓库来下载依赖项。
- 其他远程仓库:如果在项目的 pom.xml 文件中配置了其他远程仓库地址,并且中央仓库、私有仓库和本地仓库都没有所需的依赖项,Maven 会按照 中指定的顺序依次搜索这些自定义远程仓库。
综上所述,Maven 下载依赖项的优先级顺序为:本地仓库 > 远程仓库(包括中央仓库) > 自定义远程仓库。Maven 会按照这个顺序查找依赖项,直到找到所需的依赖或者搜索完所有配置的仓库。如果依赖项在某个仓库中找到了,Maven 会将其下载到本地仓库,并在后续构建过程中直接使用本地仓库中的依赖,以加快构建速度和确保依赖项的一致性。
Maven的配置文件说明
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- | This is the configuration file for Maven. It can be specified at two levels: | | 1. User Level. This settings.xml file provides configuration for a single user, | and is normally provided in ${user.home}/.m2/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -s /path/to/user/settings.xml | | 2. Global Level. This settings.xml file provides configuration for all Maven | users on a machine (assuming they're all using the same Maven | installation). It's normally provided in | ${maven.home}/conf/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -gs /path/to/global/settings.xml | | The sections in this sample file are intended to give you a running start at | getting the most out of your Maven installation. Where appropriate, the default | values (values used when the setting is not specified) are provided. | |--> <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 | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository 本地仓库地址 --> <localRepository>D:\Program Files\localMavenRepoistory</localRepository> <!-- interactiveMode | This will determine whether maven prompts you when it needs input. If set to false, | maven will use a sensible default value, perhaps based on some other setting, for | the parameter in question. | | Default: true <interactiveMode>true</interactiveMode> --> <!-- offline | Determines whether maven should attempt to connect to the network when executing a build. | This will have an effect on artifact downloads, artifact deployment, and others. | | Default: false <offline>false</offline> --> <!-- pluginGroups | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list. |--> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <!-- proxies | This is a list of proxies which can be used on this machine to connect to the network. | Unless otherwise specified (by system property or command-line switch), the first proxy | specification in this list marked as active will be used. |--> <proxies> <!-- proxy | Specification for one proxy, to be used in connecting to the network. | <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> --> </proxies> <!-- servers | This is a list of authentication profiles, keyed by the server-id used within the system. | Authentication profiles can be used whenever maven must make a connection to a remote server. |--> <servers> <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | 在 Maven 中,<server> 元素用于配置认证信息,主要是用于在构建过程中访问需要认证的远程仓库或资源 当使用 Maven 构建项目时,有些远程仓库可能需要进行身份验证(例如私有仓库),这时候就需要提供访问该仓库的用户名和密码。 为了不将用户名和密码明文写在 pom.xml 文件中,可以将认证信息放在 Maven的 settings.xml 文件中的 <servers> 元素中 | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> 在上述示例中,配置了一个 <server> 元素,它的 <id> 是 "deploymentRepo",表示该认证信息用于访问 ID 为 "deploymentRepo" 的远程仓库。 然后,通过 <username> 和 <password> 元素分别提供用户名和密码,这些信息将用于在访问远程仓库时进行身份验证。 当 Maven 需要访问远程仓库或资源时,会检查 settings.xml 文件中的 <servers> 元素,如果有匹配的 <server> 元素(根据 <id> 属性匹配), 就会使用该认证信息进行访问。这样,就可以安全地在 settings.xml 文件中管理认证信息,而不需要在 pom.xml 文件中明文暴露用户名和密码。 --> <!-- Another sample, using keys to authenticate. <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> --> <server> <id>releases</id> <username>zs</username> <password>pp</password> </server> <server> <id>snapshots</id> <username>zs</username> <password>pp</password> </server> <server> <id>docker.r.io</id> <username>zs</username> <password>pp</password> </server> </servers> <!-- mirrors | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. |--> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <!-- mirror <mirror> <id>public</id> <name>Nexus Public Mirror</name> <url>http://repo.thunisoft.com/maven2/content/groups/public-repo/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>nexus</id> <mirrorOf>public-snapshots</mirrorOf> <url>http://repo.thunisoft.com/maven2/content/groups/public-snapshots/</url> </mirror> --> <!--镜像仓库(Mirror Repository)是一种用于加速依赖项下载的机制。在 Maven 中,当你构建项目时,通常需要从远程仓库下载各种依赖项(JAR 文件、插件等)。这些远程仓库可能分布在全球各地, 下载速度可能受到网络延迟和带宽限制的影响,导致构建过程较慢。 为了解决这个问题,Maven 允许配置一个镜像仓库,它是一个位于本地网络或高速服务器上的代理仓库。当你配置了镜像仓库后,Maven 将优先尝试从镜像仓库下载依赖项,而不是直接从远程仓库下载。 如果镜像仓库中已经存在所需的依赖项,下载速度将大大加快,因为镜像仓库通常位于本地网络或高速服务器上,与开发者的构建环境更近,网络延迟较小。 镜像仓库不保存所有的依赖项,它只会缓存从远程仓库下载的依赖项。当 Maven 需要下载依赖项时,首先尝试从镜像仓库获取。 如果在镜像仓库中找不到所需的依赖项,Maven 会继续从配置的其他远程仓库下载。--> <mirror> <id>rep</id> <!-- 用于指定该镜像仓库代理的目标仓库。它的作用是告诉 Maven 该镜像仓库要替代哪个远程仓库的下载请求。 当前配置表示这个镜像仓库将代理所有远程仓库的请求,但会排除掉 ID 分别为 "private1"、"private2" 和 "private3" 的远程仓库,这些仓库的请求将不会由这个镜像仓库来处理。 这样的配置是有用的,例如,当你有多个私有仓库,并且想要一个镜像仓库来加速下载公共仓库的依赖项,同时不影响私有仓库的下载请求。通过排除私有仓库的 ID,确保私有仓库的请求不会被这个镜像仓库所代理,避免了可能出现的下载问题--> <mirrorOf>*,!private1,!private2,!private3</mirrorOf> <url>http://maven.r.io/</url> </mirror> </mirrors> <!-- profiles | This is a list of profiles which can be activated in a variety of ways, and which can modify | the build process. Profiles provided in the settings.xml are intended to provide local machine- | specific paths and repository locations which allow the build to work in the local environment. | | For example, if you have an integration testing plugin - like cactus - that needs to know where | your Tomcat instance is installed, you can provide a variable here such that the variable is | dereferenced during the build process to configure the cactus plugin. | | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles | section of this document (settings.xml) - will be discussed later. Another way essentially | relies on the detection of a system property, either matching a particular value for the property, | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'. | Finally, the list of active profiles can be specified directly from the command line. | | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact | repositories, plugin repositories, and free-form properties to be used as configuration | variables for plugins in the POM. | Profile是一种用于在不同环境或需求下为项目定义不同配置选项的机制。 通过使用 Profile,你可以根据不同的情况激活或禁用特定配置,从而实现项目的定制化构建 |--> <profiles> <profile> <id>env1</id> <!-- 在 Maven 的 Profile 中,你可以通过 <repositories> 元素来定义特定的仓库配置。 这允许你在不同的环境或需求下为项目指定不同的远程仓库,从而实现定制化的依赖管理 --> <repositories> <repository> <id>releases</id> <url>http://xxxxxx</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>rep-plugin</id> <url>http://xxxxxxxx</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> <!-- profile | Specifies a set of introductions to the build process, to be activated using one or more of the | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> | or the command line, profiles have to have an ID that is unique. | | An encouraged best practice for profile identification is to use a consistent naming convention | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. | This will make it more intuitive to understand what the set of introduced profiles is attempting | to accomplish, particularly when you only have a list of profile id's for debug. | | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. <profile> <id>jdk-1.4</id> <activation> <jdk>1.4</jdk> </activation> <repositories> <repository> <id>jdk14</id> <name>Repository for JDK 1.4 builds</name> <url>http://www.myhost.com/maven/jdk14</url> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories> </profile> --> <!-- | Here is another profile, activated by the system property 'target-env' with a value of 'dev', | which provides a specific path to the Tomcat instance. To use this, your plugin configuration | might hypothetically look like: | | ... | <plugin> | <groupId>org.myco.myplugins</groupId> | <artifactId>myplugin</artifactId> | | <configuration> | <tomcatLocation>${tomcatPath}</tomcatLocation> | </configuration> | </plugin> | ... | | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to | anything, you could just leave off the <value/> inside the activation-property. | <profile> <id>env-dev</id> <activation> <property> <name>target-env</name> <value>dev</value> </property> </activation> <properties> <tomcatPath>/path/to/tomcat/instance</tomcatPath> </properties> </profile> --> </profiles> <!-- activeProfiles | List of profiles that are active for all builds. | 激活指定的环境 <activeProfiles> <activeProfile>alwaysActiveProfile</activeProfile> <activeProfile>anotherAlwaysActiveProfile</activeProfile> </activeProfiles> --> <activeProfiles> <activeProfile>env1</activeProfile> </activeProfiles> </settings>
到此这篇关于Maven下载依赖的顺序及配置文件小结的文章就介绍到这了,更多相关Maven下载依赖顺序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!