使用Python批量上传本地maven库到nexus

发布时间:2024年01月14日

背景:外包类项目开发时是调用的公司maven仓库进行开发,交付后需要将maven仓库转移到客户环境。

原理:1、打开idea运行源代码,将maven包下载到本地仓库,

? ? ? ? ? ?2、下载包所在目录中执行脚本将本地仓库的maven包上传到客户nexus

脚本代码如下:

# -*- coding: utf-8 -*-
import os
import subprocess

# releases仓库地址
REPO_URL_RELEASES = "http://192.168.2.230:8081/repository/hosted-release/"
# snapshots仓库地址
REPO_URL_SNAPSHOTS = "http://192.168.2.230:8081/repository/hosted-snapshot/"


USERNAME = "admin"
PASSWORD = "admin"

def upload_files(repo_url, username, password):
    for root, dirs, files in os.walk("."):
        for file_name in files:
            if file_name.endswith(".sh"):
                continue
            if file_name.startswith("."):
                continue
            if "-SNAPSHOT" in root:
                continue
            if "_remote.repositories" in file_name:
                continue
            if file_name.startswith("^archetype-catalog.xml") or file_name.startswith("^maven-metadata-local.xml") or file_name.startswith("^maven-metadata-deployment.xml"):
                continue

            file_path = os.path.join(root, file_name)
            upload_url = os.path.join(repo_url, file_path[2:])
            curl_command = ["curl", "-u", "{}:{}".format(username, password), "-X", "PUT", "-v", "-T", file_path, upload_url]

            try:
                subprocess.check_call(curl_command)
            except subprocess.CalledProcessError as e:
                print("Failed to upload {}: {}".format(file_path, e))
            else:
                print("Uploaded: {}".format(file_path))

if __name__ == "__main__":
    print("Uploading releases...")
    upload_files(REPO_URL_RELEASES, USERNAME, PASSWORD)

    print("\nUploading snapshots...")
    upload_files(REPO_URL_SNAPSHOTS, USERNAME, PASSWORD)

文章来源:https://blog.csdn.net/onlyonexhj/article/details/135581531
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。