maven仓库配置及搜索顺序

今天在构建 IBPMS(Java Business Process Management,业务流程管理)demo 时,出现依赖的包无法下载,运行环境时公网内网环境,趁此梳理一下 maven 仓库的配置方式,并解一下自己的疑惑。

  1. maven 仓库配置方式
    1.1 maven setting.xml 文件配置
    下面的 setting.xml 文件是最常用的配置,示例中仓库地址配置是公司的私服仓库,只配置了 profile 和镜像 mirror,下面简要介绍一下 profile 和 mirror 两个配置标签的作用。
tbmirror-all * taobao mirror http://mvnrepo.abc.com/mvn/repository
<profiles>
    <profile> 
        <id>test</id> 
        <repositories> 
            <repository> 
                <id>central</id> 
                <url>http://mvnrepo.abc.com/mvn/repository</url> 
            </repository> 
        </repositories> 
        
        <!-- maven插件的仓库从这里下载 -->
        <pluginRepositories> 
            <pluginRepository> 
                <id>central</id> 
                <url>http://mvnrepo.abc.com/mvn/repository</url> 
            </pluginRepository> 
        </pluginRepositories> 
    </profile> 
</profiles> 

<pluginGroups>
    <pluginGroup>com.alibaba.org.apache.maven.plugins</pluginGroup>
    <pluginGroup>com.alibaba.maven.plugins</pluginGroup>
</pluginGroups>

<activeProfiles> 
    <activeProfile>test</activeProfile> 
</activeProfiles> 
镜像 mirror 如果仓库 X 可以提供仓库 Y 存储的所有内容,那么就可以认为 X 是 Y 的一个镜像。换句话说,任何一个可以从仓库 Y 获得的构件,都能够从它的镜像中获取。举个例子,http://maven.net.cn/content/groups/public/ 是中央仓库 http://repo1.maven.org/maven2/ 在中国的镜像,由于地理位置的因素,该镜像能够提供比中央仓库更快的服务。因此,可以配置 Maven 使用该镜像来替代中央仓库,代码如下: ... maven.net.cn one of the central mirrors in china http://maven.net.cn/content/groups/public/ central

*

external:*

repo1,repo2

*,!repo1
1.2 应用 pom.xml 仓库配置方式
这里的配置与 maven setting 类似,不再多余解释,直接引用笔者的 demo 配置,如下:

jboss-public-repository-group JBoss Public Repository Group https://repository.jboss.org/nexus/content/groups/public/ default never daily 2. maven 仓库搜索优先级 1)maven 仓库搜索路径 mavenSearch.png maven 仓库搜索需要分两种场景:

Case 1:如果没有配置 mirrorOf* 的镜像仓库,搜索路径按照下面顺序获取构件(jar)

Step 1:查找本地仓库,如果没有则进入到下一步
Step 2:在全局应用的私服仓库中(setting.xml文件的profile)寻找,如果没有则进入下一步
Step 3:在应用自身的私服仓库中(应用pom.xml配置的repository)寻找,如果没有则进入下一步
Step 4:查找中央仓库,如果没有配置mirror,就默认中央仓库地址 https://repo.maven.apache.org/maven2
Case 2:如果配置有 mirrorOf * 的镜像仓库,则忽略上述规则,只从此仓库获取jar包(详细参考下面示例)

2)场景分析
以博主遇到的场景做分析,xmlpull.jar 包不在公司的远程仓库(http://mvnrepo.abc.com/mvn/repository)中,存在于 远程仓库https://repository.jboss.org/nexus/content/groups/public,结合以下两种场景来说明问题。

场景一:以下配置能从远程仓库中正常拉取 xmlpull.jar 包
原因:按上述搜索顺序,先从在全局应用的私服仓库中(setting.xml文件的profile)寻找,没有找到,则从项目 pom.xml 配置的 repository 找到并下载成功。

maven setting.xml 配置



tbmirror-all
central
taobao mirror
http://mvnrepo.abc.com/mvn/repository

<profiles>
    <profile> 
        <id>test</id> 
        <repositories> 
            <repository> 
                <id>central</id> 
                <url>http://mvnrepo.abc.com/mvn/repository</url> 
            </repository> 
        </repositories> 
        
        <!-- maven插件的仓库从这里下载 -->
        <pluginRepositories> 
            <pluginRepository> 
                <id>central</id> 
                <url>http://mvnrepo.abc.com/mvn/repository</url> 
            </pluginRepository> 
        </pluginRepositories> 
    </profile> 
</profiles> 

<pluginGroups>
    <pluginGroup>com.alibaba.org.apache.maven.plugins</pluginGroup>
    <pluginGroup>com.alibaba.maven.plugins</pluginGroup>
</pluginGroups>

<activeProfiles> 
    <activeProfile>test</activeProfile> 
</activeProfiles> 
应用 pom.xml 仓库配置 jboss-public-repository-group JBoss Public Repository Group https://repository.jboss.org/nexus/content/groups/public/ default never daily 场景二:以下配置不能从远程仓库中拉取 xmlpull.jar 包,报错误信息:one of its dependencies could not be resolved: Could not find artifact xmlpull:xmlpull:jar:1.2.0 in tbmirror-all (http://mvnrepo.abc.com/mvn/repository) 原因:maven setting.xml 配置了 mirror*,只从公司私服上下载 xmlpull.jar 包,未找到则报错

maven setting.xml 配置



tbmirror-all
*
taobao mirror
http://mvnrepo.abc.com/mvn/repository

<profiles>
    <profile> 
        <id>test</id> 
        <repositories> 
            <repository> 
                <id>central</id> 
                <url>http://mvnrepo.abc.com/mvn/repository</url> 
            </repository> 
        </repositories> 
        
        <!-- maven插件的仓库从这里下载 -->
        <pluginRepositories> 
            <pluginRepository> 
                <id>central</id> 
                <url>http://mvnrepo.abc.com/mvn/repository</url> 
            </pluginRepository> 
        </pluginRepositories> 
    </profile> 
</profiles> 

<pluginGroups>
    <pluginGroup>com.alibaba.org.apache.maven.plugins</pluginGroup>
    <pluginGroup>com.alibaba.maven.plugins</pluginGroup>
</pluginGroups>

<activeProfiles> 
    <activeProfile>test</activeProfile> 
</activeProfiles> 
应用 pom.xml 仓库配置 jboss-public-repository-group JBoss Public Repository Group https://repository.jboss.org/nexus/content/groups/public/ default never daily
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值