Nexus搭建maven仓库并使用

一、基本介绍

参考:https://www.hangge.com/blog/cache/detail_2844.html
https://blog.csdn.net/zhuguanbo/article/details/129026067 详细

1、为什么搭建私服

  • 如果没有私服,需要的构件都需要通过maven的中央仓库或者第三方的maven仓库下载到本地,而一个团队的所有人都重复的从maven仓库下载构件加大了仓库的负载和浪费了外网带宽,如果网速慢的话,还会影响项目的进程。
  • 项目在内网进行的,可能根本连接不了maven的中央仓库和第三方的maven仓库
  • 开发的公共构件如果需要提供给其他项目使用,也需要搭建私服

2、nexus介绍

Nexus 是一个专门的 Maven 仓库管理软件,它不仅能搭建 Maven 私服,还具备如下一些优点使其日趋成为最流行的 Maven 仓库管理器

  • 提供了强大的仓库管理功能,构件搜索功能
  • 它基于 REST,友好的 UI 是一个 ext.js 的 REST 客户端
  • 它占用较少的内存
  • 基于简单文件系统而非数据库

二、Nexus 服务的安装

Nexus 既可以使用传统的二进制包进行安装,也可以使用 Docker 容器的方式进行安装运行。下面分别介绍这两种方法。

1、使用二进制发行包安装

(1)首先确保系统 JDK 环境
(2)接着我们访问 Nexus 官网(点击访问)进行下载nexus3下载地址
(3)根据环境选择下载相应的版本,比如我这里下载 Linux 版本的安装包
image
(4)将下载下来的压缩包上传到服务器(比如 /usr/local 目录下),然后进行解压

tar -zxvf nexus-3.21.2-03-unix.tar.gz

(5)解压后会得到两个文件夹:nexus-3.21.2-03(nexus 服务目录)、sonatype-work(私有库目录)
image
(6)进入 nexus-3.21.2-03 文件夹,其中 etc/nexus-default.properties 文件配置端口(默认为 8081)和 work 目录信息,我们可以按需修改

cd nexus-3.21.2-03
cat etc/nexus-default.properties

image
(7)然后执行如下命令开放 8081 端口:

firewall-cmd --permanent --add-port=8081/tcp
firewall-cmd --reload

(8)最后执行如下命令启动服务即可:

cd bin
./nexus start

或者使用./nexus run 会直接在控制台输出日志

2、使用 Docker 镜像进行安装

(1)首先执行如下命令下载 Nexus3 镜像

docker pull sonatype/nexus3

(2)接着执行如下命令,创建宿主机挂载目录

mkdir –vp /usr/local/nexus-data

(3)最后执行如下命令运行 Nexus3 容器即可:

docker run -d --name nexus3 -p 8081:8081 -v /usr/local/nexus-data:/var/nexus-data sonatype/nexus3

(4)同样不要忘记执行如下命令开放 8081 端口:

firewall-cmd --permanent --add-port=8081/tcp
firewall-cmd --reload

三、Nexus 服务的配置

(1)Nexus 服务启动以后,我们使用浏览器访问http://IP:8081/ ,点击右上角登录按钮:
image
(2)首次登录会提示密码保存在 /usr/local/sonatype-work/nexus3/admin.password 文件中,我们查看服务器上这个文件内容,然后作为密码登录:
image
(3)登录后会让我们设置新的密码(这里我设置为 123)
(4)登录后的界面如下:
image

(1)默认仓库说明
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/ 拉取jar
maven-releases:私库发行版jar
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务。在本地maven基础配置setting.xml或项目pom.xml中使用
(2)仓库类型说明:
group:这是一个仓库聚合的概念,用户仓库地址选择group的地址,即可访问group中配置的,用于方便开发人员自己设定的仓库。maven-public就是Group类型的仓库。内部设置了多个仓库,访问顺序取决于配置顺序,3.x默认为Releases、Snapshots6、Central,当然也可以自己设置
hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的jar文件
snapashots:本地项目的快照仓库,测试版
releases:本地项目的正式版本
proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下
virtual(虚拟类型): 虚拟仓库(这个基本用不到,重点关注上面三个仓库的使用)

四、创建新用户和新仓库

1.创建用户

可以省略
登陆admin后,可以点击上面的“设置”图标,在“设置”里可以添加用户、角色,对接LDAP等的设置,如下:
image
image
创建用户好后,重新登陆创建的用户

2、创建仓库

上面对仓库的概率清楚后,我们开始创建新仓库,主要创建3个仓库

proxy仓库 作用是去远程拉取jar包

hosted仓库 作用是存放本地上传的三方jar包

group仓库 作用是将上面来个放到这个组里,进行统一管理

(1)proxy 代理仓库创建

中央仓库的代理默认为https://repo1.maven.org/maven2/ 我们可以更换成阿里云中央仓库。
http://maven.aliyun.com/nexus/content/groups/public/
image
(2)hosted 仓库创建

host仓库这里了两种不同的存储类型
image
image
(3)group仓库

主要就是把上面三个仓库放到该组里,然后让这个组给外面访问。

image
全家福来一个

image

五、项目中使用

1.将jar发送到nexus私服务器

1、创建maven项目
创建一个最简单的maven项目,然后新建一个工具类,用来测试当把它打成jar包放到私服后,其它项目是否能够成功引用。
image
2、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.cgy</groupId>
    <artifactId>ceshi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>
    </dependencies>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://192.168.2.7:8081/repository/java-release</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://192.168.2.7:8081/repository/java-snapshot</url>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <!-- 指定jdk -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

3、setting.xml配置
在这里只要配置登陆nexus的用户名密码,不然没有用户名和密码怎么能将jar包发送到私服呢。

<servers>
 <server>
    	<id>releases</id>
    	 <username>admin</username>
      <password>123</password>
    </server>

      <server>
    	<id>snapshots</id>
    	 <username>admin</username>
      <password>123</password>
    </server>
<servers>
  • 注意:maven会判断版本后面是否带了-SNAPSHOT,如果带了就发布到snapshots仓库,否则发布到release仓库。这里我们可以在pom.xml文件中

(4)执行命令:mvn deploy
image
发现部署到nexus私服成功,我们到私服查看下,因为这里的版本是带SNAPSHOT,所以会发布到snapshots仓库中。
image

说明已经成功将jar包发布到nexus私服中了。那么下一步是如何引用私服中的jar包了。

2.从nexus引用第三方jar包

让maven项目使用nexus作为远程仓库有两种方式,第一种是在项目的pom.xml中进行更改,让单个项目使用nexus仓库;另一种是通过修改maven的配置文件settings.xml进行更改,让所有项目都使用nexus仓库。我们这里采取第二种,只需要setting.xml就可以了。还有就是拉取jar的私服仓库地址只要写一个java-group就可以了,因为在创建这个组的时候,里面已经包含了其它三个仓库。
(1)setting.xml完整配置

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <pluginGroups></pluginGroups>
    <proxies></proxies>
    <servers>
        <!--第一个nexus-xu要和下面的mirror中的id一致,代表拉取是也需要进行身份校验-->
        <server>
            <id>nexus-xu</id>
            <username>xuxiaoxiao</username>
            <password>xuxiaoxiao113</password>
        </server>
        <server>
            <!--这两个前面讲过,是jar上传时候进行的验证,id对应的是pom中id属性的值-->
            <id>releases</id>
            <username>xuxiaoxiao</username>
            <password>xuxiaoxiao113</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>xuxiaoxiao</username>
            <password>xuxiaoxiao113</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>nexus-xu</id>
            <name>internal nexus repository</name>
            <!--镜像采用配置好的组的地址-->
            <url>http://47.96.44.110:8081/repository/java-group/</url>
            <mirrorOf>!internal.repo,*</mirrorOf>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <!--ID用来确定该profile的唯一标识-->
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
        <profile>
            <id>nexus-pr</id>
            <!-- 远程仓库列表 -->
            <repositories>
                <repository>
                    <id>nexus-xu</id>
                    <name>Nexus Central</name>
                    <!-- 虚拟的URL形式,指向镜像的URL-->
                    <url>http://47.96.44.110:8081/repository/java-group/</url>
                    <layout>default</layout>
                    <!-- 表示可以从这个仓库下载releases版本的构件-->
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <!-- 表示可以从这个仓库下载snapshot版本的构件 -->
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <!-- 插件仓库列表 -->
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus-xu</id>
                    <name>Nexus Central</name>
                    <url>http://47.96.44.110:8081/repository/java-group/</url>
                    <layout>default</layout>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <!--需要激活 <profile>中的ID才生效-->
        <activeProfile>nexus-pr</activeProfile>
        <activeProfile>jdk-1.8</activeProfile>
    </activeProfiles>
</settings>

2、验证
(1)新建项目添加pom依赖

    <dependencies>
        <dependency>
            <groupId>com.jincou</groupId>
            <artifactId>xuxiaoxiao</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

(2)看是否拉取到私服的jar包
image

并没有报错,表拉取成功

(3)写测试类
image

引用成功

(4)看后台输出

image

输出成功

从这里将jar包发送到私服和从私服拉取jar就成功了。

posted @ 2022-11-08 14:26  spiderMan1-1  阅读(1317)  评论(0编辑  收藏  举报