python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python上传本地maven到nexus

Python实现批量上传本地maven库到nexus

作者:onlyonexhj

这篇文章主要为大家详细介绍了如何使用Python实现批量上传本地maven库到nexus,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以参考下

背景:外包类项目开发时是调用的公司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)

到此这篇关于Python实现批量上传本地maven库到nexus的文章就介绍到这了,更多相关Python上传本地maven到nexus内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文