06-nexus私仓环境搭建
Nexus安装检查服务器上是否安装有 JDK 1.8 +,如果没有则需要下载安装JDK。到sonatype官网下载Nexus Repository Manager OSSnexus有OSS版和PRO版。OSS版开源免费,PRO版需要付费。此处下载OSS版将下载的压缩包放到服务器解压sonatype将Nexus安装包托管到了 Fastly CDN,国内访问Fastly CDN非常卡顿。可能会下载失败,需要多试几次。
解压后会有两个文件夹:nexus-3.69.0-02nexus软件sonatype-worknexus工作目录。该文件夹和nexus软件在相同路径中,最好不要改动。修改nexus的配置编辑 nexus-3.69.0-02/etc/nexus-default.properties 文件。或者编辑sonatype-work/nexus3/etc/nexus.properties文件(推荐)在linux上创建nexus用户:使用nexus用户登录,并启动nexus:浏览器放问nexus启动较缓慢,需要等待一段时间才能连上http://192.168.xxx.xxx:9091/首次登录时修改密码进入nexus的web管理页面后,所有的配置都是只读的,需要登录才能操作点击Sign In 进行登录默认用户名:admin默认密码:在 /home/nexus/sonatype-work/nexus3/admin.password 文件中旧版本Nexus没有该密码文件,默认密码为:admin123首次登录需要修改密码,将密码改为:admin修改密码后,admin.password文件会被自动删除配置是否允许匿名访问(配置为允许)启用匿名访问意味着,用户可以在没有凭据的情况下从仓库搜索、浏览和下载组件。Maven私仓创建maven私仓使用admin登录nexus点击系统管理设置按钮(左上角小齿轮)创建文件夹保存maven数据:进入 Repository -> Blob Stores,create blob store,类型选择File,名称输入my-maven-file,路径会自动生成,也可以自己调整。创建私仓:进入 Repository -> Repositories,create 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):将需要上传的自己本地的资源库整体上传。先将本地repository仓库文件夹打成一个完整的zip压缩包上传到nexus服务器上解压zip进入repository目录清理*.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
将本地仓库里面所有的maven-metadata-alimaven.xml改名为maven-metadata.xml(alimaven是本地maven的settings文件中设置的镜像仓库名)
find . -name "maven-metadata-alimaven.xml" -execdir mv {} maven-metadata.xml \;
编写 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}/{} ;
将私仓文件、sh脚本授权执行shell脚本并传入参数
./mvnimport.sh -u admin -p admin -r http://192.168.xxx.xxx:9091/repository/my-maven/
等全部导入完毕后,在nexus控制台页面刷新即可看到已导入的jar