maven仓库与jar查找

简介

maven上手非常容易,是因为maven提供了很多默认配置,不过有时候,面对一些稍微复杂的情况,需要我们自己配置,我们就需要了解一点maven的原理。

例如,公司有一个私服,但是很多子公司在使用,每个公司的repository不一样,有时候有些jar只放在他们自己的repository中,所以需要访问他们的repository,同时也需要访问自己的repository,这时应该如何配置呢?

查找jar包顺序

  1. 查找本地仓库(maven配置文件settings.xml中localRepository配置)
  2. 查找全局仓库配置(setting.xml文件的profile中配置)
  3. 查找项目仓库配置(pom.xml文件中profile配置)
  4. 查找项目仓库配置(pom.xml文件中repository配置)
  5. 查找中央仓库(默认https://repo.maven.apache.org/maven2)

匹配到仓库之后,如果这个仓库如果有镜像(mirror),就会使用mirror配置的仓库地址。

jar包查找顺序

配置本地仓库

maven的conf目录下settings.xml中localRepository配置

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

全局仓库配置

maven的conf目录下settings.xml中profile配置,如果私服规划比较好,基本不会变,就可以使用全局配置,这样可以避免在每个项目中配置。

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  
    <profiles>
        <profile>
            <id>aliyun-repository</id>
            <repositories>
                <repository>
                    <id>aliyun</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <!--允许下载使用release版本-->
                        <enabled>true</enabled>
                        <!--设置从远程更新的频率,默认是daily -->
                        <!--三种取值,alway:总是更新,never:从不更新,interval:X 每隔X分钟更新 -->
                        <updatePolicy>daily</updatePolicy>
                        <!--用来配置maven检查校验和文件的策略-->
                        <!--当构建部署到本地仓库的时候 会同时部署对应的校验和文件-->
                        <!--有三种取值,ignore:忽略校验,fail:校验失败停止构建,warn:输出警告信息-->
                        <checksumPolicy>ignore</checksumPolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>owner-repository</id>
            <repositories>
                <repository>
                    <id>owner</id>
                    <name>自己组织的代码库</name>
                    <url>http://192.168.5.5:7777/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>other-repository</id>
            <repositories>
                <repository>
                    <id>other</id>
                    <name>公司内部其他组织的仓库</name>
                    <url>http://192.168.5.5:7777/nexus/content/groups/group-share</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <!-- 激活仓库配置 -->
    <activeProfiles>
        <activeProfile>aliyun-repository</activeProfile>
        <activeProfile>owner-repository</activeProfile>
        <activeProfile>other-Repository</activeProfile>
    </activeProfiles>
</settings>

多个激活的repository,会按顺序依次查找。

为什么有个默认的中央仓库central会生效,是因为Maven安装目录下/lib/maven-model-builder-${version}.jar中的org\apache\maven\model\pom-4.0.0.xml中配置了:

 <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

项目仓库配置

项目配置在项目的pom.xml中,项目配置仓库基本都是用的是repositories

如果,项目要访问一些特殊的仓库,就需要单独配置项目仓库,例如,很多商用库就通过这种方式配置

<repositories>
    <repository>
        <id>maven-public</id>
        <name>私服公用仓库地址</name>
        <url>http://192.168.5.5:7777/nexus/content/groups/public</url>
    </repository>
    <repository>
        <id>maven-thirdparty</id>
        <name>私服第三方包仓库地址</name>
        <url>http://192.168.5.5:7777/nexus/content/repositories/thirdparty/</url>
    </repository>
</repositories>

镜像

镜像配置:

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <mirror>
            <id>aliyun</id>
            <name>阿里云仓库地址</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <!--中央仓库central的镜像,就是所有访问中央仓库的都会去访问阿里云仓库了-->
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
</settings>
  • id:用于标识mirror
  • name:更像是mirror的说明
  • url:这个mirror的仓库地址
  • mirrorOf:哪个仓库的镜像

mirror是镜像,也意味着,只会查找第一个匹配到的mirror,查找顺序是按mirror的id字典序查找,而不是配置文件中的先后顺序(这操作比较迷,一是不直观,二是我为了顺序得去改id,相当于id有2个职责了)

只会找第一个匹配的mirror的前提是mirror可以访问,所以更准确的说是找第一个可以访问的mirror。

例如,central中央仓库配置了2个mirror,id分别是A和B,现在要找一个org.meeet.helper.jar包,如果A的url能ping通,就只会在这个url仓库查找,哪怕没有找到,都不会去B的url仓库找。只有当A的url不能ping通的时候,才会去B的url仓库查找。

mirrorOf匹配规则:

  1. *:匹配所有远程仓库
  2. external:*:匹配所有外部仓库(除了使用:localhost、file://协议的远程仓库)
  3. repo1,repo2:匹配仓库repo1和repo2,使用逗号分隔多个远程仓库
  4. *,!repo:匹配除repo外的所有远程仓库(感叹号将仓库从匹配中排除)

要分清mirror和repository,mirror只是repository的镜像,很多初学的朋友总想通过mirror来配置repository仓库,所以会出现根本不生效、或者一些奇怪复杂的配置。

所以,mirror尽量就只配置一个私服仓库,如公司的私服或者阿里云的仓库来作为central中央仓库的镜像,用于加快访问。

如果,需要访问多个仓库,尽量通过前面介绍的profile和repository来配置,如果自己的Nexus私服还可以通过配置group来处理。

Nexus

Nexus是Maven仓库管理器,前面我们提到了私服(私有仓库),就可以使用Nexus,它能简化内部仓库的维护和外部仓库的访问。

例如,公司内部,我们就可以使用Nexus搭建私服,大家都配置私服地址,然后由Nexus去读取外部的库,当然也可以管理发布内部的库。

Nexus有4种仓库类型:

  1. hosted:本地仓库,主要用于内部项目的发布(maven-releases、maven-snapshots)
  2. proxy:代理仓库,外部仓库代理,例如中央仓库(maven-central)
  3. group:组仓库,逻辑库,可以配置多个hosted与proxy(maven-public)
  4. virtual:虚拟仓库,适配maven1

Nexus仓库类型

私服发布配置

<distributionManagement>
    <snapshotRepository>
        <!-- id与settings.xml中server.id配置一致-->
        <id>snapshot</id>
        <url>http://192.168.5.5:7777/repository/maven-snapshots/</url>
    </snapshotRepository>
    <repository>
        <!-- id与settings.xml中server.id配置一致-->
        <id>release</id>
        <url>http://192.168.5.5:7777/repository/maven-releases/</url>
    </repository>
</distributionManagement>

配置私服用户密码

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>release</id>
            <username>admin</username>
            <password>123456</password>
        </server>
        <server>
            <id>snapshot</id>
            <username>admin</username>
            <password>123456</password>
        </server>
    </servers>
</settings>

打包插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
        <tagBase>${project.artifactId}-${project.version}</tagBase>
        <tagNameFormat>v@{project.version}</tagNameFormat>
        <autoVersionSubmodules>true</autoVersionSubmodules>
        <generateReleasePoms>false</generateReleasePoms>
        <!-- 跳过单元测试 -->
        <arguments>-DskipTests</arguments>
        <!-- 执行目标是install还是deploy,install安装本地,deploy部署到远程仓库 -->
        <goals>deploy</goals>
    </configuration>
</plugin>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值