06-nexus私仓环境搭建

简介: 本文详细介绍Nexus Repository Manager OSS的安装与配置,涵盖JDK环境准备、服务部署、用户创建及启动操作。指导用户搭建Maven和Docker私有仓库,实现jar包与镜像的上传下载,并配置匿名访问与本地客户端信任。支持多种仓库类型,适用于企业级制品管理。

Nexus安装

  1. 检查服务器上是否安装有 JDK 1.8 +,如果没有则需要下载安装JDK。
  2. 到sonatype官网下载Nexus Repository Manager OSS

nexus有OSS版和PRO版。OSS版开源免费,PRO版需要付费。

此处下载OSS版

  1. 将下载的压缩包放到服务器解压

sonatype将Nexus安装包托管到了 Fastly CDN,国内访问Fastly CDN非常卡顿。可能会下载失败,需要多试几次。

tar -zxvf  nexus-3.30.0-01-unix.tar.gz

解压后会有两个文件夹:

  • nexus-3.69.0-02

nexus软件

  • sonatype-work

nexus工作目录。

该文件夹和nexus软件在相同路径中,最好不要改动。

  1. 修改nexus的配置

编辑 nexus-3.69.0-02/etc/nexus-default.properties 文件。

或者编辑sonatype-work/nexus3/etc/nexus.properties文件(推荐)

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=9091
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/
# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature
nexus.hazelcast.discovery.isEnabled=true
  1. 在linux上创建nexus用户:
# 添加用户
useradd nexus
# 配置密码 nexus
passwd nexus
  1. 使用nexus用户登录,并启动nexus:
# 启动
./nexus start
# 关闭
./nexus stop
# 查看状态
./nexus status
# 重启
./nexus restart
  1. 浏览器放问nexus

启动较缓慢,需要等待一段时间才能连上

http://192.168.xxx.xxx:9091/


首次登录时修改密码

  1. 进入nexus的web管理页面后,所有的配置都是只读的,需要登录才能操作
  2. 点击Sign In 进行登录默认用户名:admin默认密码:在 /home/nexus/sonatype-work/nexus3/admin.password  文件中

旧版本Nexus没有该密码文件,默认密码为:admin123

  1. 首次登录需要修改密码,将密码改为:admin

修改密码后,admin.password文件会被自动删除

  1. 配置是否允许匿名访问(配置为允许)

启用匿名访问意味着,用户可以在没有凭据的情况下从仓库搜索、浏览和下载组件。


Maven私仓

创建maven私仓

  1. 使用admin登录nexus
  2. 点击系统管理设置按钮(左上角小齿轮)
  3. 创建文件夹保存maven数据:进入 Repository -> Blob Storescreate blob store,类型选择File,名称输入my-maven-file,路径会自动生成,也可以自己调整。
  4. 创建私仓:进入 Repository -> Repositoriescreate repository,选择 maven2(hosted)(内网无法连接代理,只能为本机Maven)。
    Name: my-mavenOnline:默认勾选Version policy:选择Mixed(快照版和发布版都允许上传)Layout policy:默认StrictContent Disposition:默认 inlineBlob store:选择刚刚创建的my-maven-fileStrict Content Type Validation:默认勾选Deployment policy:选择 Allow redeploy(允许重复上传)点击Create repository完成创建


向maven私仓上传jar包

方式1(适合上传单个jar):使用admin登录页面,点击左侧Upload,选择my-maven,将需要上传的jar包上传即可。


方式2(适合上传多个jar):将需要上传的自己本地的资源库整体上传。

  1. 先将本地repository仓库文件夹打成一个完整的zip压缩包
  2. 上传到nexus服务器上
  3. 解压zip
  4. 进入repository目录
  5. 清理*.lastUpdated_remote.repositories文件
# 查看所有*.lastUpdated
find . -name '*.lastUpdated' -type f
# 删除*.lastUpdated
find . -name '*.lastUpdated' -type f -exec rm {} +
# 检查
find . -name '*.lastUpdated' -type f
# 查看所有 _remote.repositories 文件
find . -name '_remote.repositories' -type f
# 删除所有 _remote.repositories 文件
find . -name '_remote.repositories' -type f -exec rm {} +
# 检查
find . -name '_remote.repositories' -type f
# 查看所有resolver-status.properties 文件
find . -name 'resolver-status.properties' -type f
# 删除所有 resolver-status.properties 文件
find . -name 'resolver-status.properties' -type f -exec rm {} +
# 检查
find . -name 'resolver-status.properties' -type f
  1. 将本地仓库里面所有的maven-metadata-alimaven.xml改名为maven-metadata.xmlalimaven是本地maven的settings文件中设置的镜像仓库名)
find . -name "maven-metadata-alimaven.xml" -execdir mv {} maven-metadata.xml \;
  1. 编写 mvnimport.sh 脚本,内容如下
#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
 
while getopts ":r:u:p:" opt; do
    case $opt in
        r) REPO_URL="$OPTARG"
        ;;
        u) USERNAME="$OPTARG"
        ;;
        p) PASSWORD="$OPTARG"
        ;;
    esac
done
 
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
  1. 将私仓文件、sh脚本授权
  2. 执行shell脚本并传入参数
./mvnimport.sh -u admin -p admin -r http://192.168.xxx.xxx:9091/repository/my-maven/
  1. 等全部导入完毕后,在nexus控制台页面刷新即可看到已导入的jar


项目中使用该私仓

配置本地的maven的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>D:\PC_Document\Maven_Workspace\repository</localRepository>
  <pluginGroups></pluginGroups>
  <proxies></proxies>
  <servers></servers>
  <mirrors>
     <!-- 设置为内网nexus的maven私仓地址 -->
     <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>my maven</name>
      <url>http://192.168.xxx.xxx:9091/repository/my-maven/</url>
     </mirror>
  </mirrors>
  <profiles>
    <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>
      <!-- 设置一个profile,声明snapshots版的依赖、插件依赖的下载地址也为内网私仓地址 -->
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://192.168.xxx.xxx:9091/repository/my-maven/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://192.168.xxx.xxx:9091/repository/my-maven/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <!-- 启用上面声明的profile -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>


Docker私仓

创建Docker私仓

  1. 使用admin登录nexus
  2. 点击系统管理设置按钮(左上角小齿轮)
  3. nexus默认docker是失效的,需要 在security --> Realms,将docker配置成Active
  4. 创建文件夹保存maven数据:进入 Repository -> Blob Storescreate blob store,类型选择File,名称输入my-docker-file,路径会自动生成,也可以自己调整。
  5. 创建私仓:进入 Repository -> Repositoriescreate repository,选择 docker(hosted)
    Name: my-dockerOnline:默认勾选Http: 勾选,并填入将来连接该Docker仓库的端口号8889Https:已经勾选了http,可以不再勾选httpsAllow anonymous docker pull:勾选。(允许不登录时匿名下载镜像)Enable Docker V1 API:默认不勾选。(无需启动对V1旧版本API的支持)Blob store:选择刚刚创建的 my-docker-fileDeployment policy:选择 Allow redeploy(允许重复提交)点击Create repository完成创建


docker客户端对私仓镜像上传下载

需要先安装好Docker客户端,在/etc/docker/daemon.json中加入可信仓库,允许http连接:

{
        "insecure-registries": [
            "https://cce.test.com",
            "192.168.xxx.xxx:8889"
        ],
        "data-root":"/data/docker",
        "log-driver":"json-file",
        "log-opts":{
            "max-size":"200m",
            "max-file":"3"
        },
  "features": {
    "buildkit": true
  }
}

加入配置后,重启docker:

sudo systemctl daemon-reload
sudo systemctl restart docker


下载镜像:

# docker私仓配置了允许匿名下载,无需登录即可进行pull镜像
docker pull 192.168.xxx.xxx/my/tomcat:9.0.90-jre8


上传镜像:

# 上传镜像到私仓时,需要先登录
docker login -u admin -p admin 192.168.xxx.xxx:8889
# 将镜像push到私仓
docker push 192.168.xxx.xxx/aaa/aaa-portal:20241031-100820
# 登出
docker logout 192.168.xxx.xxx:8889


其他仓库

npm私仓、helm私仓等与前面搭建几乎一致。

与docker私仓类似,nexus默认npm也是是失效的,需要 在security --> Realms,将npm配置成Active

目录
相关文章
|
缓存 JavaScript 前端开发
从零开始的抢购脚本开发-油猴开发教程(多快好省)
从零开始的抢购脚本开发-油猴开发教程(多快好省)
486 0
|
1月前
|
网络协议
02 | 协议:怎么设计可扩展且向后兼容的协议?
本文深入讲解RPC协议的设计原理,从“透明化”出发,剖析协议在应用层的作用。通过对比HTTP协议,揭示RPC为何需设计私有、高效、可扩展的协议,并详解消息边界、序列化方式、协议头结构及扩展机制,强调协议设计中兼容性与性能的平衡,助力构建高性能分布式系统。(238字)
64 5
 02 | 协议:怎么设计可扩展且向后兼容的协议?
|
1月前
|
存储 开发工具 git
05-Gitlab容器环境搭建
本文介绍如何通过Docker搭建Gitlab CE(社区版)环境。包含拉取镜像、创建持久化目录、运行容器并映射配置、数据和日志卷,以及访问Gitlab并初始化项目的方法。详细说明了SSH与HTTP访问配置、初始密码获取,并提供本地代码上传的两种方式,帮助快速部署并使用私有代码仓库。
41 1
|
1月前
|
存储 NoSQL 算法
10-Docker安装Redis
本文介绍Docker安装Redis单机与集群部署,涵盖配置映射、数据持久化及3主3从集群搭建。深入解析Redis集群存储算法:哈希取余、一致性哈希与哈希槽,重点说明槽位分配机制及16384个槽的设计原理,并演示主从扩缩容操作流程。
183 0
|
1月前
|
Java Maven 数据安全/隐私保护
Nexus仓库
Nexus仓库是Sonatype推出的开源制品管理工具,支持Maven、Npm、Docker等格式。本文介绍其在Linux和Docker环境下的安装配置,包括JDK部署、OSS版下载、用户权限、匿名访问设置,以及仓库创建与上传下载操作,涵盖密码重置、数据持久化及脚本批量导入等内容,助力搭建高效私有仓库。
|
1月前
|
存储 关系型数据库 MySQL
07-Mysql容器环境搭建
本文介绍了MySQL的Docker环境搭建全过程,因CPU兼容性问题选用8.4.0-oraclelinux8镜像,详细说明了容器卷映射、配置文件设置、容器启动及数据库导入方法,并涵盖用户权限配置、数据备份与恢复、程序连接配置等关键操作,助力高效部署与管理MySQL数据库。
41 0
|
Arthas 监控 Java
(十一)JVM成神路之性能调优篇:GC调优、Arthas工具详解及各场景下线上最佳配置推荐
“在当前的互联网开发模式下,系统访问量日涨、并发暴增、线上瓶颈等各种性能问题纷涌而至,性能优化成为了现时代开发过程中炙手可热的名词,无论是在开发、面试过程中,性能优化都是一个常谈常新的话题”。
1644 3
|
监控 安全 持续交付
【专栏】Webhook是服务器主动发送事件通知的机制,打破传统客户端轮询模式,实现数据实时高效传递。
【4月更文挑战第29天】Webhook是服务器主动发送事件通知的机制,打破传统客户端轮询模式,实现数据实时高效传递。常用于持续集成部署、第三方服务集成、实时数据同步和监控告警。具有实时性、高效性和灵活性优势,但也面临安全风险和调试挑战。理解并善用Webhook能提升系统性能,广泛应用于现代软件开发和集成。
1261 0
|
机器学习/深度学习 人工智能 运维
基于AI的自动化事件响应:智慧运维新时代
基于AI的自动化事件响应:智慧运维新时代
548 11
|
JavaScript Java 测试技术
从零开始:Nexus私服搭建与Maven仓库配置的完全指南
从零开始:Nexus私服搭建与Maven仓库配置的完全指南
23603 7

热门文章

最新文章