maven多仓库私库模板配置

本文档详细描述了ApacheMaven的settings.xml中关于本地仓库路径、远程仓库镜像列表(包括阿里云、华为云等),以及各种活跃的Maven配置profiles,以便在构建过程中自动选择合适的仓库和编译选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录


尽管可以配置多个镜像服务器(mirrors),Maven 实际上仅使用其中一个作为活动节点,默认情况下,仅有首个配置的镜像服务器会被使用。具体而言,只有当首个镜像服务器无法建立连接时,Maven 才会尝试切换到下一个镜像服务器。然而,我们的期望是:如果 a.jar 在首个镜像服务器中不可得,Maven 应该能够转向列表中的第二个镜像服务器来查找并下载所需的文件,但实际上 Maven 并未按此逻辑操作。

需要注意的是,在配置多个镜像服务器时,mirrorOf 参数不应设置为 " * "。这是因为 " * " 表示匹配所有仓库(repositories),这意味着无论你配置了多少个不同的仓库,只要指定了 " * ",Maven 就会统一通过镜像服务器的地址来访问所需的构件(如某个 jar 文件)。这样一来,实际的仓库地址差异将被忽略,所有请求都将通过所指定的镜像路径进行。

使用完毕请关闭,否则每次更新maven都会非常慢。遍历所有库

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
    <!-- local repository -->
    <localRepository>D:\apache-maven-3.8.5\repository</localRepository>

    <!-- 远程存储库身份验证信息 -->
    <servers>
        <server>
            <id>nexus1</id>
            <username>admin</username>
            <password>password</password>
        </server>
    </servers>

    <!--远程存储库的镜像列表 -->
   <mirrors>
	<!--想加快读取速度,打开下面这个 -->
	    <!--
    <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
    -->
    
   <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>central</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
 
      <mirror>
         <id>aliyun-public</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun public</name>
         <url>https://maven.aliyun.com/repository/public</url>
     </mirror>
     <mirror>
         <id>aliyun-central</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun central</name>
         <url>https://maven.aliyun.com/repository/central</url>
     </mirror>
     <mirror>
         <id>aliyun-spring</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun spring</name>
         <url>https://maven.aliyun.com/repository/spring</url>
     </mirror>
     <mirror>
         <id>aliyun-spring-plugin</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun spring-plugin</name>
         <url>https://maven.aliyun.com/repository/spring-plugin</url>
     </mirror>
     <mirror>
         <id>aliyun-apache-snapshots</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun apache-snapshots</name>
         <url>https://maven.aliyun.com/repository/apache-snapshots</url>
     </mirror>
     <mirror>
         <id>aliyun-google</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun google</name>
         <url>https://maven.aliyun.com/repository/google</url>
     </mirror>
     <mirror>
         <id>aliyun-gradle-plugin</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun gradle-plugin</name>
         <url>https://maven.aliyun.com/repository/gradle-plugin</url>
     </mirror>
     <mirror>
         <id>aliyun-jcenter</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun jcenter</name>
         <url>https://maven.aliyun.com/repository/jcenter</url>
     </mirror>
     <mirror>
         <id>aliyun-releases</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun releases</name>
         <url>https://maven.aliyun.com/repository/releases</url>
     </mirror>
     <mirror>
         <id>aliyun-snapshots</id>
         <mirrorOf>central</mirrorOf>
         <name>aliyun snapshots</name>
         <url>https://maven.aliyun.com/repository/snapshots</url>
     </mirror>

        <mirror>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <url>https://repo1.maven.org/maven2/</url>
            <mirrorOf>central</mirrorOf>
        </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 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.
     |
     |-->
    <profiles>
        <profile>
            <id>jdk11</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>11</jdk>
            </activation>
            <properties>
                <maven.compiler.source>11</maven.compiler.source>
                <maven.compiler.target>11</maven.compiler.target>
                <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
            </properties>
        </profile>
        
        <profile>
            <id>jdk21</id>

            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>21</jdk>
            </activation>
            <properties>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>
                <maven.compiler.compilerVersion>21</maven.compiler.compilerVersion>
            </properties>
        </profile>
        

        <profile>
            <id>chinese</id>
            <repositories>
                <repository>
                    <id>aliyun</id>
                    <url>https://maven.aliyun.com/repository/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
                <repository>
                    <id>huaweicloud</id>
                    <url>https://repo.huaweicloud.com/repository/maven/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

        <profile>
            <id>spring-all</id>
            <repositories>
                <repository>
                    <id>spring</id>
                    <url>https://maven.aliyun.com/repository/spring</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
                <repository>
                    <id>spring-plugin</id>
                    <url>https://maven.aliyun.com/repository/spring-plugin</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
                <repository>
                    <id>spring-milestone</id>
                    <url>https://repo.spring.io/milestone/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
                <repository>
                    <id>spring-snapshot</id>
                    <url>https://repo.spring.io/snapshot</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

        <profile>
            <id>google</id>
            <repositories>
                <repository>
                    <id>google</id>
                    <url>https://maven.aliyun.com/repository/google</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

        <profile>
            <id>apache-snapshots</id>
            <repositories>
                <repository>
                    <id>apache-snapshots</id>
                    <url>https://maven.aliyun.com/repository/apache-snapshots</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

        <profile>
            <id>gradle-plugin</id>
            <repositories>
                <repository>
                    <id>gradle-plugin</id>
                    <url>https://maven.aliyun.com/repository/gradle-plugin</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

        <profile>
            <id>maven-all</id>
            <repositories>
                <repository>
                    <id>maven2-repo1</id>
                    <url>https://repo1.maven.org/maven2</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <!-- 对于所有构建都是活动的配置文件列表-->
    <activeProfiles>
        <activeProfile>chinese</activeProfile>
        <activeProfile>spring-all</activeProfile>
        <activeProfile>google</activeProfile>
        <activeProfile>apache-snapshots</activeProfile>
        <activeProfile>maven-all</activeProfile>
        <activeProfile>gradle-plugin</activeProfile>
    </activeProfiles>
</settings>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最难不过坚持丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值