自动上传nexus的python脚本mvndeploy.py

文章介绍了如何在Python中使用`mvndeploy`命令,通过`deploy_jar`和`deploy_pom`函数将JAR和POM文件上传到LotusCars的Nexus仓库,通过检查过程日志记录上传状态。
摘要由CSDN通过智能技术生成

coding=utf-8

import os
import subprocess
from util import Logger,YamlConfig

logging = Logger()
logger = logging.get_logger()
yaml = YamlConfig()
REPO_URL_SNAPSHOTS = yaml.repos[“snapshot”]
REPO_URL_RELEASES = yaml.repos[“release”]
def deploy_jar(file_path,file_name):
if ‘SNAPSHOT.jar’ in file_name:
url = ‘http://nexus-library.lotuscars.com.cn:8081/repository/’+REPO_URL_SNAPSHOTS
repositoryid = REPO_URL_SNAPSHOTS
else:
url = ‘http://nexus-library.lotuscars.com.cn:8081/repository/’+REPO_URL_RELEASES
repositoryid = REPO_URL_RELEASES
group_list = file_path.split(‘/’)
groupid = ‘.’.join(group_list[1:-3])
artifactid = group_list[-3]
version = group_list[-2]
#file = os.path.join(root,file_name)

mvn_command = ["mvn", "deploy:deploy-file", "-DgroupId={}".format(groupid), "-DartifactId={}".format(artifactid),
               "-Dversion={}".format(version), "-Dpackaging=jar", "-Dfile={}".format(file_path), "-Durl={}".format(url),
               "-DrepositoryId={}".format(repositoryid)]
print(mvn_command)
try:
    subprocess.check_call(mvn_command)
    logger.info('{} upload success'.format(file_name))
except subprocess.CalledProcessError as e:
    logger.info('Failed to upload {}'.format(file_name))

def deploy_pom(file_path,file_name):
if ‘SNAPSHOT.pom’ in file_name:
url = ‘http://nexus-library.lotuscars.com.cn:8081/repository/’+REPO_URL_SNAPSHOTS
repositoryid = REPO_URL_SNAPSHOTS
else:
url = ‘http://nexus-library.lotuscars.com.cn:8081/repository/’+REPO_URL_RELEASES
repositoryid = REPO_URL_RELEASES
group_list = file_path.split(‘/’)
groupid = ‘.’.join(group_list[1:-3])
artifactid = group_list[-3]
version = group_list[-2]
#file = os.path.join(root,file_name)

mvn_command = ["mvn", "deploy:deploy-file", "-DgroupId={}".format(groupid), "-DartifactId={}".format(artifactid),
               "-Dversion={}".format(version), "-Dpackaging=pom", "-Dfile={}".format(file_path), "-Durl={}".format(url),
               "-DrepositoryId={}".format(repositoryid)]
print(mvn_command)
try:
    subprocess.check_call(mvn_command)
    logger.info('{} upload success'.format(file_name))
except subprocess.CalledProcessError as e:
    logger.info('Failed to upload {}'.format(file_name))

def main():
for root, dirs, filenames in os.walk(‘.’):
for file_name in filenames:
file_path = os.path.join(root,file_name)
if file_name.endswith(‘.jar’):
deploy_jar(file_path,file_name)
elif file_name.endswith(‘.pom’):
deploy_pom(file_path,file_name)
else:
continue

if name == ‘main’:
main()

util.py

import logging
import yaml
import time
import sys

class Logger:
def init(self):
self.logger = logging.getLogger(name)
self.logger.setLevel(logging.INFO)
formatter = logging.Formatter(‘%(asctime)s-%(name)s-%(levelname)s-%(message)s’)

    filename_date = time.strftime("%Y-%m-%d-%H-%M",time.localtime())
    logfile_name = filename_date+'.log'
    fh = logging.FileHandler('./{}'.format(logfile_name),'a','utf-8')
    fh.setFormatter(formatter)
    fh.setLevel(logging.INFO)
    self.logger.addHandler(fh)

    sh = logging.StreamHandler(sys.stdout)
    sh.setFormatter(formatter)
    sh.setLevel(logging.INFO)
    self.logger.addHandler(sh)
def get_logger(self):
    return self.logger

class YamlConfig:
def init(self):
self.config = yaml.load(open(“./mvn_config.yaml”,“r”,encoding=“utf-8”),Loader=yaml.FullLoader)
self.access = self.config[‘access’]
self.repos = self.config[“repos”]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值