当前位置:   article > 正文

nexus3 maven迁移仓库迁移_nexus3仓库迁移

nexus3仓库迁移

迁移背景:

nexus 3.33 升级到 nexus 3.64 过程中,私服 maven 的 snapshot 和 hosted 无法上传。由于这个 snapshot 和 hosted 和 npm-proxy 放的同一个 blob存储,无法单独拆除去,所以采用迁移的方式

迁移思路:

down下来 snapshot 和 hosted 仓库,然后 批量上传,下方的代码演示的 release 更改相关的仓库地址即可迁移 snapshot

技术栈:

python 正则

maven配置:

此处配置的 server 和 mirror 分别是上传的凭证和上传的仓库,此处我配置了 snapshot 和 release 两个的!

...
    <server>
      <id>xinyunkeji-snapshot</id>
      <username>test-user</username>
      <password>test-pass</password>
    </server>
    <server>
      <id>xinyunkeji-release</id>
      <username>test-user</username>
      <password>test-pass</password>
    </server>
...
<mirror>
    <id>xinyunkeji-snapshot</id>
    <mirrorOf>*</mirrorOf>
    <name>xinyunkeji-snapshot</name>
    <url>https://mirrors.xinyunkeji.com/repository/maven-test-snapshot2</url>
</mirror>
<mirror>
    <id>xinyunkeji-release</id>
    <mirrorOf>*</mirrorOf>
    <name>xinyunkeji-snapshot</name>
    <url>https://mirrors.xinyunkeji.com/repository/maven-test-release-test</url>
</mirror>
...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

down仓库的python文件:

import os
import re
import requests
from urllib.parse import unquote

def decode_urls(url_list):
    decoded_urls = [unquote(url) for url in url_list]
    return decoded_urls

def download_url(url, save_dir):
    response = requests.get(url)

    # 检查响应状态码
    if response.status_code == 200:
        # 获取URL的基本路径
        base_url = '/'.join(url.split('/')[:-1])

        # 解析HTML内容
        html_content = response.text

        # 搜索所有链接
        links = find_links(html_content)
        # 遍历链接
        for link in links:
            file_url = base_url +"/"+ link


            # 检查链接是否为目录
            if link.endswith('/'):

                # 创建本地目录
                save_subdir = os.path.join(save_dir, link)
                os.makedirs(save_subdir, exist_ok=True)

                # 递归下载子目录
                download_url(file_url, save_subdir)
            else:
                # 下载文件
                save_file = link.split("/")[-1]
                download_file(link, save_dir+save_file)
    else:
        print(f"Failed to download URL: {url}")


def find_links(html_content):
    # 使用正则表达式或HTML解析库解析HTML内容,提取所有链接
    # 例如,可以使用正则表达式 r'<a\s+href=[\'"](.*?)[\'"]\s*>' 来提取链接
    # 返回一个包含所有链接的列表
    # 使用正则表达式匹配链接
    pattern = r'<a\s+href=[\'"](.*?)[\'"]\s*>'
    matches = re.findall(pattern, html_content)
    matches = decode_urls(matches)
    if '../' in matches:
        matches.remove('../')
    print(matches)

    # 返回匹配到的链接列表
    return matches


def download_file(url, save_path):
    response = requests.get(url, stream=True)

    # 检查响应状态码
    if response.status_code == 200:
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
    else:
        print(f"Failed to download file: {url}")


# 指定下载URL和保存目录(这里是从那个不能上传损坏的仓库下载)
url = "https://mirrors.xinyunkeji.com/service/rest/repository/browse/maven-test-release/"
save_dir = '/opt/maven-hosted/download'

# 创建保存目录(如果不存在)
os.makedirs(save_dir, exist_ok=True)

# 开始下载
download_url(url, save_dir)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

批量上传到maven仓库

import os,subprocess

def find_jar_pom_files(directory):
    # 遍历目录及其子目录下的所有文件和目录
    for root, dirs, files in os.walk(directory):
        # 对于每个文件名,检查它是否以 .jar 结尾
        for filename in files:
            if filename.endswith('.jar'):
                # 构造 POM 文件名,并获取其完整路径
                pom_filename = filename[:-4] + '.pom'
                pom_filepath = os.path.join(root, pom_filename)
                # 如果 POM 文件存在,则输出目录路径、JAR 文件名和 POM 文件名
                if os.path.exists(pom_filepath):
                    print(f"Directory: {root}")
                    print(f"JAR file: {filename}")
                    print(f"POM file: {pom_filename}")
                    jar_filename=root + "/" + filename
                    pom_filename=root + "/" + pom_filename
                    #此处file是jar包,pomfile是pom文件,url是maven中配置需要上传到的仓库,后面的id是maven配置server的凭证id,此处使用jar和pom上传,也可用一系列id信息上传,只不过批量上传的话,还得获取相关id的信息,参考如下
                    #mvn deploy:deploy-file -DgroupId=com.aaa -DartifactId=bbb -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=bbb-0.0.1-SNAPSHOT.jar -Durl=http://192.168.1.189:8081/repository/maven-snapshots/ -DrepositoryId=nexus-snapshot
                    command = "mvn deploy:deploy-file -Dfile={} -DpomFile={} -Durl=https://mirrors.xinyunkeji.com/repository/maven-test-release-test/ -DrepositoryId=xinyunkeji-release".format(jar_filename, pom_filename)
                    print(command)
                    print("\n")
                    # 使用 subprocess.run() 函数执行命令,并捕获输出结果
                    result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)



# 设置要遍历的目录路径
directory = '/opt/maven-hosted/download'
# 调用函数,查找所有包含 JAR 和 POM 文件的目录
find_jar_pom_files(directory)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/48671
推荐阅读
相关标签
  

闽ICP备14008679号