【运维知识高级篇】一篇文章带你上手Maven私服nexus!

这篇文章带大家了解nexus私服的内容,我们在maven编译的时候需要拉取很多依赖,如果是用maven官方的中央仓库拉取的话,有些依赖比较缓慢,影响构建速度。我们通常有两种解决方式,一种是将maven拉取镜像的数据源修改成阿里云这种国内的镜像,还有一种是可以配置一个nexus私服,去保存一些常见的依赖,也可以当yum仓库使用,存放rpm包,这种nexus私服,类似于yum仓库,harbor仓库。

文章目录

  • 修改Maven数据源
  • 部署Nexus
  • Nexus基础配置与使用

修改Maven数据源

我们先来介绍第一种提升Maven构建速度的方式,修改成国内的数据源。
1、将下面的配置内容复制到我们maven的配置文件里,找mirror字样的。

 
nexus-aliyun 
* 
Nexus aliyun 
http://maven.aliyun.com/nexus/content/groups/public 

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第1张图片

2、测试数据源是否是通过阿里云的源下载。

[root@Jenkins ~]# cd hello-world-war/
[root@Jenkins hello-world-war]# mvn clean
...
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (0 B at 0.0 KB/sec)
...
[root@Jenkins hello-world-war]# mvn package
...
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-filtering/1.0-beta-2/maven-filtering-1.0-beta-2.jar (0 B at 0.0 KB/sec)
[INFO] Packaging webapp
[INFO] Assembling webapp [hello-world-war] in [/root/hello-world-war/target/hello-world-war-1.0.0]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/hello-world-war/src/main/webapp]
[INFO] Webapp assembled in [268 msecs]
[INFO] Building war: /root/hello-world-war/target/hello-world-war-1.0.0.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 45.227 s
[INFO] Finished at: 2023-09-05T22:30:52+08:00
[INFO] Final Memory: 11M/77M
[INFO] ------------------------------------------------------------------------

部署Nexus

浏览器访问:https://www.sonatype.com/download-oss-sonatype
进入nexus官网,填写一些信息,点击download就可以下载了,但是需要填公司邮箱,蛮麻烦的,文末有我自己准备的nexus-3.13.0-01-unix.tar下载链接,相对方便些。

[root@Nexus ~]# rpm -ivh jdk-8u181-linux-x64.rpm 
[root@Nexus ~]# tar xf nexus-3.13.0-01-unix.tar.gz
[root@Nexus ~]# mv nexus-3.13.0-01 /usr/local/nexus
[root@Nexus ~]# /usr/local/nexus/bin/nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexus
[root@Nexus ~]# /usr/local/nexus/bin/nexus status
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
nexus is running.

需要多等一会,大概几分钟。因为他是把内存变成了仓库,如果担心出问题可以使用/usr/local/nexus/bin/nexus run,在前台运行
端口是8081,有端口后浏览器访问10.0.0.202:8081即可。

[root@Nexus nexus]# ss -ntl|grep 8081
LISTEN     0      50           *:8081                     *:*                  

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第2张图片

Nexus基础配置与使用

右上角登陆用户,账号admin,密码admin123
【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第3张图片

1、配置备用依赖源

点进center仓库

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第4张图片

更换备用源为阿里源,如果这个仓库中没有相应的依赖,会从阿里源去拉取。

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第5张图片

修改后下滑保存

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第6张图片

2、复制public源

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第7张图片

http://10.0.0.202:8081/repository/maven-public/

复制上面的地址,把它替换到maven配置文件中即可。

【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第8张图片
下滑可以看到在这个镜像仓库的组里有三个成员,在上面的源搜索其实是在这三个仓库里去寻找依赖,如果这三个库都没有,就会去刚才配置的阿里源里面去寻找。
【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第9张图片
3、改动maven的依赖源
需要改动的地方很多,可以直接复制我改好的作为setting.xml

[root@Jenkins ~]# cd /usr/local/maven/conf/
[root@Jenkins conf]# mv settings.xml settings.xml.bak
[root@Jenkins conf]# cat settings.xml






  

  

  

  
  
    
  

  
  
    
  

  
  
    

    
        
     my-nexus-releases   
     admin   
     admin123   
        
        
     my-nexus-snapshot   
     admin   
     admin123   
     
  

  
  
  
  nexus
  *
  http://10.0.0.202:8081/repository/maven-public/

    
  

  
  
    

    

  nexus
  
    
      central
      http://10.0.0.202:8081/repository/maven-public/
      true
      true
    
  
 
    
      central
      http://10.0.0.202:8081/repository/maven-public/
      true
      true
    
  

  

  

nexus


4、试用nexus仓库

[root@Jenkins conf]# cd ~/hello-world-war/
[root@Jenkins hello-world-war]# mvn clean
...
[INFO] Building Hello World Web Application Repository 1.0.0
[INFO] ------------------------------------------------------------------------
Downloading: http://10.0.0.202:8081/repository/maven-public/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
...
[root@Jenkins hello-world-war]# mvn package
Downloaded: http://10.0.0.202:8081/repository/maven-public/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar (0 B at 0.0 KB/sec)
[INFO] Packaging webapp
[INFO] Assembling webapp [hello-world-war] in [/root/hello-world-war/target/hello-world-war-1.0.0]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/hello-world-war/src/main/webapp]
[INFO] Webapp assembled in [172 msecs]
[INFO] Building war: /root/hello-world-war/target/hello-world-war-1.0.0.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:33 min
[INFO] Finished at: 2023-09-10T20:46:24+08:00
[INFO] Final Memory: 11M/46M
[INFO] ------------------------------------------------------------------------

试用首次package的时候还是需要很久,因为202仓库里面没有这些依赖,需要从阿里源去拉取。
【运维知识高级篇】一篇文章带你上手Maven私服nexus!_第10张图片
我们再次进行清理和打包,发现打包时间又一分半变成了6秒。

[root@Jenkins hello-world-war]# mvn clean
[root@Jenkins hello-world-war]# mvn package
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.674 s
[INFO] Finished at: 2023-09-10T20:47:31+08:00
[INFO] Final Memory: 10M/97M
[INFO] ------------------------------------------------------------------------

Nexus私服下载链接:https://pan.baidu.com/s/1i0uf1Qy5UuYRJcm_3mLCpw?pwd=7fxh
JDK下载链接:https://pan.baidu.com/s/1sGftvjyOtGI4ji4zRMFi1Q?pwd=nlyo

你可能感兴趣的:(运维知识分享,#,高级运维知识,运维,maven,java,nexus,jenkins,ci/cd,ci)