Skip to content

配置总览

xml
<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
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">

    <localRepository>D:/maven/repo</localRepository>

    <mirrors>
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>Aliyun Maven</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
    </mirrors>

    <servers>
        <server>
            <id>releases</id>
            <username>nexususer</username>
            <password>nexuspass</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>nexususer</username>
            <password>nexuspass</password>
        </server>
    </servers>

    <proxies>
        <proxy>
            <id>example-proxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>proxy.example.com</host>
            <port>8080</port>
            <username>proxyuser</username>
            <password>proxypass</password>
            <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
        </proxy>
    </proxies>

    <profiles>
        <profile>
            <id>dev</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://repo.maven.apache.org/maven2</url>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>

    <pluginGroups>
        <pluginGroup>org.example.plugins</pluginGroup>
    </pluginGroups>
</settings>

<localRepository>

  • 说明:本地仓库路径(windows默认 C:\Users\你的用户名.m2\settings.xml)

<mirrors>

  • 说明:配置镜像地址,加速依赖的下载

  • 配置项说明

    配置项说明
    id唯一标识
    mirrorOf表明哪些依赖项从当前镜像下载,* 表示全部
    name名称
    url镜像地址

<servers>

  • 说明:认证信息(如部署到私服时的账号和密码)

  • 配置项说明

    配置项说明
    id和pom.xml文件中的 <distributionManagement> 中的信息对应,具体对应关系移步 idea上传jar包至本地仓库和私服 查看
    username和password用于认证

<proxies>

  • 说明:有时候你用的电脑 不能直接访问外网(比如公司网络有防火墙),需要通过代理服务器才能上网,这个标签就是告诉 Maven:遇到联网需求时,先联系代理服务器,由代理去帮你访问外部世界

  • 配置项说明

    配置项说明
    id代理的标识名(随便起,方便管理)
    active是否启用这个代理(true / false
    protocol代理的协议类型(http / https
    host代理服务器的主机名或IP
    port代理服务器的端口号
    username代理服务器账号(如果需要登录)
    password代理服务器密码
    nonProxyHosts哪些主机不需要代理,用 `

<profiles>

  • 说明:自定义配置集(如不同环境的仓库),比如项目开发时用公司内网仓库,回家后用互联网仓库,这样在使用mvn时通过 -P 指定使用不同的profile,如 mvn clean install -Pwork

    xml
    <profiles>
        <profile>
            <id>work</id>
            <repositories>
                <repository>
                    <id>company-repo</id>
                    <url>http://repo.company.com/maven2</url>
                </repository>
            </repositories>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>home</id>
            <repositories>
                <repository>
                    <id>aliyun</id>
                    <url>https://maven.aliyun.com/repository/public</url>
                </repository>
            </repositories>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>
  • 配置项说明

    配置项说明
    idprofile 的唯一标识
    repositories配置本 profile 下的仓库
    activation设置激活条件,告诉 Maven 什么时候自动用这个 profile
    子标签<activeByDefault>:是否默认启用(true/false)

    子标签<property>:当某个属性存在时激活(如 -Denv=prod)
    子标签<os>:指定操作系统时激活
    properties定义一些变量(类似于全局变量),这些变量可以在 pom.xml 里用 ${变量名} 访问
    • activation子标签举例

      xml
      <activation>
          <activeByDefault>true</activeByDefault>
      </activation>
      xml
      <activation>
          <property>
              <name>env</name>
              <value>prod</value>
          </property>
      </activation>
    • properties标签举例

      xml
      <!-- settings文件中定义属性名 -->
      <properties>
          <java.version>17</java.version>
      </properties>
      xml
      <!-- pom.xml中任意位置使用该属性 -->
      ${settings.java.version}

<activeProfiles>

  • 说明:告诉 Maven默认启用哪些 profile,也就是不加 -P 参数时,Maven 自动用这些 profile

  • 配置项说明

    配置项说明
    activeProfile写上你想默认激活的 profile id(与 <profiles> 中的 id 对应)

<pluginGroups>

  • 说明:用于配置自定义插件的前缀,比如公司开发了自己的maven插件,groupId是top.qnmdmyy.plugins,那么指定pluginGroups后,使用时就可以简化mvn指令了,就从 mvn top.qnmdmyy.plugins:testplugin:run 变成了 mvn testplugin:run

  • 配置项说明

    配置项说明
    pluginGroup指定groupId

MIT版权,未经许可禁止任何形式的转载