从Nexus私服的代理仓库中下载构件(2种方式)和部署构件到Nexus私服的宿主仓库中(2种方式)

发布时间 2023-08-09 15:24:05作者: 且行且思

一、从Nexus私服的代理仓库中下载构件(2种方式)

Nexus私服的主要目的之一:代理远程仓库(即当Maven项目需要下载构件到本地仓库时,不再请求外部的远程仓库,而直接从Nexus私服中下载)。 ===》1. 在Maven项目的pom.xml文件中配置Nexus 只对当前项目有效 项目需要下载构件到本地仓库时,会依次从以下配置的Nexus代理仓库中寻找。


<!-- 声明远程仓库,一个repository元素对应一个远程仓库 -->
<repositories>
    <!-- 声明一个Nexus私服上的仓库  -->
    <repository>
        <!-- 
        仓库id 
          仓库的唯一标识。Nexus内置的Maven中央仓库的id为central,如果其他的仓库也使用该id,就会覆盖中央仓库的配置。
        -->
        <id>nexus</id>
        <!-- 仓库名称 -->
        <name>nexus</name>
        <!-- 仓库地址 -->
        <url>http://localhost:8081/nexus/content/repositories/hello_central_proxy/</url>
        <!-- 是否开启该仓库的release版本下载支持 -->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!-- 是否开启该仓库的snapshot版本下载支持 -->
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<!-- 声明远程插件仓库,一个pluginRepository元素对应一个远程插件仓库 -->
<pluginRepositories>
    <!-- 声明一个Nexus私服上的插件仓库 -->
    <pluginRepository>
        <!-- 插件仓库 id -->
        <id>nexus</id>
        <!-- 插件仓库 名称 -->
        <name>nexus</name>
        <!-- 配置的插件仓库的地址 -->
        <url>http://localhost:8081/nexus/content/repositories/hello_central_proxy/</url>
        <!-- 是否开启该插件仓库的release版本下载支持 -->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!-- 是否开启该插件仓库的snapshot版本下载支持 -->
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

 

===》2. 在Maven安装目录/conf目录和本地仓库目录下的settings.xml文件中配置Nexus
  对本机中的所有项目有效
  setting.xml不支持直接配置repositories和pluginRepositories,好在Maven提供了Profile机制,能够让我们将仓库配置放在Profile中。
<profiles>
    <profile>
        <id>nexus</id>
        <!--声明一个或多个远程仓库  -->
        <repositories>
            <!-- 声明一个 Nexus 私服上的仓库  -->
            <repository>
                <!--仓库id  -->
                <id>nexus</id>
                <!-- 仓库的名称 -->
                <name>nexus</name>
                <!--仓库的地址  -->
                <url>http://localhost:8081/nexus/content/repositories/hello_central_proxy/</url>
                <!-- 是否开启该仓库的 release 版本下载支持 -->
                <releases>
                    <enabled>true</enabled>
                </releases>
                <!-- 是否开启该仓库的 snapshot 版本下载支持 -->
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <!-- 声明一个或多个远程插件仓库 -->
        <pluginRepositories>
            <!--声明一个 Nexus 私服上的插件仓库  -->
            <pluginRepository>
                <!--插件仓库 id -->
                <id>nexus</id>
                <!--插件仓库 名称 -->
                <name>nexus</name>
                <!-- 配置的插件仓库的地址 -->
                <url>http://localhost:8081/nexus/content/repositories/hello_central_proxy/</url>
                <!-- 是否开启该插件仓库的 release 版本下载支持 -->
                <releases>
                    <enabled>true</enabled>
                </releases>
                <!-- 是否开启该插件仓库的 snapshot 版本下载支持 -->
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

 

镜像(mirror)
  Nexus私服通常会与镜像结合使用,使Nexus成为所有远程仓库的私服(这样不仅可以从Nexus中获取所有所需构件,还能将配置集中到Nexus私服中,简化Maven本身的配置)。
  创建一个匹配任何仓库的镜像(镜像的地址为Nexus中仓库的地址),这样Maven对于任何构件的下载请求都会被拦截跳转到Nexus私服中,并从指定仓库地址中获取构件:

<!-- 所有构件请求都从nexus私服的指定仓库地址中获取,上述profile中配置的url都会失效 -->
<mirrors>
    <mirror>
        <id>nexus</id>
        <name>nexus name</name>
        <mirrorOf>*</mirrorOf>
        <url>http://localhost:8081/nexus/content/groups/hello_repository_group/</url>
    </mirror>
</mirrors>

 

二、部署构件到Nexus私服的宿主仓库中(2种方式)
===》1. 通过配置Maven项目将构件自动部署到Nexus宿主仓库
  将快照版本构件部署到Nexus中策略为Snapshot的宿主仓库中;将发布版本构件部署到Nexus中策略为Release的宿主仓库中;
步骤:
  1. 配置项目的pom.xml文件。
<project>
      ...
      <!-- 负责将指定的构件部署到Nexus指定的宿主仓库中 -->
      <distributionManagement>
        <!-- 用于定义部署Release版本构件到哪个仓库 -->
        <repository>
            <!-- 宿主仓库的唯一标识 -->
            <id>hello_Release_hosted</id>
            <!-- 宿主仓库的地址 -->
            <url>http://localhost:8081/nexus/content/repositories/hello_Release_hosted/</url>
        </repository>
        <!-- 用于定义部署Snapshot版本构件到哪个仓库 -->
        <snapshotRepository>
            <id>hello_Snapshot_hosted</id>
            <url>http://localhost:8082/nexus/content/repositories/hello_Snapshot_hosted/</url>
        </snapshotRepository>
      </distributionManagement>
    </project>
  2. 在maven安装目录/conf目录和本地仓库目录下的setting.xml中配置认证信息。
  如果不进行配置,当宿主仓库开启了部署功能时,任何人都可以连接并部署构件至这个仓库。
<settings>
      ...
      <servers>
        <server>
            <id>hello_Release_hosted</id>
            <username>admin</username>
            <password>密码</password>
        </server>
        <server>
            <id>hello_Snapshot_hosted</id>
            <username>admin</username>
            <password>密码</password>
        </server>
      </servers>
    </settings>
3. 使用mvn命令部署构件。
    mvn clean deploy
在Nexus界面的仓库列表中选中hello_Snapshot_hosted宿主仓库,在Browse Index选项卡中可以看到构件(hello-0.01-SNAPSHOT.jar)已经部署到该仓库中。

===》2. 在Nexus界面中手动上传构件
  有些 Jar 文件(如:Oracle 的 JDBC 驱动)由于许可证等原因,无法存放在公开仓库中。此外,还有一些小型的开源项目,它们没有将自己的构件分发到公共仓库中,也没有维护自己的仓库,因此这些构件是无法从公共仓库中获得的。若 Maven 项目中需要这类构件,我们就需要将构件下载到本地,然后手动上传到 Nexus 私服。
步骤
  1. 选中要上传到哪个仓库,选择Artifact Upload选项卡,其中GAV Definition(提供了2种定义构件坐标的方式)用于定义上传构件的坐标信息。
    方式1. 若该构件通过Maven构建产生的,则可以选择From POM,指定该项目的pom.xml(Nexus会自动从pom.xml文件中获取构件的坐标), 最后点击Upload Artifact(s) 按钮。
    方式2. 若该构件来自第三方(如:Oracle 的 JDBC 驱动),则只能选择GAV Parameters(手动定义构件的坐标),点击Add Artifact按钮上传该构件,最后点击Upload Artifact(s) 按钮。
  2. OK