一、Maven 配置文件概述

(一)settings.xml 优先级

Maven 的 settings.xml 有两个关键的存在位置:

全局配置:位于 Maven 安装目录的 conf/settings.xml。这个配置文件是系统级别的,对所有使用该 Maven 安装环境的用户生效。它就像是一个公共的规则手册,规定了一些通用的设置,比如默认的远程仓库地址、代理服务器配置等。就像公司里有一套公共的开发规范,所有员工都要遵循,全局配置就类似于这样的规范,对所有开发者一视同仁。

用户配置:在用户目录的.m2/settings.xml。这个配置文件是个性化的,仅针对当前用户起作用。它允许每个用户根据自己的需求,对 Maven 进行定制。比如,你可以在这里指定自己偏好的本地仓库路径,或者配置自己的私服认证信息。用户配置只影响当前用户的 Maven 使用。

当这两个配置文件同时存在时,会遵循 “就近优先” 的原则,即用户配置会覆盖全局配置。这就像在一个项目中,个人的特殊需求如果与公共规范冲突,以个人需求为准,确保每个开发者都能灵活调整 Maven 配置以适应自己的开发环境。

(二)配置优先级体系

Maven 的配置优先级从高到低依次为:

1.项目级 pom.xml:这是项目专属的配置文件,定义了项目的基本信息、依赖关系、构建配置等。它的优先级最高,因为它是针对当前项目的具体设置,对项目的构建和运行起着直接的控制作用。比如,项目中特定的依赖版本、插件配置等,都可以在 pom.xml 中精确指定,以满足项目的独特需求。

2.用户级.m2/settings.xml:用户级别的配置文件,主要用于设置用户个人的偏好和环境相关的配置,如本地仓库位置、私服认证等。它的优先级次之,在项目没有明确指定某些配置时,会参考用户级别的设置。

3.全局级 conf/settings.xml:全局配置文件,提供了系统层面的默认设置。它的优先级最低,只有在项目级和用户级都没有相关配置时,才会采用全局配置。

理解这一配置优先级体系,能有效避免配置冲突。例如,在一个团队开发项目中,项目级的 pom.xml 定义了项目所需的依赖版本,即使某个开发者在自己的用户配置中不小心设置了不同的依赖版本,也不会影响项目的正常构建,因为项目级的配置会覆盖用户级的配置,确保了项目构建行为的一致性和稳定性 ,符合预期。

二、核心配置项详解

在深入了解 Maven 的 settings.xml 文件后,接下来我们将详细探讨其中的核心配置项,这些配置项是定制 Maven 行为的关键,能够帮助我们优化项目构建过程,提高开发效率。

(一)本地仓库配置

本地仓库是 Maven 在本地存储下载的依赖库和插件的地方。默认情况下,Maven 使用用户主目录下的.m2/repository 作为本地仓库 。不过,这个默认路径有时并不那么理想,比如 C 盘空间有限,或者你希望将依赖存储到读写速度更快的磁盘分区,这时就需要修改本地仓库的配置。

修改本地仓库路径的方法很简单,只需在 settings.xml 文件中找到localRepository标签,并将其值设置为你想要的路径即可。例如,如果你想将本地仓库设置到 D 盘的 maven-repository 目录下,可以这样配置:

<settings>
   <localRepository>D:\maven-repository</localRepository>
   <!-- 其他配置 -->
</settings>

在进行首次配置时,建议大家手动创建目标目录。因为默认路径会占用C盘很大的磁盘空间,而且让 Maven 自动生成目录,可能会遇到权限问题,导致依赖下载失败。

(二)镜像管理

在 Maven 中,镜像就像是一个 “高速中转站”,如果我们使用maven默认的远程中央仓库,下载速度会非常缓慢。这时,配置一个合适的镜像就可以大大提高下载速度。

1. 镜像配置语法

在 settings.xml 文件中,通过mirrors标签来配置镜像。每个镜像由子标签mirror定义,其中几个关键属性的作用如下:

  • id :镜像的唯一标识符,用于区分不同的镜像,就像每个人都有一个独一无二的身份证号。

  • mirrorOf :指定该镜像所代理的仓库,即替代哪个仓库。这个属性支持通配符,例如:

    * :匹配所有仓库,这意味着所有的仓库请求都会被重定向到这个镜像。

    *, !repoId :排除指定的仓库 repoId,除了这个特定的仓库,其他仓库的请求都会走这个镜像,实现了有针对性的代理。

  • name :镜像的名称,用于描述该镜像,方便我们识别,比如 “阿里云镜像”。

  • url :镜像的地址,也就是我们下载依赖的地址。

在国内,网络访问国外的中央仓库常常会受到限制,导致下载速度缓慢甚至失败。因此,推荐使用阿里云、华为云等国内的镜像源。以阿里云镜像为例,配置如下:

<mirrors>
   <mirror>
       <id>aliyunmaven</id>
       <mirrorOf>*</mirrorOf>
       <name>阿里云公共仓库</name>
       <url>https://maven.aliyun.com/repository/public</url>
   </mirror>
</mirrors>

这样配置后,Maven 在下载依赖时就会优先从阿里云镜像下载,大大提升下载速度。

2. 多镜像场景配置

在某些复杂的项目中,可能需要同时配置多个镜像来代理不同的仓库。当存在多个镜像配置时,Maven 会按照镜像在 settings.xml 文件中的声明顺序,根据属性的匹配情况来选择使用哪个镜像 ,首个激活镜像将被使用。

例如,如果你同时配置了阿里云镜像和华为云镜像,并且阿里云镜像在文件中排在前面,当 Maven 请求下载依赖时,会先检查阿里云镜像是否匹配,如果匹配则从阿里云镜像下载;只有当阿里云镜像无法连接或者没有找到所需依赖时,才会尝试使用华为云镜像。

(三)服务器认证:安全访问私有仓库

在实际项目中,我们有时会使用私有仓库来管理项目的依赖和构件,比如 Nexus、Artifactory 等。当需要从私有仓库下载依赖或者将项目构件部署到私有仓库时,就需要进行身份认证,以确保仓库的安全性。

在 settings.xml 文件中,通过servers标签来配置服务器认证信息。每个服务器认证由子标签server定义,主要属性包括:

  • id :服务器的唯一标识符,这个 id 要与 pom.xml 文件中或repository标签里的 id 一致,就像两把对应的钥匙和锁。

  • username 和 password:访问服务器的用户名和密码。

例如,配置一个 Nexus 私有仓库的认证信息:

<servers>
   <server>
       <id>my-nexus</id>
       <username>admin</username>
       <password>admin123</password>
   </server>
</servers>

需要注意的是,为了安全起见,不要将敏感的认证信息直接写入 pom.xml 文件,因为 pom.xml 可能会被提交到版本控制系统,从而导致信息泄露。而 settings.xml 文件通常只存在于本地开发环境或者构建服务器上,这样可以更好地保护认证信息的安全 。

三、实战配置示例

对于刚接触 Maven 的新手来说,进行一些基础的优化配置就能显著提升开发体验。以下是一个基础优化配置的示例:

<settings>
    <!--本地仓库-->
    <localRepository>d:\\mvn_repo</localRepository>
    <!--镜像-->
    <mirrors>
        <mirror>
            <id>aliyun</id>
            <name>aliyun maven</name>
            <url>https://maven.aliyun.com/nexus/content/groups/public</url>
            <mirrorOf>central,!nexus-releases,!nexus-snapshots</mirrorOf>
        </mirror>
    </mirrors>
    <!-- 企业库 -->
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
        <activeProfile>rdc</activeProfile>
    </activeProfiles>
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus-releases</id>
                    <url>http://dev.maisuitx.com:38081/repository/maven-releases/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>nexus-snapshots</id>
                    <url>http://dev.maisuitx.com:38081/repository/maven-snapshots/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>rdc</id>
            <!-- 覆盖默认的部署仓库配置 -->
            <properties>
                <!-- 指定发布版依赖(非快照版) 的部署仓库 优先级高于pom.xml中的distributionManagement -->
                <!-- 格式为 仓库ID::部署类型::仓库URL -->
                <altReleaseDeploymentRepository>
                    rdc-releases::default::https://packages.aliyun.com/maven/repository/2113787-release-v3o3ys/
                </altReleaseDeploymentRepository>
            <!-- 指定快照版依赖(带-SNAPSHOT后缀) 的部署仓库 -->
                <altSnapshotDeploymentRepository>
                    rdc-snapshots::default::https://packages.aliyun.com/maven/repository/2113787-snapshot-Gqcbdq/
                </altSnapshotDeploymentRepository>
            </properties>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>snapshots</id>
                    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>rdc-releases</id>
                    <url>https://packages.aliyun.com/maven/repository/2113787-release-v3o3ys/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>rdc-snapshots</id>
                    <url>https://packages.aliyun.com/maven/repository/2113787-snapshot-Gqcbdq/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
                <pluginRepository>
                    <id>snapshots</id>
                    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
                <pluginRepository>
                    <id>rdc-releases</id>
                    <url>https://packages.aliyun.com/maven/repository/2113787-release-v3o3ys/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
                <pluginRepository>
                    <id>rdc-snapshots</id>
                    <url>https://packages.aliyun.com/maven/repository/2113787-snapshot-Gqcbdq/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <!--私库凭证-->
    <servers>
        <server>
            <id>nexus-releases</id>
            <username>huangzhen111</username>
            <password>huangzhen111.</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>huangzhen222</username>
            <password>huangzhen222.</password>
        </server>
        <!-- 个人阿里云仓库 -->
        <server>
            <id>rdc-releases</id>
            <username>60d1xxxxxxx73831a5cbc9</username>
            <password>56(zdfsdfTAAf-</password>
        </server>
        <server>
            <id>rdc-snapshots</id>
            <username>60d15aassdff383we5cbc9</username>
            <password>56(TTFgtyuR3</password>
        </server>
    </servers>
</settings>

在这个配置中:

本地仓库配置:将本地仓库设置到 D 盘的 mvn_repo 目录下,避免默认存储在 C 盘导致空间不足的问题,同时也便于管理依赖。

镜像仓库配置:使用阿里云镜像,将默认的中央仓库请求重定向到阿里云公共仓库,大大加快了依赖下载速度,尤其在国内网络环境下效果显著。对于阿里云私有仓库或者nexus私服则不进行重定向。

私服认证配置:配置了私有仓库(Nexus)和阿里云私服的认证信息,包括发布版本和快照版本的仓库认证,确保能够安全地从私有仓库下载和上传构件。

文章作者: Z
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 微博客
工具 运维 基础 maven
喜欢就支持一下吧