背景
java依赖包工程每次打包都需要半小时左右,但每次开发只修改里面的几个小模块,需要实现只编译变更模块的功能,来提升编译的效率。
设计思路
根据git pull的输出来获取变更文件的绝对路径,循环遍历找到有pom.xml的目录,并执行mvn deploy
流程图
shell脚本
#!/bin/bash
<<INFO
AUTHOR:运维@小兵
DATE:2022-04-30
DESCRIBE:mvn根据git变更文件编译子模块
SYSTEM:CentOS 7.6.1810
WARNING:警告信息
MODIFY:
INFO
set -e
[[ $# -ne 1 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 java工程路径" && exit 1
PROJECT_DIR=$1
Check_Env() {
[[ ! -d ${PROJECT_DIR} ]] && echo "ERROR:${PROJECT_DIR} Not Found" && exit 1
if ! command -v mvn &> /dev/null;then
echo "ERROR:mvn Command Not Found" && exit 1
fi
}
Mvn_Child_Module() {
local path=""
cd ${PROJECT_DIR}
if [[ -f git_pull.log ]];then
git pull &> git_pull.log
sed -n '/^Fast-forward/,$p' git_pull.log | egrep '^ (create|delete)' | awk '{print $4}' | uniq > tmp.txt
sed -n '/^Fast-forward/,$p' git_pull.log | grep '^ rename' | awk -F'[ {]' '{print $3}' | uniq >> tmp.txt
sed -n '/^Fast-forward/,$p' git_pull.log | egrep '[\+-]+$' | awk '{print $1}' | uniq >> tmp.txt
sort -rn tmp.txt | uniq > update_file.txt
rm -f tmp.txt
if [[ -s update_file.txt ]];then
for path in $(cat update_file.txt)
do
src_path=${path}
path=$(echo ${path%/*})
while [ ! -f ${PROJECT_DIR}/${path}/pom.xml ]
do
path=$(echo ${path%/*})
if ! echo "${path}" | grep '/' &> /dev/null;then
break
fi
done
cd ${PROJECT_DIR}/${path}
if [[ -f pom.xml ]];then
if [[ ! -f mvn_flag ]];then
mvn -q clean deploy -Dmaven.test.skip=true
touch mvn_flag
else
echo "INFO:${path} Already mvn"
fi
sed -i "1d" ${PROJECT_DIR}/update_file.txt
else
echo "WARN:${path}/pom.xml Not Exist"
fi
done
find ${PROJECT_DIR}/ -name 'mvn_flag' -type f | xargs rm -f
echo "INFO:mvn ${PROJECT_DIR} Success"
else
echo "INFO:${PROJECT_DIR} Not Update File"
fi
else
mvn -q clean deploy -Dmaven.test.skip=true
touch git_pull.log
echo "INFO:mvn ${PROJECT_DIR} Success"
fi
}
Check_Env
Mvn_Child_Module
|