test环境中自动完成从cvs更新代码、构建部署等,简化部署过程!
之前文章中描述了怎么利用maven profile分dev、test、pro三种环境,为了方便test环境中部署最新代码,利用maven和shell,执行脚本完成代码从cvs的更新、构建、undeply、停止tomcat、deploy、启动tomcat过程。主要借助三个插件帮助我完成上面所有动作
1:maven scm plugin 从cvs更新代码
2:maven cargo plugin 执行undeply、停止tomcat、deploy、启动tomcat
3:mavaen antrun plugin, 如果cargo中配置jmx端口,jvm启动jmx。cargo:stop无法正常停止服务器,于是使用antrun plugin来停止服务器。
下面贴出pom.xml配置和shell script,配置不算复杂,相信很容易看懂。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ustcsoft.eisp.cargo</groupId><artifactId>cargo-deployer-local</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><properties><myproject.name>ROOT</myproject.name><cargo.project.groupid>com.ustcsoft.eisp</cargo.project.groupid><cargo.project.artifactid>eisp-integration</cargo.project.artifactid><cargo.project.version>0.0.1-SNAPSHOT</cargo.project.version><!--可单独提取并配置到profiles文件来激活 --><catalina.home>/home/tomcat/eisp-tomcat</catalina.home><cargo.tomcat.manager.protocol>http</cargo.tomcat.manager.protocol><cargo.tomcat.manager.hostname>127.0.0.1</cargo.tomcat.manager.hostname><cargo.tomcat.manager.port>8080</cargo.tomcat.manager.port><cargo.tomcat.manager.ajp.port>8009</cargo.tomcat.manager.ajp.port><cargo.tomcat.manager.username>manager</cargo.tomcat.manager.username><cargo.tomcat.manager.password>000000</cargo.tomcat.manager.password> </properties><dependencies><dependency><groupId>${cargo.project.groupid}</groupId><artifactId>${cargo.project.artifactid}</artifactId><version>${cargo.project.version}</version><type>war</type></dependency></dependencies><scm><connection>scm:cvs:pserver:ste-cloud:starrd-tester@192.168.20.4:1401/TS-CLOUD</connection><developerConnection>scm:cvs:pserver:ste-cloud:starrd-tester@192.168.20.4:1401/TS-CLOUD</developerConnection></scm><build><plugins><plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <configuration> <username>ste-cloud</username> <workingDirectory>/home/tomcat/sources/eisp</workingDirectory> <providerImplementations> <cvs>cvs_native</cvs> </providerImplementations> </configuration> </plugin><plugin><groupId>org.codehaus.cargo</groupId><artifactId>cargo-maven2-plugin</artifactId><version>1.2.0</version><configuration><wait>true</wait><!--以下标记将详细描述你想要部署的容器--><container><containerId>tomcat7x</containerId><type>installed</type><!-- 本地已安装Tomcat时设置 --><home>${catalina.home}</home><!--本地Tomcat安装目录 --></container><!-- 容器的属性 --><configuration><!-- 如果你在容器属性中指定类型为"installed"的话,你就必须在如下配置type==existing和重配一次安装目录--><type>existing</type><home>${catalina.home}</home><properties><cargo.tomcat.manager.url>${cargo.tomcat.manager.protocol}://${cargo.tomcat.manager.hostname}:${cargo.tomcat.manager.port}/manager</cargo.tomcat.manager.url><cargo.servlet.port>${cargo.tomcat.manager.port}</cargo.servlet.port><cargo.remote.username>${cargo.tomcat.manager.username}</cargo.remote.username><cargo.remote.password>${cargo.tomcat.manager.password}</cargo.remote.password><cargo.tomcat.ajp.port>${cargo.tomcat.manager.ajp.port}</cargo.tomcat.ajp.port><cargo.jvmargs>-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=172.17.3.242 -Djava.awt.headless=true</cargo.jvmargs></properties></configuration><!-- 准备执行部署 --><deployer><!-- 类型设置必须跟容器一致 --><type>installed</type><deployables> <!--在deployable中指定你要部署的webapp --><deployable> <groupId>${cargo.project.groupid}</groupId> <artifactId>${cargo.project.artifactid}</artifactId> <type>war</type> <!--如果你想重新定义Web应用的名称时设置,大多数情况下你都会定义此属性--> <properties> <context>${myproject.name}</context> </properties></deployable></deployables></deployer></configuration></plugin><plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <configuration> <target> <exec executable="bash" dir="${catalina.home}"> <arg line="./bin/catalina.sh stop"/> </exec> <sleep seconds="5" /> </target> </configuration></plugin></plugins></build></project>
#!/bin/sh#功能:测试环境代码更新、打包、停止服务器、部署、启动服务器功能的实现#提示键盘输入printf "测试环境代码是否需要更新打包Please enter Yes/No:"#读取输入字符read acase $a inYes|yes|y) echo "you choose Yes" #更新代码 mvn scm:update cd /home/tomcat/sources/eisp/com.ustcsoft.eisp.parent #打包 mvn install -U cd /home/tomcat/sources/cargo/cargo-deployer-local #撤销部署 mvn cargo:undeploy #休息10s sleep 10s #停止服务器,增加jmx端口以后,无法使用cargo:stop正常停止服务器,使用ant 调用shell停止服务器。 mvn antrun:run #部署 mvn cargo:deploy #启动服务器 nohup mvn cargo:run &;;No|no|n) echo "you choose No" cd /home/tomcat/sources/cargo/cargo-deployer-local mvn cargo:undeploy sleep 10s mvn cargo:stop mvn cargo:deploy nohup mvn cargo:run &;;*) echo "you choose error";;esac