Files
AuthCenterConsoleApp/bash/build_jar.sh
2026-01-17 19:43:20 +08:00

104 lines
3.3 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

#!/bin/bash
# 查找项目根目录向上找含src文件夹的目录
find_project_root() {
# 修复:命令执行用$(),变量引用加$
local current_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
# 修复循环条件找src目录找不到就向上退到根目录停止
while [ ! -d "${current_dir}/src" ] && [ "${current_dir}" != "/" ]; do
current_dir=$(dirname "${current_dir}")
done
if [ "${current_dir}" = "/" ] && [ ! -d "${current_dir}/src" ]; then
echo "❌ 未找到项目根目录无src文件夹"
exit 1
fi
echo "${current_dir}"
}
# 基础目录定义(修复变量引用)
BASE_DIR=$(find_project_root)
SRC_DIR="${BASE_DIR}/src"
LIB_DIR="${BASE_DIR}/libs"
OUT_DIR="${BASE_DIR}/runtime"
LOG_DIR="${BASE_DIR}/logs"
JAR_DIR="${BASE_DIR}/jar"
MAIN_CLASS="Main" # 修复:补全全类名(必须包名+类名,否则运行报错)
JAR_NAME="AuthCenter-$(date +%Y%m%d%H%M).jar"
MANIFEST_FILE="${OUT_DIR}/MANIFEST.MF"
# 校验src目录
if [ ! -d "${SRC_DIR}" ]; then
echo "❌ 项目根目录[${BASE_DIR}]下无src目录请核对结构"
exit 1
fi
# 查找所有java源文件修复命令执行+判空)
JAVA_FILES=$(find "${SRC_DIR}" -name "*.java")
if [ -z "${JAVA_FILES}" ]; then
echo "❌ src目录下无.java源文件"
exit 1
fi
# 创建目录(修复:引号统一,避免空格问题)
mkdir -p "${OUT_DIR}" "${LOG_DIR}" "${JAR_DIR}"
# 构建classpath修复依赖查找+拼接逻辑)
CP_PARAM=""
LIB_CP=""
if [ -d "${LIB_DIR}" ]; then
# 修复:加*匹配所有jar包
JAR_LIST=(${LIB_DIR}/*.jar)
# 修复判断是否有jar包
if [ -f "${JAR_LIST[0]}" ]; then
# 修复:依赖路径拼接
LIB_CP=$(IFS=:; echo "${JAR_LIST[*]}")
CP="${OUT_DIR}:${LIB_CP}"
CP_PARAM="-cp ${CP}"
else
echo " libs目录无jar包无依赖编译"
CP_PARAM="-cp ${OUT_DIR}"
fi
else
echo " 无libs目录无依赖编译"
CP_PARAM="-cp ${OUT_DIR}"
fi
# 打印目录信息(修复:源码数统计命令执行)
echo "📌 项目目录信息"
echo " 根目录 ${BASE_DIR}"
echo " 源码数 $(echo "${JAVA_FILES}" | wc -l | tr -d ' ')"
echo " 输出目录:${OUT_DIR}"
echo " JAR输出 ${JAR_DIR}/${JAR_NAME}"
echo "------------------------------------------------------------------------"
# 编译源码(修复:变量引用+参数格式)
echo "开始编译Termux+Java7兼容..."
javac -encoding UTF-8 -source 1.7 -target 1.7 ${CP_PARAM} -d "${OUT_DIR}" ${JAVA_FILES}
if [ $? -ne 0 ]; then
echo -e "\n❌ 编译失败排查JDK版本、源码语法、libs依赖包"
exit 1
fi
echo -e "\n✅ 编译成功!"
# 生成MANIFEST清单修复变量引用+路径处理)
echo "📝 生成MANIFEST清单文件..."
echo "Manifest-Version: 1.0" > ${MANIFEST_FILE}
echo "Main-Class: ${MAIN_CLASS}" >> ${MANIFEST_FILE}
if [ -n "${LIB_CP}" ]; then
# 修复依赖路径格式化适配JAR清单
echo "Class-Path: $(echo ${LIB_CP} | sed "s|${BASE_DIR}/||g" | tr ':' ' ')" >> ${MANIFEST_FILE}
fi
# 打包可运行JAR修复变量引用+静默打包)
echo "📦 打包可运行JAR文件..."
cd ${OUT_DIR} && jar cvfm "${JAR_DIR}/${JAR_NAME}" "${MANIFEST_FILE}" ./* >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\n❌ JAR打包失败"
exit 1
fi
echo -e "✅ JAR打包成功路径${JAR_DIR}/${JAR_NAME}"