赞
踩
#!/bin/bash # ------------------------------ environment variable # define script file name. When delete ./ files, don't delete self. MY_NAME=middleware.sh # 分支 branch_name=test # define the Tomcat param TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-9.0.69 TOMCAT_PORT=8080 # project name, also is default context. PROJandCONTEXT_NAME=middleware # max try times. It's second. max_trial_time=100 sleep_time=7 #GIT_DIR GIT_DIR=/usr/local/tomcat/ # 启用下划线效果 OPEN_ULINE=\\033[4m CLOSE_ULINE=\\033[24m #拉取最新代码 echo " 拉取最新代码 " cd /usr/local/tomcat/middleware git pull sleep 5 git checkout -b $branch_name origin/$branch_name sleep 5 git pull echo " [1] execute maven package. Command:mvn -U clean compile package -Ptest" mvn -U clean compile package -Ptest echo -e "\n$OPEN_ULINE [3] delete old package and deploy new package $CLOSE_ULINE" echo " [2] delete all file or dir in $TOMCAT_HOME/webapps start with $PROJandCONTEXT_NAME." TOMCAT_PID_INFO=`ps -ef | grep $TOMCAT_NAME` PID_LIST=`ps -ef | grep $TOMCAT_NAME | grep -v "grep" | awk '{print $2}'` # ------------------------------ environment variable end --------------------------------- # ------------------------------ pre-check echo -e "\n$OPEN_ULINE [3] pre-check(前置校验) $CLOSE_ULINE" echo " TOMCAT_HOME: $TOMCAT_HOME " echo " MY_NAME: $MY_NAME " echo " web context name: $PROJandCONTEXT_NAME " # check directory is exist. if [ -d $TOMCAT_HOME ]; then echo -e " [ OK ] Tomcat directory check " else echo -e " [ FAIL ] Tomcat directory check " exit 0 fi # check Maven $(mvn --version >/dev/null 2>&1) echo -e "check maven exist." hasMaven=$? if [ "$hasMaven" == "127" ]; then echo " [ERROR] maven is not exists!quit!" exit 0 else echo " [ OK ] you have install maven. continue..." fi # ------------------------------ pre-check end --------------------------------- # ------------------------------ function define function stopTomcatService () { # shutdown tomcat # use command netstat to find whether the port is closed. for (( i=$sleep_time; i<$max_trial_time; i=i+$sleep_time )) do if [ "$(ps -ef | grep apache-tomcat-9.0.63 | grep -v "grep" | awk '{print $2}')" == "" ]; then echo " Tomcat service not started or has been closed normally." break else kill -9 $(ps -ef | grep apache-tomcat-9.0.63 | grep -v "grep" | awk '{print $2}') fi sleep $sleep_time done } # ------------------------------ function define end --------------------------------- echo "[4] Pre-check" echo " Tomcat Home: $TOMCAT_HOME " echo " Tomcat Name: $TOMCAT_NAME " echo " TOMCAT_PID_INFO: ps -ef | grep $TOMCAT_NAME" echo " TOMCAT_PID_INFO: $TOMCAT_PID_INFO" echo " Tomcat PID_LIST: $PID_LIST" echo "[5] Shutdown Tomcat normally." if [ "$PID_LIST" != "" ]; then echo " [5.1] Shut down the tomcat server normally..." $TOMCAT_HOME/bin/shutdown.sh >/dev/null 2>&1 echo " [5.2] The normal shutdown program ends. Sleep for 5 seconds..." sleep 5 fi echo -e "\n$OPEN_ULINE [4] stop tomcat service $CLOSE_ULINE" stopTomcatService echo "tomcat 已停止服务" sleep 10 rm -rf $TOMCAT_HOME/webapps/$PROJandCONTEXT_NAME rm -rf $TOMCAT_HOME/webapps/$PROJandCONTEXT_NAME.war echo " [6] 删除旧包" sleep 30 find $TOMCAT_HOME/webapps/ -maxdepth 1 -name $PROJandCONTEXT_NAME* -exec rm -r {} \; echo " [7] copy package to tomcat webapps." tmp_dir=$GIT_DIR/middleware/target/$PROJandCONTEXT_NAME.war cp $GIT_DIR/middleware/target/$PROJandCONTEXT_NAME.war $TOMCAT_HOME/webapps/ # cp -R ./target/$PROJandCONTEXT_NAME $TOMCAT_HOME/webapps/ echo -e "\n$OPEN_ULINE [6] start tomcat $CLOSE_ULINE" $TOMCAT_HOME/bin/startup.sh echo " ========================================== " echo " *** mes-data-middleware SUCCESS *** " echo " ========================================== " sleep 10 echo "tail -f $TOMCAT_HOME/logs/catalina.out" # tail -f $TOMCAT_HOME/logs/catalina.out
Springboot项目的入口类长这样:(这样的是以jar包的形式启动)
@SpringBootApplication
public class MiddlewareApplication {
public static void main(String[] args) {
SpringApplication.run(MiddlewareApplication.class, args);
}
}
war包的需要改成这样:(继承SpringBootServletInitializer类,并重写configure方法)
@SpringBootApplication
public class MiddlewareApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MiddlewareApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MiddlewareApplication.class);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。