文章目录
安装
下载地址
https://www.sonatype.com/download-oss-sonatype
nexus界面
Maven私库格式
maven规定了一个仓库标准,Central仓库是最大的Maven在线仓库,它遵守Maven仓库标准用于很多开源项目的发布。
Version policy–版本策略
Release
发布稳定版本的jar包,maven central用的是这种。
Snapshot
snapshot仓库适合持续开发。这些jar包名称一般以-SNAPSHOT
结尾。这种仓库允许重复上传相同版本号的jar包,区别与release仓库一旦上传jar包将不可更改。
Mixed
混合类型的仓库可以部署release包和snapshot包。
Layout policy-- 结构策略
Maven制定了标准的目录结构和文件命名,但是因为历史原因,一些构建工具比如sbt或者自定义开发的构建工具没有遵循这些标准。为了保持兼容性
- Permissive
配置Permissive策略允许仓库的assets(上传的artifact)违反标准 - Strict
上传的artifact必须遵守标准。
私库结构
私库分类分成三种,proxy,hosted,group。三者之间的关系如下:
Settings.xml
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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers>
<server>
<id>nexus</id>
<!--配置你的密码-->
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<proxies/>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
项目如何发布到私库?
<project>
<!--省略---->
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases</name>
<url>http://localhost:8081/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot</name>
<url>http://localhost:8081/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
私库管理规范
组织内部按照角色划分为多个私库,如图所示,代理私库从在线仓库下载,经过CI工具发布到Stage仓库。Stage仓库用于测试jar包稳定性,最终经过稳定性测试的jar包放到生产私库中。
批量上传本地依赖到Nexus私库
用法:
第一个参数传本地缓存目录,如/repository
第二个参数传hosted私库地址
#!/bin/bash
DEPENDENCY_DIR=$1
PRIVATE_REPOURL=$2/service/rest/v1/components?repository=snap
i=0
artifacts=$(find $DEPENDENCY_DIR | grep pom$)
for item in $artifacts
do
dir_temp=$(dirname $item)
#echo $dir_temp
for file in $(ls $dir_temp)
do
#extension=$(echo $file |awk -F '.' '{print $2}')
if [[ "$file" =~ pom$ ]]
then
pom_path=$dir_temp/$file
fi
if [[ "$file" =~ jar$ ]]
then
jar_path=$dir_temp/$file
fi
done
echo "-------------------------------"
echo $pom_path
echo $jar_path
if [[ -z $jar_path ]]
then
# only pom exist
curl -v -u admin:icywater -X POST -F "maven2.generate-pom=false" -F "maven2.asset1=@$pom_path" -F "maven2.asset1.extension=pom" $PRIVATE_REPOURL
else
# pom and jar both exist
curl -v -u admin:icywater -X POST -F "maven2.generate-pom=false" -F "maven2.asset1=@$pom_path" -F "maven2.asset1.extension=pom" -F "maven2.asset2=@$jar_path;type=application/java-archive" -F "maven2.asset2.extension=jar" $PRIVATE_REPOURL
fi
pom_path=""
jar_path=""
done
备份与恢复
[root@gitlab nexus]# ls -l
total 156276
drwxr-xr-x. 9 root root 163 Sep 28 08:21 nexus-3.25.0-03
-rw-r--r--. 1 root root 160022971 Sep 28 08:21 nexus-3.25.0-03-unix.tar.gz
drwxr-xr-x. 3 root root 20 Sep 28 08:22 sonatype-work
全部文件都拷贝到另一个机器就可以了,数据存在sonatype-work里。
Reference List
- https://help.sonatype.com/repomanager3/formats/maven-repositories
- nexus小笔记