zoukankan      html  css  js  c++  java
  • maven 私服

    公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库,
    每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,
    其它项目组从私服服务器下载所依赖的构件(jar)。
    私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载,如下图:
      

    搭建私服环境:

      Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。

      安装 nexus:

        解压 nexus-2.12.0-01-bundle.zip 到指定目录

        使用管理员方式打开 cmd,进入到 bin 目录,执行  nexus.bat install 安装

        安装成功在服务中查看有 nexus 服务

       卸载 nexus:

        管理员 cmd 进入 nexus 的 bin 目录,执行:nexus.bat uninstall

      启动 nexus:

        cmd 进入 bin目录,执行 nexus.bat start 启动,或者到 Windows 服务中,直接启动 nexus 服务

        如果启动失败,在Nexus安装目录下,找到【~injswconf】文件夹,打开【wrapper.conf】文件,设置安装的JDK的bin目录

          # Set the JVM executable
          # (modify this to absolute path if you need a Java that is not on the OS path)
          wrapper.java.command=D:Javajdk1.8.0_131injava

      查看 nexus 的配置文件 conf/nexus.properties:

        # Jetty section
        application-port=8081  # nexus 的访问端口配置
        application-host=0.0.0.0  # nexus 主机监听配置(不用修改)
        nexus-webapp=${bundleBasedir}/nexus  # nexus 工程目录
        nexus-webapp-context-path=/nexus  # nexus 的 web 访问路径
        # Nexus section
        nexus-work=${bundleBasedir}/../sonatype-work/nexus  # nexus 仓库目录
        runtime=${bundleBasedir}/nexus/WEB-INF  # nexus 运行程序目录

      访问:http://localhost:8081/nexus/

        使用 Nexus 内置账户admin/admin123 登陆

    仓库类型:

      查看 nexus 的仓库:

      

       nexus 的仓库有 4 种类型:

        

        1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
        2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
        3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。
        4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件

      central :代理仓库,代理中央仓库,代理地址 https://repo1.maven.org/maven2/

        

      apache-snapshots :代理仓库存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/

      central-m1 :virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件
      releases : 本地仓库,存储 releases 构件。
      snapshots : 本地仓库,存储 snapshots 构件。
      thirdparty :第三方仓库
      public:仓库组

        

      nexus 仓库默认在 sonatype-work 目录中:

         

    将项目发布到私服配置:

      第一步: 需要在客户端即需要发布项目的电脑上配置 maven 环境,并修改 settings.xml 文件,配置连接私服的用户名和密码 。

        在 settings.xml 文件 servers 标签内部配置用户名和密码

    <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>

        releases 连接发布版本项目仓库  snapshots 连接测试版本项目仓库

        此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。
      第二步: 配置项目 pom.xml 文件

        配置私服仓库的地址,公司自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,
        如果版本为 release 则上传到私服的 release 仓库,如果版本为 snapshot 则上传到私服的 snapshot 仓库

    <distributionManagement>
      <repository>
        <id>releases</id>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
      </repository>
      <snapshotRepository>
        <id>snapshots</id>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
      </snapshotRepository>
    </distributionManagement>

        注意:pom.xml 这里<id> 和 settings.xml 配置 <id> 对应!

      将项目工程打成 jar 包发布到私服:执行 deploy 命令

      根据本项目 pom.xml 中 version 定义决定发布到哪个仓库,如果 version 定义为 snapshot,
      执行 deploy 后查看 nexus 的 snapshot 仓库,如果 version 定义为 release 则项目将发布到 nexus 的 release仓库

    从私服下载 jar 包:

      没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,
      有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,
      如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,
      这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度,
      项目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多。

      管理仓库组

        nexus中包括很多仓库,hosted中存放的是企业自己发布的jar包及第三方公司的jar包,
        proxy 中存放的是中央仓库的 jar,为了方便从私服下载 jar 包可以将多个仓库组成一个仓库组,
        每个工程需要连接私服的仓库组下载 jar 包。

      打开 nexus 配置仓库组

        

      第一步:在 setting.xml 中配置私服的仓库

         在 profiles 标签内部配置

    <profile>   
      <!-- profile的id -->
      <id>dev</id>   
        <repositories>   
          <repository>  
              <!-- 仓库id,repositories可以配置多个仓库,保证id不重复 -->
                      <id>nexus</id>   
                      <!-- 仓库地址,即nexus仓库组的地址 -->
                      <url>http://localhost:8081/nexus/content/groups/public/</url>   
                      <!-- 是否下载releases构件 -->
                      <releases>   
                        <enabled>true</enabled>   
                      </releases>   
                      <!-- 是否下载snapshots构件 -->
                      <snapshots>   
                        <enabled>true</enabled>   
                      </snapshots>   
                    </repository>   
                  </repositories>  
                   <pluginRepositories>  
                      <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
                      <pluginRepository>  
                          <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
                          <id>public</id>  
                          <name>Public Repositories</name>  
                          <url>http://localhost:8081/nexus/content/groups/public/</url>  
                      </pluginRepository>  
                  </pluginRepositories>  
          </profile>

        使用 profile 定义仓库需要激活才可生效,在 settings 标签内部配置

    <activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>

    把第三方 jar 包放入本地仓库或私服:演示所用jar包 fastjson-1.1.37.jar

      1. 放入本地仓库

        方式1:----进入jar包所在目录运行
            mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar
        方式2:----打开cmd直接运行
            mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=jar包所在的位置 -Dpackaging=jar

      2.放入私服

        需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息

    <server>
        <id>thirdparty</id>
        <username>admin</username>
        <password>admin123</password>
    </server>

        方式1:----进入jar包所在目录运行

            mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

        方式2:----打开cmd直接运行

            mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=jar包所在的位置 -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

      参数说明:

        DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位的,也可以自己起名字。
        Dfile 表示需要上传的 jar 包的绝对路径。
        Durl 私服上仓库的位置,打开 nexus——>repositories菜单,可以看到该路径。
        DrepositoryId 服务器的表示 id,在 nexus 的configuration 可以看到。
        Dversion 表示版本信息

        

  • 相关阅读:
    leetcode 202
    C++判断字符是否是元音字母
    [转][图]性能瓶颈分析图
    MySQL的一次优化记录 (IN子查询和索引优化)
    compiz隐藏最大化窗口标题栏
    hyper-v server 2019安装,工作组客户端远程管理
    powershell删除默认共享
    Powershell 更改计算机上的远程桌面的RDP端口
    Windows server 2019 Core安装Exchange 2019
    使用 PowerShell 安装 卸载Microsoft Defender 防病毒
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12324265.html
Copyright © 2011-2022 走看看