forked from CatalpaFlat/build-maven-project-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
194 lines (159 loc) · 5.5 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# writen by CatalpaFlat at 2019.05.30
branch=''
begin_hash=''
end_hash=''
help=''
# help
print_help() {
cat <<EOF
use ${program}
--branch=<git branch> - 构建 git branch (default:当前分支)
--begin_hash=<git commit hash> - 欲构建git起始commit hash值 (default:当前分支的最近hash)
--end_hash=<git commit hash> - 希望构建的commit的hash值 (default:当前分支的最近hash的上一次commit)
--help - prints help screen
EOF
}
# 传参
parse_arguments() {
local helperKey="";
local helperValue="";
local current="";
while [[ "$1" != "" ]]; do
current=$1;
helperKey=${current#*--};
helperKey=${helperKey%%=*};
helperKey=$(echo "$helperKey" | tr '-' '_');
helperValue=${current#*=};
if [[ "$helperValue" == "$current" ]]; then
helperValue="1";
fi
eval ${helperKey}=${helperValue};
shift
done
}
parse_arguments ${@};
if [[ "$help" == "1" ]]; then
print_help;
exit 1;
fi
ECHO Initstating Build Version Differences ......
ECHO Initstating Git Info ......
# git环境
if [[ ! -d ".git" ]]; then
ECHO error: please init Git Repository
exit 1;
fi
if [[ ! -z ${branch} ]]; then
git checkout ${branch}
fi
# 获取默认commit-hash
if [[ -z "$begin_hash" ]] && [[ -z "$end_hash" ]] ; then
for p in $(git log --pretty=oneline -2) ; do
if [[ ${#p} -eq 40 ]]; then
if [[ -z ${begin_hash} ]]; then
begin_hash=${p}
else
end_hash=${p}
break
fi
fi
done
fi
is_begin_has=false
# 是否当前最新commit
if [[ $(git log --pretty=oneline -1) == *${begin_hash}* ]]; then
is_begin_has=true
fi
# 非当前最新分支commit,回滚到原始版本,可能当时maven原始配置不支持compile或会出现构建失败(如:使用本地仓/私有仓库等)
if [[ ${is_begin_has} = false ]]; then
project_path=$(pwd)
project_name=${project_path##*/}
cd ..
build_project_name=${project_name}_build_temp_project
if [[ ! -d ${build_project_name} ]]; then
mkdir ${build_project_name}
fi
\cp -rf ${project_name}/. ${build_project_name}
cd ${build_project_name}
git reset --hard ${begin_hash}
fi
ECHO Build Maven ......
mvn clean compile -q -DskipTest
ECHO Initstating to transport ......
# 创建增量部署文件夹
build_path=build-path/
current_date=`date +%Y%m%d`
if [[ ! -d "$build_path$current_date" ]]; then
mkdir -p ${build_path}${current_date}
else
rm -rf ${build_path}${current_date}
mkdir -p ${build_path}${current_date}
fi
default_target_paths=()
default_java_file=java
module_index=0
# 检索当前项目是否maven多模块开发,递归检索,并设置其编译后的代码位置(暂只提供了java类型)
obtain_module(){
for module in ` cat ./pom.xml | grep '<module>' | awk -F '>' '{print $2}' | awk -F '<' '{print $1}' `
do
cd ${module}
if [[ ! -d "/pom.xml" ]]; then
module_exist=`cat ./pom.xml | grep '<module>' | awk -F '>' '{print $2}' | awk -F '<' '{print $1}'`
if [[ -z ${module_exist} ]]; then
if [[ ! -d "/target" ]]; then
if [[ -z $1 ]]; then
default_target_paths[module_index]=${module}/target/classes
else
default_target_paths[module_index]=$1/${module}/target/classes
fi
((module_index++))
fi
else
if [[ -z $1 ]]; then
obtain_module ${module}
else
obtain_module $1/${module}
fi
fi
fi
cd ..
done
}
obtain_module
# 通过git diff --name-only实现两次commit之间文件差异,并且将begin_hash的代码进行编译后,将差异的文件拷贝到“增量文件夹”中,以备后续进行增量部署
for file_path in $(git diff --name-only ${begin_hash} ${end_hash}) ; do
package_path=${file_path%/*}
file_name=${file_path##*/}
file_type=${file_name##*.}
if [[ ${package_path} != *.* ]]; then
if [[ ! -d "./${build_path}${current_date}/$package_path" ]] ; then
mkdir -p ./${build_path}${current_date}/${package_path}
fi
fi
#
if [[ ${file_type} = ${default_java_file} ]]; then
module_path=${package_path##*java}
file_class_name=${file_name%.*}
module_type=${package_path%%/*}
for default_target_path in ${default_target_paths[@]}; do
target_module_path=$(echo ${default_target_path} | awk -F '/target/' '{print $1}')
file_target_module_path=$(echo ${package_path} | awk -F '/src/' '{print $1}')
file_target_package_path=$(echo ${package_path} | awk -F '/src/main/java/' '{print $2}')
default_module_type=${default_target_path%%/*}
if [[ ${target_module_path} = ${file_target_module_path} ]]; then
cp -afx ${default_target_path}/${file_target_package_path}/${file_class_name}* ./${build_path}${current_date}/${package_path}
fi
done
else
if [[ ${package_path} != *.* ]]; then
if [[ ! -d "./${build_path}${current_date}/$package_path" ]] ; then
mkdir -p ./${build_path}${current_date}/${package_path}
fi
else
package_path=${package_path%/*}
fi
cp -afx ${file_path} ./${build_path}${current_date}/${package_path}
fi
done
ECHO DONE