直接上码:

将这个脚本,放到项目的根目录即可.

#!/bin/bash

# 使用方式:

# 部署生产环境
# 完全部署: ./server.sh p
# 只部署静态文件: ./server.sh p static
# 只部署后端文件: ./server.sh p class


# 部署beta环境
# 完全部署 ./server.sh pre
# 只部署后端文件: ./server.sh pre class

YYYYYY_REMOTE_IP="xxx.xxx.xx.xx"
BETA_REMOTE_IP="yyy.yyy.yyy.yyy"

V2_ROOT=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
SCP_PORT=1850

YYYYYY_PRODUCT_APPJS="var current_lang = 'zh-cn';\n
var GOLBAL_CONFIG = {\n
\t//	'wbapi': 'http://localhost',\n
\t'wbapi': 'http://wbapi.xxxxxxx.com',\n
\t'wxapi': 'http://wxapi.xxxxxxx.com',\n
\t'luceneapi': 'http://lucenesdk.xxxxxxx.com',\n
\t'wbappid': 'u.xxxxxxx.com'\n
};\n
"

BETA_PRODUCT_APPJS="var current_lang = 'zh-cn';\n
var GOLBAL_CONFIG = {\n
\t//	'wbapi': 'http://localhost',\n
\t'wbapi': 'http://wbapi.yyyyyy.com',\n
\t'wxapi': 'http://wxapi.yyyyyy.com',\n
\t'luceneapi': 'http://lucenesdk.yyyyyy.com',\n
\t'wbappid': 'beta.yyyyyy.com'\n
};\n
"


NOW="$(date +'%Y_%m_%d_%H_%M_%S')"

echo "第一个参数为:->$1<-"
echo "第二个参数为:->$2<-"

if [ "$1" == "p" ]; then
   echo "部署生产环境"
   ENV="product"
   REMOTE_IP=${YYYYYY_REMOTE_IP}
   PRODUCT_APPJS=${YYYYYY_PRODUCT_APPJS}
else
   echo "部署beta环境"
   ENV="pre-release"
   REMOTE_IP=${BETA_REMOTE_IP}
   PRODUCT_APPJS=${BETA_PRODUCT_APPJS}	
fi

# 这个比较好,增量更新
function deployV2UseRsync(){
    echo "部署的环境是" ${ENV} "IP为" ${REMOTE_IP}
    mvn clean package -P${ENV} -Dmaven.test.skip=true > /dev/null && bundleRes $1 $2 && echo "执行前端脚本成功" &&  cd ${V2_ROOT}/target/ROOT/ &&  echo "In v2 ROOT" 


    #完全同步 ROOT下面的目录,但除了 res/upload 和 res/lottery 目录,因为要保留服务器上传的文件,这个功能要谨慎处理
    ssh -p ${SCP_PORT} yourusername@${REMOTE_IP} "tar -czf /home/yourusername/tomcat/apache-tomcat-7.0.27/webapps/ROOT-${NOW}.tar.gz /home/yourusername/tomcat/apache-tomcat-7.0.27/webapps/ROOT"
    echo "备份v2资源成功"

    if [ "$2" == "static" ];  then
        echo "部署静态文件"
    	deployStaticFile
    elif [ "$2" == "class" ];  then
	echo "部署Java文件"
	deployJavaClassFile $1 $2
    else
        echo "部署Java文件和静态文件"
	deployJavaClassFile $1 $2
	deployStaticFile $1 $2
    fi



    if [ "$1" != "p" ] && [ "$2" != "static" ]; then
	echo "要重启beta服务器"
	ssh -p ${SCP_PORT} yourusername@${REMOTE_IP} "/home/yourusername/yzy/restartV2.sh"
	echo "重启beta服务器成功"
    fi

}
# 进行前端处理
function bundleRes(){
	echo "处理前端公共处理"
	gulp srcToDist
	gulp release
        if [ "$1" == "p" ]; then
		echo "前端处理生产环境的部署"
		gulp mainrev configrev --production
        else
		echo "前端处理beta环境的部署"
		gulp mainrev configrev
        fi

}

# 同步静态资源文件
function deployStaticFile(){
   cd ${V2_ROOT}/target/ROOT/res/
   rsync -azh --delete --exclude="upload/" --exclude="lottery/" --exclude="${V2_ROOT}/target/ROOT/res/upload/" --exclude="${V2_ROOT}/target/ROOT/res/lottery/" "-e ssh -p ${SCP_PORT}" ${V2_ROOT}/target/ROOT/res/ yourusername@${REMOTE_IP}:/home/yourusername/tomcat/apache-tomcat-7.0.27/webapps/ROOT/res/
   rsync -azh --delete "-e ssh -p ${SCP_PORT}" ${V2_ROOT}/target/ROOT/index.html yourusername@${REMOTE_IP}:/home/yourusername/tomcat/apache-tomcat-7.0.27/webapps/ROOT/index.html
   rsync -azh --delete "-e ssh -p ${SCP_PORT}" ${V2_ROOT}/target/ROOT/WEB-INF/velocity/ yourusername@${REMOTE_IP}:/home/yourusername/tomcat/apache-tomcat-7.0.27/webapps/ROOT/WEB-INF/velocity/	
   echo "同步静态资源文件成功"
}

# 同步与后端java程序相关文件
function deployJavaClassFile(){
	deleteUnusedJar
	rsync -azh --delete "-e ssh -p ${SCP_PORT}" ${V2_ROOT}/target/ROOT/WEB-INF/ yourusername@${REMOTE_IP}:/home/yourusername/tomcat/apache-tomcat-7.0.27/webapps/ROOT/WEB-INF/	
	echo "同步后端资源WEB-INF目录文件成功"
}

# 删除无用的jar包
function deleteUnusedJar(){
	rm ${V2_ROOT}/target/ROOT/WEB-INF/lib/javax.servlet-api-3.0.1.jar
	echo "删除无用的jar包成功"
}

#旧的前端处理应该是要废弃了 ~~

function oldBundleRes(){
    MAIN_JS=`cat res/dist/rev-manifest.json | jq '.["main.js"]' | head -c -2 | tail -c +2`
    echo "前端打包后的main js为 " ${MAIN_JS}
    sed '1,9d' res/dist/${MAIN_JS} > res/dist/${MAIN_JS}.tmp.js && mv res/dist/${MAIN_JS}.tmp.js res/dist/${MAIN_JS}
    echo -e ${PRODUCT_APPJS} | cat - res/dist/${MAIN_JS} > res/dist/${MAIN_JS}.js.temp && mv res/dist/${MAIN_JS}.js.temp res/dist/${MAIN_JS}

    sed '1,9d' res/js/newform/config.js > res/js/newform/config.tmp.js && mv res/js/newform/config.tmp.js res/js/newform/config.js
    echo -e ${PRODUCT_APPJS} | cat - res/js/newform/config.js > res/js/newform/config.js.temp && mv res/js/newform/config.js.temp res/js/newform/config.js
}

deployV2UseRsync $1 $2

restartV2.sh 文件内容

#!/bin/bash

## kill 

ps aux | grep tomcat名 | grep "/home" | awk '{ print $2}' | xargs kill -9

## start 

/home/yourusername/tomcat/apache-tomcat-7.0.27/bin/startup.sh && tail -f -n 200 /home/yourusername/tomcat/apache-tomcat-7.0.27/logs/catalina.out