Maven settings.xml配置

Henry Lv3

localRepository

  • <localRepository>:指定本地仓库的位置,默认情况下 Maven 会将依赖下载到用户目录下的 .m2/repository 目录中。可以通过修改该元素来改变本地仓库的位置,例如:
    1
    <localRepository>/path/to/local/repo</localRepository>

settings.xml组成

  • <settings>:根元素,包含所有的配置。
  • <localRepository>:指定本地仓库的位置。
  • <mirrors>:配置镜像仓库,用于加速依赖下载。
  • <servers>:配置服务器认证信息,用于私有仓库的访问。
  • <profiles>:定义不同的构建配置,可以根据需要启用或禁用。
  • <activeProfiles>:指定当前激活的配置文件。
  • <proxies>:配置代理服务器,用于访问外部网络。

mirrors

  • <mirror>:定义“仓库镜像/仓库重定向”配置。
    • <id>:镜像的唯一标识符。
    • <mirrorOf>:指定该镜像适用于哪些仓库,可以使用通配符。
      • central
        镜像 id 为 central 的仓库。
      • *
        镜像所有仓库。
      • external:*
        镜像所有外部仓库,不包括本地仓库。
      • external:*,!smartwarehouse-maven-release,!smartwarehouse-maven-snapshot
        镜像所有外部仓库,但排除你自己的私有 release / snapshot 仓库。
    • <url>:镜像仓库的URL地址。
    • <layout>:指定仓库布局,通常为default

servers

  • <server>:定义服务器认证信息。
    • <id>:服务器的唯一标识符,对应于仓库的<id>
      • 要与pom.xml中一致,即distributionManagement.repository.id == settings.servers.server.id;distributionManagement.snapshotRepository.id == settings.servers.server.id
      • 要与settings.xml的的id相同,即repository.id == server.id;pluginRepository.id == server.id
    • <username>:访问仓库所需的用户名。
    • <password>:访问仓库所需的密码。

profiles

  • <profile>:定义一个构建配置文件。
    • <id>:配置文件的唯一标识符。
    • <activation>:指定激活条件,可以根据操作系统、JDK版本等条件激活。
    • <properties>:定义自定义属性,可以在构建过程中使用。
    • <repositories>:指定该配置文件下的仓库列表。配置的是项目依赖仓库。也就是 Maven 去哪里下载项目 pom.xml 中 、parent、BOM 里声明的依赖。
    • <pluginRepositories>:指定该配置文件下的插件仓库列表。配置的是Maven 插件仓库。Maven 插件不是你的业务代码依赖,而是 Maven 在构建过程中使用的工具。
      • 执行 mvn test 会使用 maven-surefire-plugin
      • 执行 mvn package 会使用 maven-jar-plugin、spring-boot-maven-plugin
      • 执行 mvn deploy 会使用 maven-deploy-plugin

activeProfiles

  • <activeProfile>:指定当前激活的配置文件的<id>

Maven声明周期

  • validate:校验项目
  • compile:编译项目
  • test:运行测试
  • package:打包制品
  • verify:验证打包结果
  • install:安装到本地Maven库
  • deploy:部署到远程Maven库
  • 标题: Maven settings.xml配置
  • 作者: Henry
  • 创建于 : 2026-06-29 16:12:51
  • 更新于 : 2026-07-01 09:36:58
  • 链接: https://mybetterworks.github.io/2026/06/29/Maven-settings-xml配置/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
Maven settings.xml配置