Nexus私服搭建

119 阅读3分钟

7-5405.jpg

前言

想必每个 Java 开发者都接触过MavenMaven 作为 Java 项目管理工具,提供了帮助管理构建、文档、报告、依赖、scms、发布、分发的方法,可以方便的编译代码、进行依赖管理、管理二进制库等等。

但是今天的主角是并不是Maven,而是Maven私服仓库。私服仓库其实就是一个本地化的中央仓库,但是私有化部署了后,在拉包速度、安全控制上面都有了很大的提升。

Maven 私服

Maven私服仓库的优势

  • 下载速度更快:Maven 私服可以部署在局域网内,从私服下载构建更快更稳定。
  • 便于部署第三方构件:例如,某公司或组织内部的私有构件、Oracle 的 JDBC 驱动等,建立私服之后,就可以将这些构件部署到私服中,供内部 Maven 项目使用。
  • 提高项目的稳定性,增强对项目的控制:如果不建立私服,那么 Maven 项目的构件就高度依赖外部的远程仓库,若外部网络不稳定,则项目的构建过程也会变得不稳定;增加权限管理、RELEASE/SNAPSHOT 版本控制等,可以对仓库进行一些更加高级的控制。

搭建工具

能够帮助我们建立 Maven 私服的软件被称为 Maven 仓库管理器(Repository Manager),主要有以下 3 种:

  • Apache Archiva
  • JFrog Artifactory
  • Sonatype Nexus

我这里选择了开源的Nexus,版本是3.x版本

搭建步骤

环境准备

  • ubuntu16.04 - 4cpu - 8G
  • JDK1.8

下载和安装

1. 下载地址

www.sonatype.com/products/so…

image-20230928150606825

输入必要内容

image-20230928150630387

2. 配置和启动

解压下载的压缩包

image-20231007111135112

进入nexus-3.60.0-02/etc目录下,按需修改对应的配置文件nexus-default.properties,如端口

image-20231007111117261

修改配置后,进入nexus-3.60.0-02/bin目录下

image-20231007111059449

 # 启动命令
 ./nexus start
 ​
 # 结束命令
 ./nexus stop
 ​
 # 查看进程命令
 ps -ef | grep "nexus"

image-20231007111025701

如此则表示程序启动成功,可通过http访问管理界面

image-20231007111343235

配置仓库

1. 管理员登录

进入sonatype-work/nexus3目录,ls 后看到admin.password,这个就是Nexus3的默认密码

image-20231007112047334

2. 仓库类型介绍

image-20231007112841712

2. 1 proxy

代理类型,当私服里边找不到包的时候,会通过代理类型的仓库去中央仓库或者三方仓库去查找包,如配置一个国内的阿里代理仓库

image-20231007113307336

2. 2 host

本地仓库类型,比如公司内部项目打包会发布到这个类型仓库下

image-20231007113441887

snapshots表示快照版本,如发布包的版本有snapshot字样,那么这个包会被打包到这个仓库下;

release区别于快照版本,那么稳定的、正式版就被发布到这个仓库下。

2. 3 group

组合仓库类型,是按照顺序排列的一组仓库,当寻找包的时候会按照这个顺序查找

image-20231007114007272

Java项目使用

pom文件样例

 <!-- 要部署的maven私服地址 -->
     <distributionManagement>
         <repository>
             <id>nexus-releases</id>
             <url>http:xxx/repository/maven-releases/</url>
             <name>nexus私服中宿主仓库->存放/下载稳定版本的构件</name>
         </repository>
         <snapshotRepository>
             <id>nexus-snapshots</id>
             <url>http:xxx/repository/maven-snapshots/</url>
             <name>nexus私服中宿主仓库->存放/下载快照版本的构件</name>
         </snapshotRepository>
     </distributionManagement>

maven配置文件样例

 <?xml version="1.0" encoding="UTF-8"?>
 ​
 ​
 <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
 ​
     <localRepository>/Users/liutong/DevelopTools/maven_3_8/nexus-mr</localRepository>
     <pluginGroups></pluginGroups>
     <proxies></proxies>
 ​
 ​
     <!-- 镜像 -->
     <mirrors>
         <mirror>
             <id>nexus</id>
             <mirrorOf>*</mirrorOf>
             <url>http://xxx/repository/maven-public</url>
         </mirror>
     </mirrors> 
 ​
     
     <servers>
         <!-- 下载用户 -->
         <server>
             <id>nexus</id>
             <username>you username</username>
             <password>you password</password>
         </server>
 ​
         <!-- 部署用户 -->
         <server>
             <id>nexus-releases</id>
             <username>you username</username>
             <password>you password</password>
         </server>
         <server>
             <id>nexus-snapshots</id>
             <username>you username</username>
             <password>you password</password>
         </server>
     </servers>
 ​
     
 ​
     <!-- 策略 -->
     <profiles>
         <profile>
             <id>dev</id>
             <repositories>
                 <repository>
                     <id>nexus</id>
                     <url>http://xxx/repository/maven-public/</url>
                     <!-- 是否允许检索发布版本(release)和快照版本(snapshot) -->
                     <releases>
                         <enabled>true</enabled>
                     </releases>
                     <snapshots>
                         <enabled>true</enabled>
                     </snapshots>
                 </repository>
             </repositories>
 ​
             <pluginRepositories> 
                 <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 -->
                 <pluginRepository> 
                     <id>nexus</id> 
                     <url>http://xxx/repository/maven-public/</url> 
                 </pluginRepository> 
             </pluginRepositories> 
         </profile>
     </profiles>
 ​
     <!-- 配置使用哪种策略 -->
     <activeProfiles>
         <activeProfile>dev</activeProfile>
     </activeProfiles>
 </settings>
 ​