特若网络 服务器运维 nexus 批量导入本地库

nexus 批量导入本地库

最近要在内网搭建nexus服务,因无法访问互联网,所以只能把依赖包上传到nexus中,通过baidu搜索发现 …

最近要在内网搭建nexus服务,因无法访问互联网,所以只能把依赖包上传到nexus中,通过baidu搜索发现

nexus2可以通过直接把本地文件目录直接放到服务器相应目录解决:

复制D:\maven\repository(本地仓库)到D:\sonatype-work\nexus\storage\central(nexus库路径)

nexus3无法通过上面的方式解决,但是从网友那得到了一个上传脚本如下:

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
    case $opt in
        r) REPO_URL="$OPTARG"
        ;;
        u) USERNAME="$OPTARG"
        ;;
        p) PASSWORD="$OPTARG"
        ;;
    esac
done
  
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

但是该上传脚本无法多线程上传,比较慢,所以自己根据该shell脚本做了python多线程版,如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import requests
from requests.auth import HTTPBasicAuth
import sys
if sys.version > '3':
    import queue as Queue
else:
    import Queue
import threading
import time

repoUrl='http://10.xx.xx.xx:8081/repository/maven-release'
userName='xxxx'
password='gjx2021'
repoPath='D:/sdk/repository'

threadNum=10

def putFile(filePath,homePath,url):
    path = homePath+'/'+filePath
    print('正在上传' + path)
    try:
        url = repoUrl+'/'+filePath
        r = requests.put(url, data=open(path,'rb').read(),files={},headers={}, auth=HTTPBasicAuth(userName, password))
        r.raise_for_status()
    except requests.RequestException as e:
        print(e)

def findFile(file):
    for root, dirs, files in os.walk(file):
        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list

        # 遍历文件
        for f in files:
            if(f.endswith('.pom') or f.endswith('.jar')):
                filePath = os.path.join(root,f).replace('\\','/').replace(file+'/','')
                putFile(filePath,file,repoUrl)
#if __name__=='__main__':            
    #findFile(repoPath)
    
    
exitFlag = 0
 
class myThread (threading.Thread):
    def __init__(self, threadID, name, q):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.q = q
    def run(self):
        print("Starting " + self.name)
        process_data(self.name, self.q)
        print("Exiting " + self.name)
 
def process_data(threadName, q):
    while not exitFlag:
        queueLock.acquire()
        if not workQueue.empty():
            data = q.get()
            queueLock.release()
            putFile(data,repoPath,repoUrl)
            print("%s processing %s" % (threadName, data))
        else:
            queueLock.release()
 
queueLock = threading.Lock()
workQueue = Queue.Queue(0)
threads = []
threadID = 1
 
# 创建新线程
for tName in range(threadNum):
    thread = myThread(threadID, "Thread-%d" % tName, workQueue)
    thread.start()
    threads.append(thread)
    threadID += 1
 
# 填充队列
queueLock.acquire()
for root, dirs, files in os.walk(repoPath):
    for f in files:
        if(f.endswith('.pom') or f.endswith('.jar')):
            filePath = os.path.join(root,f).replace('\\','/').replace(repoPath+'/','')
            workQueue.put(filePath)
queueLock.release()
 
# 等待队列清空
while not workQueue.empty():
    pass
 
# 通知线程是时候退出
exitFlag = 1
 
# 等待所有线程完成
for t in threads:
    t.join()
print("Exiting Main Thread") 

本文原创,禁止转载。http://www.gjxblog.com/2021/01/04/1229/

作者: GJX

广告位
联系我们

联系我们

邮箱: gjx.xin@qq.com

说明:投稿请邮箱联系我,可获得5-50的酬劳。
关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部