zoukankan      html  css  js  c++  java
  • (转)Maven仓库——私服介绍

    背景:对Maven私服一直想做个深入的总结,因为不了解,所以感觉很陌生。

    转载地址:http://blog.csdn.net/catoop/article/details/62312477

    常用功能和介绍

    私服是什么?

    私服,即私有服务器,是公司内部Maven项目需要通过其下载依赖包和插件的一个内部maven仓库。Nexus是常用的私用Maven服务器,一般是公司内部使用。

    将自己的Maven项目指定到私服地址 
    从私服下载中央库的项目索引 
    从私服下载中央库的项目索引 
    将第三方项目jar上传到私服供其他项目使用

    Nexus 安装后,默认端口为8081,访问 http://192.168.x.x:8081/nexus/index.html 打开页面,默认账号密码为:admin/admin123

    下面是登录后的截图: 
    这里写图片描述 
    其中左侧菜单中的Repositories比较常用。

    一般用到的仓库种类是hosted和proxy。 
    Hosted代表宿主仓库,用来发布一些第三方不允许的组件,比如oracle驱动、比如商业软件jar包、公司开发的jar包库。

    Proxy代表代理远程的仓库,最典型的就是Maven官方中央仓库、JBoss仓库等等。如果构建的Maven项目的用户电脑本地仓库没有依赖包,那么就会去Proxy代理站点(即Nexus私服)去下载,那么如果代理站点也没有此依赖包,就会去远程中央仓库下载依赖。代理站点(私服)下载成功后再下载至用户本机仓库。

    其实Maven这个自带的默认仓库一般情况下已经够大多数项目使用了。特殊情况时在配置新的仓库,比如增加JBoss仓库等(配置指定url即可)。

    如下,描述了用户电脑、私服、远程中央仓库的关系图: 
    这里写图片描述

    仓库的类型

    hosted 类型的仓库,内部项目的发布仓库 
    releases 内部的模块中release模块的发布仓库 
    snapshots 发布内部的SNAPSHOT模块的仓库 
    3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去 
    proxy 类型的仓库,从远程中央仓库中寻找数据的仓库 
    group 类型的仓库,组仓库用来为了方便我们开发人员而进行设置的仓库

    Maven项目索引

    下载Maven项目索引,项目索引是为了使用者能够在私服站点查找依赖使用的功能。 
    这里写图片描述

    保存后后台会运行一个任务,点击菜单栏的Scheduled Tasks(再左侧菜单Administration组中)选项即可看到有个任务在RUNNING。 下载完成后,Maven索引就可以使用了,在搜索栏输入要搜索的项,就可以查到相关的信息。例如 spring-core,下图是一个示范(就可以检索出它的相关信息,包括怎么配置依赖信息):

    这里写图片描述

    全局配置和用户配置的区别

    变量名:M2_HOME
    变量值:D:apache-maven-2.2.1 (maven安装目录)

    ~在《Maven实战》中指代用户目录。比如我的用户目录C:Usersprd-lxw

    在用户目录下可以发现.m2文件夹。默认情况下,该文件夹下放置了Maven本地仓库.m2/repository  所有的Maven构件都被存储到该仓库中,以方便重用。

    默认情况下~/.m2目录下除了repository仓库之外就没有其它目录和文件了,不过大多数Maven用户需要复制M2_HOME/conf/settings.xml文件到 ~/.m2/settings.xml

    这是一条最佳实践。

    全局配置,M2_HOME/conf/settings.xml,整台机器上的所有用户都受该配置的影响;

    用户配置, ~/.m2/settings.xml,只有当前用户才受该配置影响。

    需要统一系统中所有用户的配置的时候使用全局配置,否则请使用用户配置。

    推荐使用用户配置的原因是便于Maven升级,不必要每次升级都修改settings.xml文件。

    配置使用私服

    只对单独一个项目有效

    我们要想使用这个私服仓库,先在项目pom中配置相关私服信息指定仓库,如下片段需要配置到maven项目的 pom.xml 中。

    <project>
        ...
        <!-- 配置仓库 -->
        <repositories>  
            <repository>  
                <id>nexus</id>  
                <name>nexus</name>
                <url>http://192.168.x.x:8081/nexus/content/groups/public/</url>  
                <releases>  
                    <enabled>true</enabled>  
                </releases>  
                <snapshots>  
                    <enabled>true</enabled>  
                </snapshots>  
            </repository>  
        </repositories> 
    
        <!-- 配置插件仓库 -->
        <pluginRepositories>  
            <pluginRepository>  
                <id>nexus</id>  
                <name>nexus</name>  
                <url>http://192.168.x.x:8081/nexus/content/groups/public/</url>  
                <releases>  
                    <enabled>true</enabled>  
                </releases>  
                <snapshots>  
                    <enabled>true</enabled>  
                </snapshots>  
            </pluginRepository>  
        </pluginRepositories>
        ... 
    </project>

    这样配置完成后,项目才会在私服下载组件。

    因为我们这个配置是配置在具体项目的 pom.xml 中的,所以只会对一个项目有效。

    如果我们想对自己电脑上的所有 maven 项目都启用这个私服配置,那么就需要将私服信息配置到 maven 的全局配置文件 settings.xml 中,其位置位于 apache-maven/conf/settings.xml

    ps:此处可以使用用户级别的配置。

    对本机所有maven有效

    settins.xml并不支持直接配置repositories和pluginRepositories。但是Maven提供了Profile机制,能让用户将仓库配置防止到setting.xml中的Profile中。修改 settings.xml 中的profiles内容为:

    <settings>
        ...
        <profiles>
            <profile>
                <id>nexus</id>
                 <!-- 配置仓库 -->
                <repositories>  
                    <repository>  
                        <id>nexus</id>  
                        <name>nexus</name>
                        <url>http://192.168.x.x:8081/nexus/content/groups/public/</url>  
                        <releases>  
                            <enabled>true</enabled>  
                        </releases>  
                        <snapshots>  
                            <enabled>true</enabled>  
                        </snapshots>  
                    </repository>  
                </repositories> 
    
                <!-- 配置插件仓库 -->
                <pluginRepositories>  
                    <pluginRepository>  
                        <id>nexus</id>  
                        <name>nexus</name>  
                        <url>http://192.168.x.x:8081/nexus/content/groups/public/</url>  
                        <releases>  
                            <enabled>true</enabled>  
                        </releases>  
                        <snapshots>  
                            <enabled>true</enabled>  
                        </snapshots>  
                    </pluginRepository> 
                </pluginRepositories>
            </profile>
        </profiles>
    
        <!-- 激活节点nexus -->
        <activeProfiles>  
            <activeProfile>nexus</activeProfile>     
        </activeProfiles> 
    
        ...
    </settings>

    之后所有本机的Maven项目就都会在这个私服下载组件(这样比较方便)

    配置镜像,让Maven只使用私服

    如果仅仅使用上述配置,Maven除了从Nexus下载构件之外,还会不时地访问中央仓库central,我们希望的是所有Maven下载请求都仅仅通过Nexus,以全面发挥四氟的作用。这个时候可以借助Maven的镜像配置。

    可以创建一个匹配任何仓库的镜像,镜像的地址为私服,这样Maven对任何仓库的构件下载请求都会转发到私服中。

    <settings>
    ...
      <mirrors>
        <mirror>
          <id>nexus</id>
          <mirrorOf>*</mirrorOf>
          <url>http://192.168.x.x:8081/nexus/content/groups/public/</url>  
        </mirror>
      </mirrors>
      
       <profiles>
            <profile>
                <id>nexus</id>
                 <!-- 配置仓库 -->
                <repositories>  
                    <repository>  
                        <id>central</id>                     
                        <url>http://central</url>  
                        <releases>  
                            <enabled>true</enabled>  
                        </releases>  
                        <snapshots>  
                            <enabled>true</enabled>  
                        </snapshots>  
                    </repository>  
                </repositories> 
    
                <!-- 配置插件仓库 -->
                <pluginRepositories>  
                    <pluginRepository>  
                        <id>central</id>                     
                        <url>http://central</url>   
                        <releases>  
                            <enabled>true</enabled>  
                        </releases>  
                        <snapshots>  
                            <enabled>true</enabled>  
                        </snapshots>  
                    </pluginRepository> 
                </pluginRepositories>
            </profile>
        </profiles>
    
        <!-- 激活节点nexus -->
        <activeProfiles>  
            <activeProfile>nexus</activeProfile>     
        </activeProfiles> 
    ...
    </settings>

     这里需要解释的是仓库及插件仓库配置,它们的id都是central,也就是说,覆盖了超级POM中央仓库的配置,它们的url已经无关紧要,因为所有的请求都会通过镜像访问私服地址。

    配置仓库及插件仓库的主要目的是开启对快照版本下载的支持,当Maven需要下载发布版本或快照版构件的时候,它首先需要检查central,看该类型的构件是否支持,得到正面的回答之后,再根据镜像匹配规则转而访问私服仓库地址。

    项目发布到私服

    再 maven 工程的 pom.xml 中添加如下配置:

    <distributionManagement>
            <repository>
                <id>nexus-releases</id>
                <name>Nexus Release Repository</name>
                <url>http://192.168.x.x:8081/nexus/content/repositories/releases/</url>
            </repository>
            <snapshotRepository>
                <id>nexus-snapshots</id>
                <name>Nexus Snapshot Repository</name>
                <url>http://192.168.x.x:8081/nexus/content/repositories/snapshots/</url>
            </snapshotRepository>
        </distributionManagement>

    上述配置中,id为该远程仓库的唯一标识,那么是为了方便人阅读,关键的url表示该仓库的地址。

    仅仅这样配置还不够,发布项目到私服肯定会失败,因为我们还没有配置权限,没有配置权限发布项目应该会出现401错误码,所以我们需要在 maven 的 settings.xml 中配置私服权限账号信息,如下:

    <!-- servers节点的属性是在向仓库发布时使用 -->
        <servers>
            <server>
                <!-- 这个ID要和项目pom.xml中distributionManagement下的ID一致 -->
                <id>nexus-releases</id>
                <username>admin</username>
                <password>admin123</password>
            </server>
            <server>
                <!-- 这个ID要和项目pom.xml中distributionManagement下的ID一致 -->
                <id>nexus-snapshots</id>
                <username>admin</username>
                <password>admin123</password>
            </server>
        </servers>

    这里写图片描述

    注意图中的Respository中的id一定要和server下的id一致,切记!!否则出现权限问题。

    然后运行命令发布 

    mvn clean deploy

    在控制台提示发布成功后,进入到私服上的仓库中,看一下确实存在刚刚发布的项目。

    这里写图片描述

    宿主库 — 3rd party

    假如我们下载了Oracle的驱动程序jar包想给其他项目组使用,就需要上传该jar包。选中宿主库 3rd party,之后选择Artifact Upload上传至宿主空间。

    这里写图片描述

    上传后到 Browse Index 中查看结果。

  • 相关阅读:
    41 最大子数组
    4 丑数 Ⅱ-找出第n个丑数
    写在编程初始
    lightoj 1068
    2018-11-8-内置函数(2)
    2018-11-7-内置函数(1)
    2018.11.06 生成器函数进阶&列表推导式&生成器表达式
    python2&python3的区别
    第一次打开Pycharm如何操作?
    关于做题的一些反思
  • 原文地址:https://www.cnblogs.com/lixuwu/p/8111773.html
Copyright © 2011-2022 走看看