Maven单、多仓库配置

1. 单独仓库配置

  • 打开Maven配置文件 apache-maven-3.6.3\conf\settings.xml
  • <mirrors></mirrors>标签里新增一个mirror配置即可。
<mirrors>
    <mirror>
        <id>aliyun</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>*</mirrorOf>
    </mirror>
</mirrors>
标签作用
id镜像的唯一标识
name名称描述
url地址
mirrorOf指定镜像规则,什么情况下从镜像仓库拉取
mirrorOf规则作用
*匹配所有,所有内容都从镜像拉取
external:*除了本地缓存的所有从镜像仓库拉取
central覆盖默认的仓库
repo1,repo2匹配仓库repo1和repo2,使用逗号分割多个远程仓库

比如:镜像配置的规则 <mirrorOf>repo1</mirrorOf>,意为:匹配到目标仓库 <id>repo1</id>

所以maven认为目标仓库central被镜像了; 不再去 https://repo.maven.apache.org/maven2/ 地址下载jar包;

而是去镜像仓库 http://maven.aliyun.com/nexus/content/groups/public/ 下载jar包。

2. 多仓库配置

有时候我们需要的jar包公有仓库没有,此时就必须启用多仓库配置,使maven同时使用私有仓库和公有仓库。

  • <profiles></profiles>标签里配置多个profile配置。
<profiles>
    <profile>
        <id>aliyun-repo</id>
        <repositories>
            <repository>
                <id>aliyun</id>
                <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
    </profile>
    <profile>
        <id>jboss-repo</id>
        <repositories>
            <repository>
                <id>jboss</id>
                <url>http://repository.jboss.org/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
    </profile>
    <profile>
        <id>maven-repo</id>
        <repositories>
            <repository>
                <id>maven2</id>
                <url>http://central.maven.org/maven2/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
    </profile>
<profiles>
  • <activeProfiles></activeProfiles>标签里使用<activeProfile></activeProfile>标签启用配置。
<activeProfiles>
    <activeProfile>aliyun-repo</activeProfile>
    <activeProfile>jboss-repo</activeProfile>
    <activeProfile>maven-repo</activeProfile>
</activeProfiles>