用于部署war并重启Tomcat的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于部署war并重启Tomcat的脚本相关的知识,希望对你有一定的参考价值。

只需要定义两个变量, 一个是目标tomcat实例的目录, 另一个是war包的名称

# Please define the absolute path of tomcat instance
THIS_TC_INSTANCE=/home/tomcat/tomcat8_jdk8_1
THIS_APP_MODULE=throne-commons

df -h
echo ‘‘
pid=`ps -ef|grep ${THIS_TC_INSTANCE}|grep -v grep|grep java|awk {print $2}`
if [ -z $pid ] ; then
  echo Tomcat ${THIS_TC_INSTANCE} is not running
else
  echo Shutting down Tomcat at ${THIS_TC_INSTANCE}
  ${THIS_TC_INSTANCE}/bin/shutdown.sh
  sleep 5
fi

pid=`ps -ef|grep ${THIS_TC_INSTANCE}|grep -v grep|grep java|awk {print $2}`
if [ -z $pid ] ; then
  echo Tomcat ${THIS_TC_INSTANCE} has been shut down
else
  echo Tomcat ${THIS_TC_INSTANCE} didnot exit normally, now killing the PID:${pid}
  ps -ef|grep ${THIS_TC_INSTANCE}|grep -v grep|grep java|awk {print $2}|xargs kill -s 9
fi

echo ‘‘
echo Removing old deployments
rm -f ${THIS_TC_INSTANCE}/webapps/${THIS_APP_MODULE}.war
rm -rf ${THIS_TC_INSTANCE}/webapps/${THIS_APP_MODULE}
echo Copying new deployments
echo ‘‘
cp -f /home/tomcat/builds/${THIS_APP_MODULE}.war ${THIS_TC_INSTANCE}/webapps/
echo Startup tomcat
${THIS_TC_INSTANCE}/bin/startup.sh

对应的Jenkins Pipeline配置

node {
   // Mark the code checkout stage....
   stage Checkout

   sh "mkdir -p throne-commons-impl"
   dir(throne-commons-impl) {
     git branch: master, credentialsId: 859dde85-5e0c-4ab3-8fd1-d31c96d4cae4, url: [email protected]:yhc/throne-commons-impl.git
   }

   env.JAVA_HOME="${tool ‘jdk1.8.0_101‘}"
   sh $JAVA_HOME/bin/java -version
   def mvnHome = tool apache-maven-3.3.9

   stage Build
   dir(throne-commons-impl) {
     sh "${mvnHome}/bin/mvn -s /home/tomcat/files/settings.xml clean package -Dmaven.test.skip=true -Pthrone_qa -e"
   }

   stage Publish
   sshagent([594d69cf-fd5e-45ea-8ab0-c94db218017a]) {
      sh ssh -p 22 [email protected] \df -h\‘‘
      sh scp throne-commons-impl/target/throne-commons.war [email protected]:/home/tomcat/builds/
      sh ssh -p 22 [email protected] \/home/tomcat/scripts/deploy_t8_j8_1_commons.sh\‘‘
   }
}

 

以上是关于用于部署war并重启Tomcat的脚本的主要内容,如果未能解决你的问题,请参考以下文章

Jenkins自动打war包,并部署到tomcat服务器

项目如何部署在linux系统上

tomcat发布war包需要重启tomcat吗?

tomcat 以war包部署后如何增量更新代码

管理多tomcat实例的shell脚本

远程部署tomcat脚本