上传jar包到私有仓库nexus3

上传Jar包到私有仓库Nexus3

官方文档

上传组件(Jar包)

POST /service/rest/v1/components
# 官方例子:
curl -v -u admin:admin123 -X POST 'http://localhost:8081/service/rest/v1/components?repository=maven-releases' 
-F maven2.groupId=com.google.guava 
-F maven2.artifactId=guava 
-F maven2.version=24.0-jre 
-F maven2.asset1=@guava-24.0-jre.jar   # 需要上传的二进制文件,此处为jar包
-F maven2.asset1.extension=jar         #对应jar包的扩展,此处为jar
-F maven2.asset2=@guava-24.0-jre-sources.jar  # 需要上传的二进制文件,此处为jar源码包
-F maven2.asset2.classifier=sources #
-F maven2.asset2.extension=jar  #对应源码包的扩展,此处为jar

参数

Field name Field type Required? Description
maven2.groupId String Yes, unless a POM asset is included in the upload Group ID of the component
maven2.artifactId String Yes, unless a POM asset is included in the upload Artifact ID of the component
maven2.version String Yes, unless a POM asset is included in the upload Version of the component
maven2.generate-pom Boolean No Whether the Nexus Repository Manager should generate a POM file based on above component coordinates provided
maven2.packaging String No Define component packaging (e.g. jar, ear)
maven2.assetN File Yes, at least one Binary asset
maven2.assetN.extension String Yes Extension of the corresponding assetN asset
maven2.assetN.classifier String No Classifier of the corresponding assetN asset

使用Python上传文件

文件结构

image-20220609090307178

python代码

import os

import requests

"""上传组件到nexus3"""
# 需要上传的文件根目录 jar包所在目录
base_dir = r'F:/uploadjar'
# 上传的仓库名
repo = 'fxxx'
# nexus3的用户名/密码
auth_user = 'admin'
auth_pwd = 'admin123'
# nexus3地址
url = 'http://xxx.xx.x.xxx:xxxx'


def get_params(path):
    """ 从path中获取参数  """
    array = path.split('/')
    group = ".".join(array[0:len(array) - 3])
    artifact = array[len(array) - 3]
    version = array[len(array) - 2]
    extension = path[path.rindex('.') + 1:]
    return group, artifact, version, extension


def get_shell(file, group, artifact, version, extension):
    """ 拼装shell脚本  """
    shell_str = f"curl -v -u {auth_user + ':' + auth_pwd} -X POST '{url}/service/rest/v1/components?repository={repo}' " \
                f"-F maven2.groupId={group} -F maven2.artifactId={artifact} -F maven2.version={version} " \
                f"-F maven2.asset1=@{file} -F maven2.asset1.extension={extension}"
    print(shell_str)
    return shell_str


def do_request(file, group, artifact, version, extension):
    """发起上传请求"""
    file_path = os.path.join(base_dir, file)
    requests.post(f"{url}/service/rest/v1/components?repository={repo}", auth=(auth_user, auth_pwd),
                  data={'maven2.groupId': group, 'maven2.artifactId': artifact, 'maven2.version': version,
                        'maven2.asset1.extension': extension},
                  files={'maven2.asset1': open(file_path, 'rb')})


def traversal_files(path_str):
    """ 遍历目录  """
    for path_name in os.listdir(path_str):
        path_name = os.path.join(path_str, path_name)
        path1 = path_name.removeprefix(base_dir + os.sep).replace('\\', '/')
        # 判断当前目录是否为文件夹
        if os.path.isdir(path_name):
            traversal_files(path_name)
        elif path_name.endswith('jar') or path_name.endswith('pom'):
            params = get_params(path1)
            get_shell(path1, *params)
            do_request(path1, *params)


# 遍历目录并上传
# traversal_files(base_dir)
# 测试上传
# do_request('ant/ant/1.6.5/ant-1.6.5.jar', 'ant', 'ant', '1.6.5', 'jar')
	

权限说明

image-20240402134954513

posted @   菜阿  阅读(965)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示