Shell脚本一键mvn发布war包

脚本初衷

客户想减少手动发布过程,减少部分工作量。但是客户不会使用jenkins,想直接脚本执行然后发布。

脚本发布步骤
  • 登录github,拉取指定项目到本地
  • 构建war包
  • 停止tomcat服务
  • 替换war包
  • 启动tomcat服务
详细脚本
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
195
#!/bin/bash
# author:key.liyk
# version: v1.0
# func: auto build war package

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ $(id -u) != "0" ] && echo "You need use root to run this script" && exit 1

clear
cat <<EOF
$(printf %.s# {1..44})
# _ _ #
# __ _ _ __ ___| |__ _ __ ___| |_ #
# / _\ | '_ \ / __| '_ \| '_ \ / _ \ __| #
# | (_| | | | | (__| | | | | | | __/ |_ #
# \__,_|_| |_|\___|_| |_|_| |_|\___|\__| #
# #
# The script for maven deploy! #
$(printf %.s+ {1..44})
EOF
cat <<EOF
1: Maven deploy
2: EXIT (or ctrl+c)
EOF

# 定义相关目录
git_dir=/data/softapps #定义git拉取存放目录
tomcat_dir=/data/tomcat9 #定义tomcat目录
git_url="https://github.com/liyk1024/wartest.git" #定义自己github项目clone路径
git_name=wartest #github项目名
remove_dir=/tmp/remove-war #移除后war目录
log_file=/tmp/install.log #定义日志目录
mvn_path=/data/maven3/bin

# 定义日志显示信息
do_log() {
local color=$1
local level=$2
shift # shift位置参数偏移
shift
local msg=$@
local prefix=""

if [[ $color -gt 30 ]]; then
prefix="\033[1;$color;40m[$level]\033[0m"
else
prefix="[$level]"
fi
echo -e "$prefix [`date +'%Y-%m-%d %H:%M:%S'`] $msg"
}
log_info() {
local msg=$@
do_log 0 INFO $msg
}
log_warn() {
local msg=$@
do_log 33 WARN $msg
}
log_error() {
local msg=$@
do_log 31 ERROR $msg
}
log_success() {
local msg=$@
do_log 32 SUCCESS $msg
}

# 检查目录
check_dir() {
log_info check directory...
for dir in $*;do
[ ! -d $dir ] && mkdir -p $dir &>> $log_file
done
[ $? -eq 0 ] && log_success check over
}

# 检查基础命令
check_command() {
log_info check basic commands...
for cmd in $*;do
hash $cmd &>> $log_file
if [ $? -ne 0 ];then
action "${cmd} check" /bin/false
log_info ${cmd} not find,installing...
yum -y install $cmd &>> $log_file
else
action "${cmd} check" /bin/true
fi
done
[ $? -eq 0 ] && log_success check over
}

# 检查java
check_java() {
log_info check java...
hash java &>> $log_file
if [ $? -eq 0 ];then
action "java check" /bin/true
else
action "java check" /bin/false
log_error Pls install java!
exit 1
fi
}

# 检查maven
check_mvn() {
log_info check maven...
hash mvn &>> $log_file
if [ $? -ne 0 ];then
action "maven check" /bin/true
else
action "maven check" /bin/false
log_error Pls install maven!
exit 1
fi
}

# 拉取代码
git_pull() {
log_info check git project...
find ${git_dir} -type d -name ${git_name} |xargs rm -rvf &>> $log_file
log_info git clone...
cd ${git_dir}
git clone $git_url &>> $log_file
[ $? -eq 0 ] && log_success git pull over!
}

# 构建war包
build() {
log_info begin build warPackage...
cd ${git_dir}/${git_name}
if [ -f pom.xml ];then
${mvn_path}/mvn clean package -Dmaven.test.skip=true &>> $log_file
war=`find ${git_dir}/${git_name} -name "*.war" |wc -l`
if [ $war -eq 1 ];then
log_success build war success!
else
log_error build war fail!
exit 1
fi
else
log_warn Pls check,not find pom.xml!
exit 1
fi
}

# 停止tomcat
stop_tomcat() {
sh ${tomcat_dir}/bin/shutdown.sh &>> $log_file
[ $? -eq 0 ] && log_success stop_tomcat over!
}

# 替换war包
remove_war() {
cd ${tomcat_dir}/webapps
if [ -d ROOT ];then
rm -rf ROOT
fi
war_url=`find ${git_dir}/${git_name} -name "*.war"`
cp ${war_url} ${tomcat_dir}/webapps/ROOT.war
}

# 启动tomcat
start_tomcat() {
sh ${tomcat_dir}/bin/startup.sh &>> $log_file
[ $? -eq 0 ] && log_success startup_tomcat over!
}

main() {
check_dir $git_dir $remove_dir
check_command git wget
check_java
check_mvn
git_pull
build
stop_tomcat
remove_war
start_tomcat
}

# 根据选择操作
read -p "Please input your choice:" choice
if [ $choice = 1 ];then
echo ""
echo -e "You can view detail use: \033[5;33mtail -f ${log_file}\033[0m"
log_info Start Maven deploy...
main
elif [[ $choice = 2 ]];then
log_info Your choice Exit!
exit 0
else
log_warn pls choice 1|2
exit 1
fi
-------------本文结束感谢您的阅读-------------
原创技术分享,感谢您的支持。