一文搞定maven仓库相关配置

配置文件

首先,要区分下面两个配置文件的位置和作用,很多时候发现自己修改了配置文件,但是没有生效,就应该检查一下配置文件是否选对了。

用户配置: u s e r . h o m e / . m 2 / s e t t i n g s . x m l 全 局 配 置 ( 命 令 行 可 通 过 − g s 参 数 覆 盖 ) : {user.home}/.m2/settings.xml 全局配置(命令行可通过-gs参数覆盖): user.home/.m2/settings.xml(gs){maven.home}/conf/settings.xml

仓库配置

配置项说明
server仓库服务器配置,例如私服需要鉴权,就要配置这一个
mirror用于替代repository镜像地址
repositoryjar包仓库地址

仓库(repository)

maven的仓库就是存放jar包的地方,主要是为了方便项目查找依赖的jar包。

默认,都会去中央仓库查找(http://repo1.maven.org/maven2/),很多朋友配置http://repo2.maven.org/maven2/做为镜像。

大家都去中央仓库查找和下载jar包,中央仓库压力就会很大,于是就有了私有仓库

私有仓库就是,自己搞个服务器,然后去定时同步中央仓库的jar包等数据,相当于加了缓存。

有了私有仓库,就可以对大家说,同志们,朋友们,大家可以不用去中央仓库找了,中央仓库太慢,你们把私有仓库配置为我服务器的地址,我这里快。

一般公司都会自己弄一个私有仓库,然在pom文件中就可以大致像下面这样配置了:

<repositories>
    <repository>
        <id>releases</id>
        <url>http://127.0.0.1/nexus/content/repositories/releases</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>snapshots</id>
        <url>http://127.0.0.1/nexus/content/repositories/snapshots</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>public</id>
        <url>http://127.0.0.1/nexus/content/groups/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

pom中的repository也可以用来配置一些特定的三方库(maven还是会把它当做私用仓库)

<repositories>
    <repository>
        <id>projectlombok.org</id>
        <url>http://projectlombok.org/mavenrepo</url>
    </repository>
</repositories>

当然,除了在pom文件中,也可以在setting中通过profile来进行配置,如:

<profiles>
    <profile>
      <id>aliyun</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>nexus163</id>
        <repositories>
            <repository>
                <id>nexus163</id>
                <name>163</name>
                <url>http://mirrors.163.com/maven/repository/maven-public/</url>
                <layout>default</layout>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                </releases>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus163</id>
                <name>163</name>
                <url>http://mirrors.163.com/maven/repository/maven-public/</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                </releases>
            </pluginRepository>
        </pluginRepositories>
    </profile>
<profiles>

上面配置的是别人公开仓库做为私有仓库,配置profile需要激活,一是通过命令参数:

mvn clean install -Paliyun

就是-P后面加上profile的id就激活对应的id配置。

也可以配置默认激活:

<activeProfiles>
    <activeProfile>aliyun</activeProfile>
    <activeProfile>nexus163</activeProfile>
</activeProfiles>

有朋友可能会问,上面不是用了别人的服务器么?怎么还叫私有仓库?

我只想说,朋友,长在新中国,和平社会共建共享,别人的服务器怎么就不当做自己的私有仓库了!

对于霸气的maven来说,除了中央仓库,其他的都叫私有仓库。

如果,你实在心有芥蒂,还可以使用镜像(mirror)

mirror

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

上面的这个mirror配置就相当于,给下面这个id为central中央仓库当做镜像。

简单点说,所有访问http://repo1.maven.org/maven2,都被切换为http://maven.aliyun.com/nexus/content/groups/public/

<repository>
    <id>central</id> 
    <url>http://repo1.maven.org/maven2</url> 
    <releases><enabled>true</enabled></releases> 
    <snapshots><enabled>true</enabled></snapshots>
</repository>

mirror中变化比较多的就是mirrorOf:

  1. *:匹配所有仓库请求,即将所有的仓库请求都转到该镜像上
  2. repo1,repo2:repo1和repo2的请求重定向到镜像,使用逗号分隔多个远程仓库
  3. *,!myrep:匹配所有仓库请求,myrep除外,使用感叹号将仓库从匹配中排除
  4. external:*: 除了本地缓存jar包,所有从镜像仓库拉取

注意:mirror只会有一个生效,配置多个mirror的情况下,默认第一个生效,如果当前一个mirror链接不可用才会顺序查找下一个。

不要想通过mirror来实现,这个仓库没有找到jar包,就在另一个仓库找这种功能。

查找jar依赖流程与顺序

在maven执行的时候,如果有依赖的jar包:

  1. 查找本地仓库
  2. 如果本地仓库没有找到,且配置了私有仓库,就会去私有仓库查找
  3. 如果没有找到,就去中央仓库centeral(http://repo1.maven.org/maven2)找
  4. 如果配置了mirror,并且mirror的mirrorOf是centeral,那就会去mirror这个镜像地址下载jar包

每个仓库都可能有镜像,如果仓库有镜像,就会通过镜像(mirror)去找,如果仓库配置了镜像,镜像没有找到,就算仓库中有对应jar,也不会去仓库找了。

不用去区分和记忆什么setting、pom、profile的仓库查找优先级,哪些考虑变量太多,很多测试并不充分,结论也是错的。

只需要按正常的逻辑记住:找本地仓库,如没有找私有仓库,没有找中央仓库,仓库有镜像就通过镜像找。

至于setting、pom、profile,按逻辑也很好记忆,肯定是按使用方便的顺序。

例如,使用了动态参数profile会覆盖pom,pom会覆盖setting。

想一想,如果命令行的动态参数profile不能覆盖pom,那要这个参数什么用?如果pom不能覆盖setting,那么就是工程的配置覆盖不了全局配置,那不是太扯了。

server

 <servers>
    <server>
        <id>releases</id>
        <username>username</username>
        <password>pass</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>username</username>
        <password>pass</password>
    </server>
</servers>

私有仓库,有些是需要授权的,比如访问一下不公开的jar包的时候,多半是需要用户名和密码的,server就是配置相同id对应仓库repository的用户名和密码的配置项。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值