maven

maven仓库配置

2019-12-08  本文已影响0人  nextbeginning

简介

maven仓库从存储位置上区分,可以分为本地仓库和远程仓库,项目中依赖jar包时,首先从本地仓库查找,本地仓库没有时候,就会去远程仓库下载至本地。从仓库作用上区分,可以分为release(已发行稳定版)、snapshot(快照版本)。

1.snapshot和release区别

snapshot版本一般开发环境中使用,通常开发环境下版本还不稳定,经常需要修改代码重新发布,snapshot版本发布至仓库后,会自动带一个带日期的后缀存储,客户端每次都会去下载最新的版本。而release版本用于上线后发布的版本,客户端缓存本地后,不会再去更新。
在一个包的pom文件中,把版本号带上-SNAPSHOT结尾即告诉maven仓库此包用SNAPSHOT仓库管理。

2.仓库镜像

因为远程中央仓库的访问量较大,通常来说不够稳定,我们可以设置一个远端仓库的镜像版本,例如阿里云中央仓库、腾讯云中央仓库、自己搭建的私服等。

<mirrors>
    <mirror>
        <id>mirror</id>
        <mirrorOf>*</mirrorOf>
        <name>mirror</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
</mirrors>

mirrorOf表示对哪个仓库id做代理,表示所有仓库。如果我们建议了自己的私服仓库,在maven中配置了两个仓库id为:rdc-releases、rdc-snapshots,想要不对这两个私服仓库代理,则可以配置mirrorOf的值为:,!rdc-releases,!rdc-snapshots

3.仓库配置

如果自己搭建了私服仓库用于deploy自己的jar包,或者用了第三方的仓库。需要配置下才可以用maven deploy命令上传自己的jar包。
在maven的配置文件中,增加repository

<repositories>
    <repository>
        <id>rdc-releases</id>
        <url>https://repo.rdc.aliyun.com/repository/110553-release-OU57qV/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>rdc-snapshots</id>
        <url>https://repo.rdc.aliyun.com/repository/110553-snapshot-S1Aagg/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

访问仓库的用户名密码,需要在server中配置

<servers>
    <server>
        <id>rdc-releases</id>
        <username>***</username>
        <password>******</password>
    </server>
    <server>
        <id>rdc-snapshots</id>
        <username>***</username>
        <password>******</password>
    </server>
</servers>

jar包所在的pom文件中,增加distributionManagement

<distributionManagement>
    <repository>
        <id>rdc-releases</id>
        <url>https://repo.rdc.aliyun.com/repository/110553-release-OU57qV/</url>
    </repository>
    <snapshotRepository>
        <id>rdc-snapshots</id>
        <url>https://repo.rdc.aliyun.com/repository/110553-snapshot-S1Aagg/</url>
    </snapshotRepository>
</distributionManagement>

这样,在使用maven clean deploy之后,maven会根据你的jar包的版本号,如果带-SNAPSHOT结尾,则上传至rdc-snapshots仓库,否则上传至rdc-releases仓库。

4.配置多个发布仓库

通常可能开发环境和线上环境,私服仓库不是同一个,可以通过在项目的pom文件中配置多个profile的形式实现

<profiles>
    <profile>
        <id>ci</id>
        <distributionManagement>
            <repository>
                <id>rdc-releases</id>
                <url>https://repo.rdc.aliyun.com/repository/110553-release-OU57qV/</url>
            </repository>
            <snapshotRepository>
                <id>rdc-snapshots</id>
                <url>https://repo.rdc.aliyun.com/repository/110553-snapshot-S1Aagg/</url>
            </snapshotRepository>
        </distributionManagement>
    </profile>
    <profile>
        ...
    </profile>
</profiles>

之后maven打包的时候通过-P指定profile使包上传到指定仓库,例如maven clean deploy -Pci将上传包至ci配置的仓库

5.同一种类型的仓库配置多个
如果同一种类型的仓库配置了多个或者没配置,例如配置了两个release仓库,个人感觉是按照顺序去下载,找不到才会找下一个。如果都找不到,则会去默认maven中央仓库找

上一篇 下一篇

猜你喜欢

热点阅读