目的:自动将工程编译打包并发布到Tomcat服务器
使用软件:JDK7 + MAVEN + SVN + TOMCAT (请提前准备好以上软件)
步骤:
1)配置JDK环境(详细步骤可自己百度下,本人暂未准备,后续会更新... ...)
2)配置MAVEN环境(详细请参考本人的另一篇文章:Linux系统MAVEN环境搭建)
3)配置SVN服务器(详细请参考本人的另一篇文章:Linux系统SVN服务器搭建)
4)上传Tomcat服务器包并解压缩
本人的路径为: /usr/local/tomcat8
解压缩命令 : tar -xvf apache-tomcat-8.5.32.tar.gz 即会解压到当前目录中,如下图:
以上基础准备工作完成后 下面开始具体的操作 (两种发布方式,可凭自己爱好)
第一种:将工程war包直接发布到Tomcat服务器的webapps目录下
a)将Maven工程(注意:必须是Maven工程)代码检出到指定目录:
本人目录为: /usr/local/src/maven/ltest (可自己设定)
从SVN检出命令为: svn co svn项目地址(自己的SVN工程地址)
如:svn co svn://www.baidu.com/tst/ltest
b)将工程检出后 将下面脚本放入一个文本中 并修改为 publish.sh:并赋上可执行权限 chmod +x publish.sh
#!/bin/sh
#定义变量
date=$(date "+%Y%m%d")
datetime=$(date "+%Y%m%d%H%M%S")
echo ">>>>>>>>>>>>>>>>> 执行开始(${datetime}) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
context="ltest"
echo "[日志]项目名称:"${context} >>${context}.log
server_publish_path="usr/local/src/maven"
echo "[日志]工程目录:"${server_publish_path} >>${context}.log
server_tomcat_path="/usr/local/tomcat8/apache-tomcat-8.5.32"
echo "[日志]Tomcat服务器目录:"${server_tomcat_path} >>${context}.log
echo "[日志]进入工程所在目录:"/${server_publish_path}/${context} >>${context}.log
cd /${server_publish_path}/${context}
echo /${server_publish_path}/${context} >>${context}.log
echo "[日志]从SVN服务器提取最新代码 >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
svn up
echo svn up >>${context}.log
echo "[日志]使用MVN命令编译并生成发布包 >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
mvn -U clean install
echo -U clean install >>${context}.log
echo "[日志]stop Tomcat >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
${server_tomcat_path}/bin/catalina.sh stop
echo ${server_tomcat_path}/bin/catalina.sh stop >>${context}.log
echo "[日志]等待10秒钟 >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
sleep 10s
echo "[日志]将原有发布文件重命名 >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
mv /${server_tomcat_path}/webapps/${context} ${server_tomcat_path}/webapps/${context}${datetime}
echo mv /${server_tomcat_path}/webapps/${context} ${server_tomcat_path}/webapps/${context}${datetime} >>${context}.log
rm -rf ${server_tomcat_path}/webapps/${context}.war
echo -rf ${server_tomcat_path}/webapps/${context}.war >>${context}.log
cp /${server_publish_path}/${context}/target/framework-1.0.1.war ${server_tomcat_path}/webapps/${context}.war
echo cp /${server_publish_path}/${context}/target/framework-1.0.1.war ${server_tomcat_path}/webapps/${context}.war >>${context}.log
echo "[日志]start tomcat >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
${server_tomcat_path}/bin/catalina.sh start
echo ${server_tomcat_path}/bin/catalina.sh start >>${context}.log
echo "[日志]查看启动日志 >>>>>>>>>>>>>>>>>>>>>>>>" >>${context}.log
tail -f /${server_publish_path}/logs/catalina.out
echo tail -f /${server_publish_path}/logs/catalina.out >>${context}.log
echo ">>>>>>>>>>>>>>>>> 本次结束 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
c)将脚本上传到Linux服务器中 执行 ./publish.sh 即可
d)结果:通过Maven命令自动编译并打包成一个 .war部署包,该包生成后会被复制到 tomcat服务器中的 webapps目录中,每次发布会保留上次发布历史,在浏览器上访问工程即可,如: localhost:8080/ltest
第二种:将工程war包以软链接形式发布到Tomcat服务器的webapps目录下
步骤与上面基本上类似,但是需要在Tomcat服务器 /conf/context.xml 文件添加一段代码
如下图:
将以上的shell脚本略作修改,即:
将上面shell脚本中的如下代码
cp /${server_publish_path}/${context}/target/framework-1.0.1.war ${server_tomcat_path}/webapps/${context}.war
修改为如下代码
ln -s /${server_publish_path}/${context}/target/framework-1.0.1.war ${server_tomcat_path}/webapps/${context}.war
即可;
二者比较嘛,第一种略中规中矩,第二种节省一些空间 省心;
本人比较推荐使用第二种(当然这种在SIT/UAT测试时,生产发布时得中规中矩的来了);
再给出一版本人整理的最新:
#!/bin/sh
#定义变量
date=$(date "+%Y%m%d")
datetime=$(date "+%Y%m%d%H%M%S")
#请先自己创建好目录: /opt/CIBS/cibs-pro/cibs
#请先自己创建好目录: /opt/CIBS/cibs-pro/cibs/cibs-publish-hist
echo ">>>>>>>>>>>>>>>>> 执行开始(${datetime}) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
context="cibs"
projectname="cibs-dev01"
server_publish_path="/opt/CIBS/cibs-pro"
echo "[日志]项目名称:"${context} >>${server_publish_path}/${context}.log
echo "[日志]工程目录:"${server_publish_path} >>${server_publish_path}/${context}.log
server_tomcat_path="/opt/CIBS/apache-tomcat-8.5.32"
echo "[日志]Tomcat服务器目录:"${server_tomcat_path} >>${server_publish_path}/${context}.log
echo "[日志]进入工程所在目录:"${server_publish_path}/${context} >>${server_publish_path}/${context}.log
cd /${server_publish_path}/${context}
server_svn_path="svn://localhost/repository/${context}/${context}-dev01"
svn_username="abcd"
svn_passwrd="abcd@abcd"
echo "[日志]SVN工程路径:"${server_svn_path} >>${server_publish_path}/${context}.log
echo "[日志]从SVN服务器提取最新代码 >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
svn checkout ${server_svn_path} --username ${svn_username} --password ${svn_passwrd} >>${server_publish_path}/${context}.log
echo "[日志]使用MVN命令编译并生成发布包 >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
cd /${server_publish_path}/${context}/${projectname}
echo "[日志]进入pom.xml文件所在目录:"${server_publish_path}/${context}/${projectname} >>${server_publish_path}/${context}.log
mvn clean package -Ppro
echo mvn clean package -Ppro >>${server_publish_path}/${context}.log
echo "[日志]stop Tomcat >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
${server_tomcat_path}/bin/catalina.sh stop
echo ${server_tomcat_path}/bin/catalina.sh stop >>${server_publish_path}/${context}.log
echo "[日志]等待10秒钟 >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
sleep 10s
echo "[日志]将原有发布文件移至历史目录 >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
cp /${server_publish_path}/${context}/${context}.war ${server_publish_path}/${context}/${context}-publish-hist/${context}.war${datetime}
echo cp /${server_publish_path}/${context}/${context} ${server_publish_path}/${context}/${context}-publish-hist/${context}${datetime} >>${server_publish_path}/${context}.log
echo "[日志]清除Tomcat服务器中webapps目录${context}软链接 >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
rm -rf ${server_tomcat_path}/webapps/${context}.war
echo rm -rf ${server_tomcat_path}/webapps/${context}.war
echo "[日志]清除Tomcat服务器中webapps目录${context} >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
rm -rf ${server_tomcat_path}/webapps/${context}
echo -rf ${server_tomcat_path}/webapps/${context} >>${server_publish_path}/${context}.log
cp ${server_publish_path}/${context}/${projectname}/target/${projectname}-0.0.1-SNAPSHOT.war ${server_publish_path}/${context}/${context}.war
echo cp ${server_publish_path}/${context}/${projectname}/target/${projectname}-0.0.1-SNAPSHOT.war ${server_publish_path}/${context}/${context}.war >>${server_publish_path}/${context}.log
ln -s /${server_publish_path}/${context}/${projectname}/target/${projectname}-0.0.1-SNAPSHOT.war ${server_tomcat_path}/webapps/${context}.war
echo ln -s /${server_publish_path}/${context}/${projectname}/target/${projectname}-0.0.1-SNAPSHOT.war ${server_tomcat_path}/webapps/${context}.war >>${server_publish_path}/${context}.log
echo "[日志]start tomcat >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
${server_tomcat_path}/bin/catalina.sh start
echo ${server_tomcat_path}/bin/catalina.sh start >>${server_publish_path}/${context}.log
echo "[日志]查看启动日志 >>>>>>>>>>>>>>>>>>>>>>>>" >>${server_publish_path}/${context}.log
tail -f /${server_tomcat_path}/logs/catalina.out
echo tail -f /${server_tomcat_path}/logs/catalina.out >>${server_publish_path}/${context}.log
echo ">>>>>>>>>>>>>>>>> 本次结束 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
结束,谢谢.. ...