Maven同步本地仓库到Nexus3

Posted by NekouTarou on 01-04,2024
项目现场是内网环境,需要将本地的Maven仓库中的jar同步到内网Nexus中,有两种方法。

方法一:使用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://xxx/nexus/repository/maven-releases/'
userName='admin'
password='xxxxx'
repoPath='C:\\Users\\NekouTarou\\Soft\\Apache\\Maven\\Tem'

threadNum=10

def putFile(filePath,homePath,url):
    path = 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+'/','')
            print("===================================================== " + filePath)
            workQueue.put(filePath)
queueLock.release()
 
# 等待队列清空
while not workQueue.empty():
    pass
 
# 通知线程是时候退出
exitFlag = 1
 
# 等待所有线程完成
for t in threads:
    t.join()
print("Exiting Main Thread") 

调整脚本中的仓库信息后,直接执行python脚本即可。参考nexus 批量导入本地库

方法二:使用sh脚本

在本地仓库根路径中创建脚本import.sh

#!/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}/{} ;

执行命令

 ./import.sh -u admin -p xxxxx -r http://xxxx/nexus/repository/maven-releases/