POM添加打包插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>  
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>zip</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<descriptors> <!--描述文件路径-->
<descriptor>描述文件路径/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>

添加描述文件

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
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ">
<id>full</id>
<!--这个id会出现在zip包名称的后面,zip的完整名是:pom.xml中的artifactId-version-id.zip -->
<formats>
<!--支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war-->
<format>zip</format>
</formats>

<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--Jar包-->
<fileSet>
<outputDirectory>${file.separator}</outputDirectory> <!--放在哪-->
<directory>target</directory><!--源目录-->
<includes>
<include>*.jar</include> <!--代码的jar包-->
</includes>
</fileSet>
<!--bin脚本-->
<fileSet>
<outputDirectory>${file.separator}bin</outputDirectory>
<directory>${project.basedir}/../../docs/build/bin/</directory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include> <!--把shell脚本打进去-->
</includes>
</fileSet>
<!--config目录-->
<fileSet>
<outputDirectory>${file.separator}config</outputDirectory>
<directory>${project.basedir}/../../docs/build/config/</directory>
<includes>
<include>**.properties</include>
</includes>
</fileSet>
<!--deploy说明目录-->
<fileSet>
<outputDirectory>${file.separator}deploy</outputDirectory>
<directory>${project.basedir}/../../docs/build/deploy/</directory>
<includes>
<include>**.md</include>
</includes>
</fileSet>
</fileSets>
</assembly>

启动脚本

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
196
197
198
199
200
201
202
203
204
#!/bin/bash

JDK_HOME="java"
VM_OPTS="-Xms2048m -Xmx2048m"
SPB_OPTS=""
APP_LOCATION=""
APP_NAME=""
PID=""
LOG_PATH=""
APP_HOME=""

# 初始化脚本所需信息
init() {
APP_HOME=$(cd `dirname $0`/../; pwd)
APP_NAME=$(basename "$(find "$APP_HOME" -name '*.jar' | head -1 |awk ' {print $NF}')")
APP_LOCATION=$(readlink -f "$APP_HOME/$APP_NAME")
LOG_PATH="$APP_HOME/logs/"
if [ ! -f "$APP_LOCATION" ];
then
echo "[Error] file not found $APP_LOCATION"
exit 0
fi
}
init

# 加载config目录下的startup.conf文件
loadProp(){
APP_HOME=$(cd `dirname $0`/../; pwd)
PROP_FILE="$APP_HOME/config/startup.properties"
if [ -f "$PROP_FILE" ];
then
SPB_OPTS=$(echo `xargs < $APP_HOME/config/startup.properties`|tr '\r' ' '|tr '\n' ' ')
echo "load properties success $SPB_OPTS"
fi
}
loadProp

confirm(){
info
read -n1 -p "Do you want to continue $1 [Y/N]? " answer
case $answer in
Y|y)
echo "start $1 ";;
N|n)
echo "cancel $1"
exit 0
;;
*)
echo "error choice";;
esac
}

# 获取PID
getPid(){
PID=$(ps -ef | grep "$APP_LOCATION" | grep -v grep | awk '{print $2}')
}


start() {
echo "=============================start=============================="
getPid
if [[ -n $PID ]]; then
echo "$APP_LOCATION is already running,PID is $PID"
else
loadProp
if [ ! -e "$LOG_PATH" ]; then
mkdir "$LOG_PATH"
fi
echo "Starting $APP_LOCATION SPB_OPTS[$SPB_OPTS] ..."
nohup $JDK_HOME -jar $SPB_OPTS $APP_LOCATION >$LOG_PATH/start.out 2>&1 &
getPid
if [[ -n $PID ]]; then
echo "Start $APP_LOCATION successfully,PID is $PID"
else
echo "Failed to start $APP_LOCATION !!!"
fi
fi
echo "=============================start=============================="
}

stop() {
echo "=============================stop=============================="
getPid
if [[ -n $PID ]]; then
kill -15 "$PID"
sleep 5
getPid
if [[ -n $PID ]]; then
echo "Stop $APP_LOCATION failed by kill -15 $PID,begin to kill -9 $PID"
kill -9 "$PID"
sleep 1
echo "Stop $APP_LOCATION successfully by kill -9 $PID"
else
echo "Stop $APP_LOCATION successfully by kill -15 $PID"
fi
else
echo "$APP_LOCATION is not running!!!"
fi
echo "=============================stop=============================="
}

restart() {
echo "=============================restart=============================="
stop
start
echo "=============================restart=============================="
}

deploy(){
echo "=============================deploy=============================="
# 检查当前运行状态
getPid
if [[ -n $PID ]]; then
echo "app is running, stopping app $APP_LOCATION"
stop
fi
# 检查待部署文件
DEPLOY_FILE="$APP_HOME/deploy/$APP_NAME"
if [ -f "$DEPLOY_FILE" ]; then
# 开始备份APP
echo "start to backup app $APP_LOCATION"
BACKUP_DIR="$APP_HOME/backup/$(date "+%Y/%m/%d/%H%M%S")/"
mkdir -p "$BACKUP_DIR"
mv "$APP_LOCATION" "$BACKUP_DIR"
echo "backup success , path : $BACKUP_DIR$APP_NAME"
# 开始移动待部署文件
mv "$DEPLOY_FILE" "$APP_LOCATION"
fi
# 启动应用
start
echo "=============================deploy=============================="
}

status() {
echo "=============================status=============================="
getPid
if [[ -n $PID ]]; then
echo "$APP_LOCATION is running,PID is $PID"
else
echo "$APP_LOCATION is not running!!!"
fi
echo "=============================status=============================="
}

log(){
tail -200f "$LOG_PATH/start.out"
}

info() {
echo "=============================info=============================="
echo "APP_LOCATION: $APP_LOCATION"
echo "APP_NAME: $APP_NAME"
echo "JDK_HOME: $JDK_HOME"
echo "VM_OPTS: $VM_OPTS"
echo "SPB_OPTS: $SPB_OPTS"
echo "LOG_PATH: $LOG_PATH"
echo "=============================info=============================="
}

help() {
echo "start: start server"
echo "stop: shutdown server"
echo "restart: restart server"
echo "status: display status of server"
echo "info: display info of server"
echo "help: help info"
echo "deploy: stop|backup|start server"
echo "log: show log"
}

case $1 in
start)
confirm "$1"
start
;;
stop)
confirm "$1"
stop
;;
restart)
confirm "$1"
restart
;;
status)
status
;;
info)
info
;;
help)
help
;;
deploy)
confirm "$1"
deploy
;;
log)
log
;;
*)
help
;;
esac
exit $?

步骤总结

  1. 引入assembly插件
  2. 指定assembly配置文件路径
  3. assembly中指定bin脚本与Jar包,使得maven package时将文件打入压缩包内
  4. 创建要使用的bin脚本
  5. 后续maven package 时将自动打zip包