a-cloud-all/.devops/scripts/maven.py

82 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Maven 打包和复制模块
"""
import os
import subprocess
import glob
from pathlib import Path
from .log import Logger
def run_maven(work_dir, maven_commands, source_path, target_dir):
"""
执行 Maven 打包和复制
参数:
work_dir: 执行 maven 命令的目录
maven_commands: 执行的命令(字符串)
source_path: 复制的源路径(支持通配符)
target_dir: 复制的目标目录
返回:
bool: 成功返回 True失败返回 False
"""
try:
# 转换为绝对路径
work_dir = Path(work_dir).resolve()
Logger.separator()
Logger.info("开始 Maven 打包")
Logger.separator()
Logger.info(f"执行目录: {work_dir}")
Logger.info(f"Maven 命令: {maven_commands}")
# 检查目录是否存在
if not work_dir.exists():
Logger.error(f"目录不存在: {work_dir}")
return False
# 执行 Maven 命令
if not Logger.run_command(maven_commands, work_dir):
Logger.error("Maven 打包失败")
return False
Logger.info("Maven 打包成功")
# 复制构建产物
Logger.separator()
Logger.info("开始复制构建产物")
Logger.separator()
source_full_path = work_dir / source_path
Logger.info(f"源路径: {source_full_path}")
Logger.info(f"目标目录: {target_dir}")
# 创建目标目录
Path(target_dir).mkdir(parents=True, exist_ok=True)
# 复制文件
files = glob.glob(str(source_full_path))
if not files:
Logger.error(f"未找到构建产物: {source_full_path}")
return False
for file in files:
file_path = Path(file)
dest = Path(target_dir) / file_path.name
Logger.info(f"复制文件: {file_path.name}")
import shutil
shutil.copy2(file, dest)
Logger.info("构建产物复制成功")
Logger.info("Maven 打包和复制完成")
return True
except Exception as e:
Logger.error(f"Maven 打包异常: {e}")
return False