#!/bin/bash
# program:
# 部署项目的脚本
# 2018-03-04 nanc
# 目录结构说明:
#
# nanc/
# -- deploy.sh 部署的脚本文件
# -- project_backup_war/ 备份项目的war目录
# -- project_deploy/ 项目的部署路径 例如:project_deploy/my_project
# -- project_source/ 项目源代码 git clone下来的项目
# -- tomcat_server/ tomcat server所在的目录
#
#
export JAVA_HOME=/usr/java/jdk1.8.0_111
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export M2_HOME=/usr/local/apache-maven-3.5.2
PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$M2_HOME/bin:$PATH
# 项目名称
if [ $# -lt 1 ];then
project_name=""
else
project_name=$1
fi
while [ "$project_name" = "" ]
do
read -p "请输入项目名称(输入exit结束):" project_name
if [ "$project_name" = "exit" ];then
exit 1
fi
done
current_date=$(date +%Y-%m-%d-%H:%M:%S)
current_path=`pwd`
project_path=$current_path/project_source/$project_name
deploy_path=$current_path/project_deploy
project_backup_war=$current_path/project_backup_war
while [ ! -d "$project_path" ] && [ "$project_name" != "exit" ]
do
read -p "项目($project_name)不存在,请重新输入所要部署的项目名称(输入exit结束):" project_name
project_path=$current_path/project_source/$project_name
done
echo "项目路径为:$project_path"
tomcat_home=$current_path/tomcat_server/$project_name
echo "tomcat路径为$tomcat_home"
echo "----------------开始部署项目 $project_name---------------------"
cd $project_path
git pull
mvn clean install -Dmaven.test.skip=true
cd target
# 结束tomcat
ps aux | grep -v grep | grep $project_name | awk '{print $2}' | xargs kill -9
# 删除已经部署了的项目
rm -rf $deploy_path/$project_name
mkdir -p $deploy_path/$project_name
cp $project_name".war" $deploy_path/$project_name
cp $project_name".war" $project_backup_war/$project_name"_"$current_date".war"
# 打开war包
cd $deploy_path/$project_name
jar xvf $project_name".war"
# 删除war包
rm -r $project_name".war"
# 开启服务
sh $tomcat_home/bin/startup.sh
网友评论