通过shell脚本在服务器上一键构建java应用并重新部署

1. 把代码从github等托管服务器上更新下来

2. 编译成war文件

3. 重启tomcat

第一次使用时,先手动地把代码拉到一个固定的目录中

cd /home/me/myappsite/code
git clone https://someuser@somesite/myapp.git

一键脚本:build-my.sh

#!/bin/sh
echo "pull and build"
cd /home/me/myappsite/code/myapp
git pull
mvn clean
mvn package -DskipTests

echo "shutting down the tomcat"
kill $(ps aux | grep 'java' | grep 'mytomcat'| awk '{print $2}')  #不要用shutdown.sh;这里需要同步杀死进程,shutdown.sh做不到同步杀

echo "clean existing war files and copy the new one"
rm -rf /home/me/mytomcat/webapps/ROOT  
cp /home/me/myappsite/code/myapp/target/myapp-x.x.war /home/me/myappsite/war/myapp.war   #tomcat的server.xml里请把root context path指向这个文件


echo "now start tomcat"
/home/me/mytomcat/bin/startup.sh

echo "get back"
cd /home/me




Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.