背景:外包类项目开发时是调用的公司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"):continueif file_name.startswith("."):continueif "-SNAPSHOT" in root:continueif "_remote.repositories" in file_name:continueif file_name.startswith("^archetype-catalog.xml") or file_name.startswith("^maven-metadata-local.xml") or file_name.startswith("^maven-metadata-deployment.xml"):continuefile_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)