maven添加指定仓库和镜像

maven历史版本下载地址:https://archive.apache.org/dist/maven/maven-3/
maven版本和java版本的关系:https://maven.apache.org/docs/history.html

在 setting.xml 中配置认证信息(后续拉取jar和上传jar都需要用到)

<settings>
    ...
    <servers>
        <server>
            <id>repository_id1</id>  <!--和pom.xml 或 setting.xml中的仓库id保持一致-->
            <username>admin</username>
            <password>admin123</password>
        </server>
        <server>
            <id>repository_id2</id>  <!--和pom.xml 或 setting.xml中的仓库id保持一致-->
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>
</settings>

项目中添加仓库

有两种方式
1.在pom.xml中添加(优先级高,开发环境这么搞,生产环境一般用第2种)
2.在setting.xml中添加

在pom.xml中添加

<!--声明一个或多个远程仓库  -->
<repositories>
    <!-- 声明一个 Nexus 私服上的仓库  -->
    <repository>
        <!--仓库id -->
        <id>nexus_id</id>
        <!-- 仓库的名称 随便写 -->
        <name>nexus_name</name>
        <!--仓库的地址  -->
        <url>nexus_repository_url</url>
        <!-- 是否开启该仓库的 release 版本下载支持 -->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!-- 是否开启该仓库的 snapshot 版本下载支持 -->
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<!-- 声明一个或多个远程插件仓库 -->
<pluginRepositories>
    <!--声明一个 Nexus 私服上的插件仓库  -->
    <pluginRepository>
        <!--插件仓库 id -->
        <id>nexus_id</id>
        <!--插件仓库 名称 -->
        <name>nexus_name</name>
        <!-- 配置的插件仓库的地址 -->
        <url>nexus_repository_url</url>
        <!-- 是否开启该插件仓库的 release 版本下载支持 -->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!-- 是否开启该插件仓库的 snapshot 版本下载支持 -->
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

在setting.xml中添加

<profiles>
    <profile>
        <id>nexus</id>
        <!--声明一个或多个远程仓库  -->
        <repositories>
            <!-- 声明一个 Nexus 私服上的仓库  -->
            <repository>
                <!--仓库id  -->
                <id>nexus_id</id>
                <!-- 仓库的名称 -->
                <name>nexus_name</name>
                <!--仓库的地址  -->
                <url>nexus_repository_url</url>
                <!-- 是否开启该仓库的 release 版本下载支持 -->
                <releases>
                    <enabled>true</enabled>
                </releases>
                <!-- 是否开启该仓库的 snapshot 版本下载支持 -->
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <!-- 声明一个或多个远程插件仓库 -->
        <pluginRepositories>
            <!--声明一个 Nexus 私服上的插件仓库  -->
            <pluginRepository>
                <!--插件仓库 id -->
                <id>nexus_id</id>
                <!--插件仓库 名称 -->
                <name>nexus_name</name>
                <!-- 配置的插件仓库的地址 -->
                <url>nexus_repository_url</url>
                <!-- 是否开启该插件仓库的 release 版本下载支持 -->
                <releases>
                    <enabled>true</enabled>
                </releases>
                <!-- 是否开启该插件仓库的 snapshot 版本下载支持 -->
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

<!--默认启用哪个仓库-->
<activeProfiles>
    <activeProfile>nexus_id</activeProfile>
</activeProfiles>

项目下载依赖的顺序

在这里插入图片描述

怎么理解mirrorof

在setting.xml中声明镜像

<mirrors>
    <mirror>
        <id>xx</id>
        <name>xx</name>
        <mirrorOf>*</mirrorOf><!--就是仓库的id, 配置*表示:拦截所有对仓库的请求,由镜像url代理-->
        <url>http://ip:port/nexus/xx</url>
    </mirror>
</mirrors>

mirrorOf其实就是repositoryId
比如one.jar依赖,本来要去仓库A才能下载,但当镜像中的mirrorOf为仓库A的id,则会跳转到镜像xx的地址http://ip:port/nexus/xx中去找该依赖,而不会再去仓库A中找

部署依赖到 Nexus

1.配置项目的 POM 文件。

<project>
    ...
    <distributionManagement>
        <repository>
            <id>repository_id1</id>
            <url>http://ip1:port1/xxxxxx</url>
        </repository>
        <snapshotRepository>
            <id>repository_id2</id>
            <url>http://ip2:port2/xxxxxx</url>
        </snapshotRepository>
    </distributionManagement>
</project>

3.使用 mvn 命令部署构件。
mvn deploy

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Maven是一个Java项目的自动化构建工具,而Maven仓库是指存储项目所需各种依赖库的集合。在进行Java项目开发时,我们需要依赖一些类库或工具,这些依赖需要通过Maven进行管理。 阿里云Maven仓库是一个基于HTTP协议的仓库,为Maven项目提供了访问速度高、可靠性好、托管服务等方便的条件。阿里云Maven提供了集中式和分布式两种仓库方式,用户可以根据自己的需求选择不同的方式,同时还提供了多种镜像地址选择,用户可以根据自身情况来选择使用不同的镜像地址。 阿里云Maven仓库镜像是指对阿里云Maven仓库进行的复制,并将其放到其他位置,以提高访问速度和稳定性。用户可以使用阿里云Maven仓库镜像地址获取所需的依赖,从而加快项目的构建速度。使用阿里云Maven仓库镜像的好处是,可以通过镜像快速获取所需依赖,不必每次从原始仓库下载,大大缩短了构建时间和依赖库下载时间,提高了开发效率。同时,阿里云Maven仓库镜像也提供了多种网络环境下的镜像地址选择,可以帮助用户根据实际情况选择最适合自己的镜像地址,保证项目开发的高效性和稳定性。 ### 回答2: Maven 仓库Maven 项目所使用的构件存储库,其中包含了各种项目依赖、插件和构建产物。而阿里云提供了可供用户使用的 Maven 仓库镜像。 使用阿里云 Maven 仓库镜像可以带来很多好处。首先,由于阿里云 Maven 仓库镜像分布在全球范围内,可以极大地提高下载速度,减少下载时间。其次,由于该镜像是由阿里云提供的,因此可以保证镜像的更新和安全,确保用户获得的是正确、可靠的构件。 在项目中使用阿里云 Maven 仓库镜像也很简单。只需在项目的 Maven 配置文件中添加阿里云 Maven 仓库地址,然后在项目构建时,Maven 将从该镜像中获取所需的构件。同时,用户也可以通过 Maven 的设置来指定阿里云 Maven 仓库镜像作为默认仓库,方便以后的操作。 总之,使用阿里云 Maven 仓库镜像不仅能够提高项目的构建效率,还能保证依赖的安全性和可靠性,是项目管理中必不可少的一步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值