(四)Maven中的仓库

一、分类

  • maven的仓库只有两大类:

    •   1.本地仓库

    •   2.远程仓库,在远程仓库中又分成了3种:

      •   2.1 中央仓库

      •   2.2 私服

      •   2.3 其它公共库

二、本地仓库

  • 本地仓库,顾名思义,就是Maven在本地存储构件的地方。
  • 注:maven的本地仓库,在安装maven后并不会创建,它是在第一次执行maven命令的时候才被创建,maven本地仓库的默认位置:无论是Windows还是Linux,在用户的目录下都有一个.m2/repository/的仓库目录,这就是Maven仓库的默认位置
  • 如何更改maven默认的本地仓库的位置,修改maven的settings.xml文件中的localRepository标签值。
    <settings>  
        <localRepository>D:\maven_new_repository</localRepository>  
    </settings>  

 

 

三、中央仓库

  • 中央仓库是默认的远程仓库,maven在安装的时候,自带的就是中央仓库的配置

  •  

  • 案例:修改默认中央仓库,即下载依赖包不再从默认的中央仓库中下载(虽然更新快但是下载速度慢),只需修改工程的pom.xml文件即可。

<!-- 修改中央仓库 -->
<repositories>
    <repository>
    <!-- 指定仓库唯一id -->
        <id>resp</id> 
        <!-- 指定仓库名 -->
        <name>resp</name>
        <!-- 指定仓库地址 -->
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <!-- 设置仓库是否为默认仓库 -->
        <layout>default</layout>
        <!-- 设置是否可以从url对应的仓库中下载快照snapshots版本的依赖 -->
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <!-- 设置是否可以从url对应的仓库中下载稳定releases版本的依赖 -->
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

<!-- 修改插件仓库 -->
<pluginRepositories>
    <pluginRepository>
        <id>pluginTest</id>
        <name>pluginTest</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <layout>default</layout>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>
  • 此时下载依赖会在http://maven.aliyun.com/nexus/content/groups/public/和默认中央仓库两个仓库同时下载,如果想要禁止默认中央仓库下载,可以将自己设置的仓库的id设置为中央仓库的id即<id>central</id>
  •  缺点:只针对当前工程,新建工程还是从默认中央仓库下载。

 

  • 案例二:通过修改镜像,修改所有工程的默认中央仓库

    •   修改maven的setting.xml文件
    <mirror>
            <id>mirrorId</id>
            <name>aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
  •   其中 <mirrorOf>*</mirrorOf> 指对所有工程的所有仓库进行映像,即所有工程所有仓库都会无效,下载依赖时从镜像的url仓库下载。如果配置<mirrorOf>central</mirrorOf>则任何从默认中央仓库下载的依赖都会转到镜像仓库下载。

 

 

四、私服

  • 定义:私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库,供局域网内的Maven用户使用。当Maven需要下载构件的时候,它从私服请求,如果私服上不存在该构件,则从外部的远程仓库下载,缓存在私服上之后,再为Maven的下载请求提供服务。我们还可以把一些无法从外部仓库下载到的构件上传到私服上。

  • 主要用于:  团队开发的时候,某个成员写好的工程打包成jar包存放于私服中,团队其他成员可以从私服中下载即可使用。如果没有私服,则必须拷贝jar包,然后在自己的工程中引入gav坐标才能使用,很麻烦。

  •  Nexus私服的安装与搭建使用,具体步骤请参考:windows下Nexus搭建Maven私服
  •  案例: 设置maven工程从私服下载依赖

<repositories>
        <repository>
            <id>nexus resp</id>
            <name>nexus resp</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>plugin resp</id>
            <name>plugin resp</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>
    •     其中,<url>http://localhost:8081/nexus/content/groups/public/</url> url的地址为nexus中group的地址,即

    •     值得注意的是: 上面项目的前提是setting.xml文件不能设置镜像为如下:
    • <mirror>
            <id>mirrorId</id>
            <mirrorOf>*</mirrorOf>
            <name>aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
          </mirror>

      否则maven工程将直接访问镜像所对应的仓库而不是我们的私服仓库。可以把镜像配置改为<mirrorOf>central</mirrorOf>,这样只会把中央仓库的请求转到镜像对应的仓库,而请求私服的请求是不会转到镜像对应的仓库的。

    •    一般来说,我们都把镜像对应的仓库设置为私服仓库,而私服仓库代理的仓库设置为阿里云仓库,这样当maven工程需要依赖的时候首先从本地仓库中找,如果没有则从私服中下载,如果私服不存在该依赖则把该依赖从阿里云中下载到私服。注意,maven工程install之后只会打包到本地仓库,而私服仓库是不会有的,需要手动添加。

 

posted @ 2017-08-08 13:48  shyroke、  阅读(287)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~