手把手教你封装通用工具并推送至Maven仓库

394 阅读8分钟

清夜无尘,月色如银。酒斟时,须满十分。叹隙中驹,石中火,梦中身。虽抱文章,开口谁亲。

曾经,我也陷入过困惑,觉得自己无法应对那些看似无法跨越的障碍。每一段艰难的路途,每一次沉重的抉择,似乎都在考验我是否足够坚韧。然而,随着时间的推移,我逐渐意识到,真正的勇气,并不是在没有恐惧的情况下前进,而是在恐惧面前依然选择坚持,依然不放弃。生活从来不会给你一条平坦的路,而真正值得我们珍惜的,是那些使我们成长的崎岖。

我开始明白,人生并非需要每个阶段都闪耀夺目。我们有时需要的,并不是一场轰轰烈烈的胜利,而是能在平凡的日子里找到真正的自我。无论命运如何起伏,重要的是我们是否仍能坚持自己那颗最初的心,是否依旧愿意相信,无论多么微小的坚持,都会在时光的流淌中开花结果。

一、Maven仓库搭建

正如标题所提到,我们需要将我们自己封装的工具打成 pom 再推送至 maven 仓库中去,那我们就需要去搭建一个 maven 仓库以备后续使用,下图为 Maven 仓库架构图:

镜像下载

为了加快镜像下载和安装的速度,一般我们需要修改镜像源:

echo '{"dns":["8.8.8.8","8.8.4.4"],"registry-mirrors":["https://hub.rat.dev","https://docker.1panel.live","https://dockerproxy.cn","https://hub.dftianyi.top","https://docker.mirrors.ustc.edu.cn","https://hub-mirror.c.163.com"]}' > /etc/docker/daemon.json

拉取仓库镜像:

docker pull sonatype/nexus3

启动服务

Nexus 官方配置里默认是 2703M 的内存占用,而如果你机器只有2G的话那么是跑不起来的,但是修改下 JVM 启动参数还是可以做到运行起来的,就是使用体验并不是很好。因为本人所购买服务器就是 2G(1年36元)的,后面我会教大家如何使用阿里出品的云效仓库。

  • Window Docker Desktop 版
docker run -itd ^
--privileged=true ^
--name nexus ^
-p 8081:8081 ^
-v "/c/nexus-data:/nexus-data" ^
--restart=unless-stopped ^
sonatype/nexus3
  • Linux 版
docker run -itd \
--privileged=true \
--name nexus \
-p 8081:8081 \
-v "/usr/local/nexus-data:/nexus-data" \
--restart=unless-stopped \
sonatype/nexus3

查看密码

# 进入容器
[root@hcss-ecs-b49c ~]# docker exec -it nexus /bin/bash

# 查看密码
[root@hcss-ecs-b49c ~]# cat /nexus-data/admin.password
378b055f-43af-4265-99ae-53a2de331b7c

内存修改

如果你的服务器资源不是 2G 的,内存足够,那么这个步骤可以完全跳过。

  • 查看可用内存

修改内存参数之前,我们建议先参照我们宿主机可用内存之后,酌情修改,同时需要注意,如果参数改的太小,服务也是跑不起来的。

[root@hcss-ecs-b49c]# free -h
      total       used        free      shared  buff/cache  available
Mem:  1.8G        216M        831M      8.6M    789M        1.4G
Swap: 0B          0B          0B
  • 扩容内存(不推荐)

当内存不足我们可以使用 Swap 中的内存,虽然不是很建议,因为会有点慢的。但这也是一个办法。

Swap 交换分区是一种虚拟内存的重要实现方式,它本质上是磁盘上预留的专用区域,用于在物理内存不足时存储暂时不用的数据。当系统面临内存压力时,会将部分不活跃的数据从物理内存转移到 swap 分区,从而释放更多可用内存空间。

这种机制不仅提高了内存利用率,还允许系统处理比实际物理内存更大的数据集,有效提升了整体性能和灵活性。

# of是空间名称,count是空间大小,bs是单位,这里是kb
# 你可以将bs=1024改为bs=1G,后面的count就可以该为count=2,就是创建2G大小的swap文件
# dd if=/dev/zero of=swapfile bs=1024 count=500000
dd if=/dev/zero of=swapfile bs=1G count=2

# 将swapfile设置为swap空间
mkswap swapfile

启用交换空间:  
swapon swapfile

# 删除交换空间命令 
# swapoff swapfile
  • 停止服务

启动 nexus 容器后停止服务修改配置参数。

service docker stop

或者使用以下命令:

systemctl stop docker
  • 查看容器ID

复制 Docker 容器 ID 后进入 /var/lib/docker/containers/ 目录:

[root@hcss-ecs-b49c ~]# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED       STATUS                                  PORTS     NAMES
565f994e456e   sonatype/nexus3   "/opt/sonatype/nexus…"   3 hours ago   Restarting (1) Less than a second ago             nexus

将容器 ID 记下来:a58d62e47f8f,进入宿主机目录:

cd /var/lib/docker/containers/

筛选我们对应容器的完整 ID :

[root@hcss-ecs-b49c containers]# ll |grep a58d62e47f8f
drwx--x--- 4 root root 4096 Jan 20 14:21 a58d62e47f8f72679e3efa62d70364dcb31ac3103ac13ef3c43b3c0136cfc551

得到筛选出的完整 ID 后进入容器 ID 目录:

cd a58d62e47f8f72679e3efa62d70364dcb31ac3103ac13ef3c43b3c0136cfc551/

编辑配置文件:

vi config.json 

或者是下面这个文件,具体视版本情况而定:

vi config.v2.json

我这边看到的内容如下:

其中,-Xms 是初始堆内存大小,-Xmx 是最大堆内存大小,后面那个是触发 FullGC的大小,我建议修改为2048,如果你电脑真的只有 2G 内存,那建议你修改成 19** 等数值,选择你合适的数字,我这里改成 1700m 了,因为内存不够它真的无法启动。

  • 重启服务
# 重启 docker
systemctl restart docker

# 重启 nexus 
docker restart nexus

⁉️如果遇到以下提示: Warning: Stopping docker.service, but it can still be activated by: docker.socket ,主要的原因是 Docker 默认开启自动唤醒机制,即 Docker 默认在关闭状态下被访问会自动唤醒 Docker,所以导致我们无法关闭 Docker 进程。

查看Docker是否开启自动唤醒机制:

systemctl status docker

如果出现如下图的状态即为开启自动唤醒机制:

停用自动唤醒机制:

systemctl stop docker.socket

然后我们再执行:

systemctl stop docker

以停止 Docker 服务。

服务日志

有时候我们需要通过日志来判断容器服务的运行情况,更好的去排查问题:

docker logs -ft nexus

验证服务

验证服务之前,请关闭对应服务端口 8081 防火墙策略,具体操作请按照对应云提供商操作文档:

关闭防火墙后,我们访问服务地址 http://你的服务器公网IP:8081/ 即可:

首次进入后,登录管理员账号 admin ,会要求你修改上面生成的默认密码,按照指示往下一步步走就好了。

二、Maven仓库配置

为了能够将我们自己封装的工具推送到仓库中,那我们本地的 Maven 就需要一些配置,以便能和我们的仓库能够进行“通信”。

配置修改

一般来说,我们的 Maven 配置文件路径都在 Maven 安装目录下的 conf/settings.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">
   <localRepository>F:\apache-maven-3.6.3\repository_</localRepository>
   <offline>false</offline>
   <interactiveMode>false</interactiveMode>
   <servers>
       <!--私库认证账号及密码-->
       <server>
           <id>nexus</id>
           <username>admin</username>
           <password>*******</password>
       </server>
   </servers>
   <mirrors>
       <!--阿里公共仓库-->
       <mirror>
           <id>aliyun</id>
           <name>Central Repository</name>
           <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
           <mirrorOf>*</mirrorOf>
       </mirror>
   </mirrors>
   <profiles>
       <profile>
           <!--私库ID,所有的都要保持一致-->
           <id>nexus</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>
           <repositories>
               <repository>
                   <id>AliyunMaven</id>
                   <name>Aliyun Maven</name>
                   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                   <snapshots>
                       <enabled>false</enabled>
                   </snapshots>
               </repository>
               <!--nexus私库-->
               <repository>
                   <id>nexus</id>
                   <url>http://116.205.112.132:8081/repository/maven-public/</url>
                   <releases>
                       <enabled>false</enabled>
                       <updatePolicy>always</updatePolicy>
                   </releases>
                   <snapshots>
                       <enabled>false</enabled>
                       <updatePolicy>always</updatePolicy>
                   </snapshots>
               </repository>
               <repository>
                   <id>maven.net.cn</id>
                   <name>Mirror from Maven in china</name>
                   <url>http://maven.net.cn/content/groups/public/</url>
                   <snapshots>
                       <enabled>false</enabled>
                   </snapshots>
               </repository>
               <repository>
                   <id>CN</id>
                   <name>OSChinaCentral</name>
                   <url>http://maven.oschina.net/content/groups/public/</url>
                   <snapshots>
                       <enabled>false</enabled>
                   </snapshots>
               </repository>
           </repositories>
       </profile>
   </profiles>
</settings>

应用配置

打开我们要推送到仓库中的开源工具项目,修改 pom 配置,具体配置如下:

推送配置

<!--发布管理-->
<distributionManagement>
    <!--私库服务停用了,所以这里我选择注释掉-->
    <repository>
        <id>nexus</id>
        <name>releases</name>
        <url>http://xxx.xxx.xxx.xx:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus</id>
        <name>snapshots</name>
        <url>http://xxx.xxx.xxx.xx:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

拉取配置

这个主要是为了拉取我们的依赖能够成功。

<!--仓库管理-->
<repositories>
    <!--阿里云仓库-->
    <repository>
        <id>AliyunMaven</id>
        <name>Aliyun Maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <!--nexus私库停用 -->
    <repository>
        <id>nexus</id>
        <name>nexus-public</name>
        <url>http://116.205.112.132:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

构建配置

主要是为了借助 maven-deploy-plugin 插件将依赖推送至仓库中。

<!--构建管理-->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
                <verbose>false</verbose>
                <showWarnings>false</showWarnings>
                <fork>true</fork>
                <compilerVersion>1.5</compilerVersion>
                <meminitial>512m</meminitial>
                <maxmem>2048m</maxmem>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>${maven.source.version}</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>${maven.deploy.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                    <configuration>
                        <updateReleaseInfo>true</updateReleaseInfo>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

策略修改

有的时候,会提示推送失败:Failed to deploy artifacts: Could not transfer artifact,这是因为仓库推送策略导致的,默认不允许重复推送,修改推送策略为可重复推送就行。

应用推送

推送

进入我们项目根目录,通过命令行进行推送,切记路径不要有中文,会推送失败。

mvn clean deploy

等待推送结束,若失败,请自行百度查询具体原因:

验证

推送结束后,我们回到我们的仓库中看看是不是都推送上来了。

工具使用

依赖使用

推送上去之后,我们就可以通过仓库进行引入我们的工具了,具体怎么引用,可以直接去仓库看:

结束语

感谢大家的阅读,由于我购买的服务器资源有限,所以我的仓库不会使用,后期将会介绍如何使用阿里云效仓库(免费)。