0
点赞
收藏
分享

微信扫一扫

将自建nexus的maven仓库迁移到阿里云私有仓库

GhostInMatrix 2023-05-09 阅读 84

背景: 原先公司使用的是自建nexus存储maven依赖包,后面全部迁移到阿里云流水线后,则maven仓库也同步迁移到阿里云


第一种:

1.在阿里云私有仓库设置代理,然后在阿里云下载自己的maven settings文件

将自建nexus的maven仓库迁移到阿里云私有仓库_JSON

2个仓库都需要设置,一个是snapshot,另外一个是release


第二种:

1.迁移release仓库,阿里云不支持迁移snapshot仓库,snapshot仓库直接使用代理方式解决

a.下载release仓库依赖包

#!/bin/bash
PASS="nexususer:nesxspassword"
URL="https://nexus.abc.com/service/rest/v1/components?repository="
REPS="maven-releases"
BASE_DIR=/tmp/maven
JSON_BASEDIR=${BASE_DIR}/json
M2_BASEDIR=${BASE_DIR}/m2

yum -y install curl wget jq

function getjson()
{
        JSON_SAVE_DIR=$1
        REP_NAME=$2
        CON_TOKEN=$3
        mkdir -p ${JSON_BASEDIR}/${REP_NAME}
        if [ -z ${CON_TOKEN}  ];then
                REQUEST_URL=${URL}${REP_NAME}
                file=1.json
        else
                REQUEST_URL="${URL}${REP_NAME}&continuationToken=${CON_TOKEN}"
                file=${CON_TOKEN}.json
        fi
        curl -s -u ${PASS} -H 'accept: application/json' ${REQUEST_URL} -o ${JSON_SAVE_DIR}/${file}
        RES_CON_TOKEN=$(cat ${JSON_SAVE_DIR}/${file}|jq .continuationToken|sed 's#"##g')
        if [ ${RES_CON_TOKEN} != 'null' ];then
                getjson ${JSON_SAVE_DIR} ${REP_NAME} ${RES_CON_TOKEN}
        fi

}
function downjson()
{
        for a in ${REPS};
        do
                JSON_DIR=${JSON_BASEDIR}/${a}
                rm -rf ${JSON_DIR}/*
                getjson ${JSON_DIR} ${a}
        done
}


function downm2()
{
for a in ${REPS};
do
        cd ${JSON_BASEDIR}/${a}
        mkdir -p ${M2_BASEDIR}/${a}
        for b in `ls`;
        do
                item_length=$(cat $b|jq '.items|length')
                echo $item_length
                for((i=0;i<${item_length};i++))
                do
                        asset_length=$(cat $b|jq '.items['${i}'].assets|length')
                        for((k=0;k<${asset_length};k++))
                        do
                                download_url=$(cat $b|jq '.items['${i}'].assets['$k'].downloadUrl'|sed 's#"##g')
                                path=$(cat $b|jq '.items['${i}'].assets['$k'].path'|sed 's#"##g')
                                curl -s -u ${PASS} ${download_url} -o ${M2_BASEDIR}/${a}/${path} --create-dirs
                        done
                done

        done
done
}

downjson
downm2

b.批量上传至阿里云私有仓库

wget https://agent-install.oss-cn-hangzhou.aliyuncs.com/migrate-local-repo-tool.jar
java -jar migrate-local-repo-tool.jar -cd "/tmp/maven/m2" -t "https://packages.aliyun.com/maven/repository/xxxxxxxxxxxxxxx" -u username -p password

备注: 如果不知道如何密码可以自行下载maven的settings文件查看

将自建nexus的maven仓库迁移到阿里云私有仓库_json_02

举报

相关推荐

0 条评论