Maven实现Web应用集成測试自己主动化 -- 部署自己主动化(WebTest Maven Plugin)

时间:2023-03-08 17:15:08

上篇:Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

之前介绍了怎样在maven中使用webtest插件实现web的集成測试,这里有个遗留问题,就是在运行maven的intergation測试时候web应用已经部署在容器中处于in service的状态,那么web应用的部署能否够自己主动化呢?在我们公司的系统中,因为使用了weblogic的cluster,自己写了脚步来实现部署,花费了不少人力物力,事实上java web应用早就有福音了,是一款自己主动安装容器和部署应用的插件神器:cargo-maven2-plugin,这插件能够兼容全部眼下主流的server如jboss、tomcat、glassfish、jetty等。

它的配置非常easy,在build里面增加cargo-maven2-plugin,为了实现集成測试自己主动化,声明集成測试阶段之前调用cargo:start启动容器和部署应用,集成測试结束后调用cargo:stop关闭容器。默认start启动的容器在maven生存周期结束之后就会结束,有时候为了调试方便,能够调用cargo:run来启动容器和部署应用,在须要的时候再通过Ctrl+C结束容器执行。

为了实现容器启动和部署自己主动化,还须要提供一些配置參数给插件,以下给出个演示样例,Cargo还有非常多其他的功能,详细能够參考:Cargo自己主动部署官方站点

                     <plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${project.cargoPluginVersion}</version>
<configuration>
<!-- 定义container -->
<container>
<!-- container类型 jetty/tomcat/weblogic/jboss etc -->
<containerId>${project.cargo.container}</containerId>
<!-- 指定容器下载路径,保存路径,解压路径,放在target文件夹能够在clean的时候自己主动清除-->
<zipUrlInstaller>
<url>${project.cargo.container.url}</url>
<downloadDir>${project.build.directory}/${project.cargo.container}/downloads</downloadDir>
<extractDir>${project.build.directory}/${project.cargo.container}/extracts</extractDir>
</zipUrlInstaller>
<!-- cargo log 存储 -->
<log>${project.build.directory}/logs/cargo.log</log>
<!-- container log 存储 -->
<output>${project.build.directory}/logs/container.out</output>
</container>
<!-- 加入container相关的配置信息 -->
<configuration>
<!-- 设置deploy home -->
<home>${project.build.directory}/${project.cargo.container}/container</home>
<!-- <type>existing</type> existing type是使用已有的容器,在开放环境中使用,集成測试
一般使用默认的install type,每次又一次创建全部资源,保证每次測试环境OK,另一种
远程部署runtime type (见以下凝视),方便远程部署測试-->
<properties>
<!-- -->
<project.cargo.hostname>${project.cargo.host}</project.cargo.hostname>
<cargo.servlet.port>${project.cargo.port}</cargo.servlet.port>
<cargo.servlet.uriencoding>${project.build.sourceEncoding}</cargo.servlet.uriencoding>
<!--
为了防止和环境中其它节点冲突,能够设置容器专门属性,
<cargo.tomcat.ajp.port>8123</cargo.tomcat.ajp.port>
<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.username>admin</cargo.tomcat.manager.username>
<cargo.tomcat.manager.password>1234</cargo.tomcat.manager.password> -->
</properties>
<!-- for remote deploy, also container type should be remote
<type>runtime</type>
<properties>
<cargo.remote.username>username</cargo.remote.username>
<cargo.remote.password>password</cargo.remote.password>
</properties> -->
</configuration>
<pre> <!-- 须要部署的应用配置 -->
<deployables>
<deployable>
<!-- group/artifact使用默认值部署本应用,默认从maven output获取部署包,能够在location中指定
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<location> war location</location>
-->
<type>war</type>
<properties>
<context>optional root context</context>
</properties>
<pingURL>optional url to ping to know if deployable is done or not</pingURL>
<pingTimeout>optional timeout to ping (default 20000 milliseconds)</pingTimeout>
</deployable>
</deployables>
<!-- 定义打包路径 -->
<packager>
<outputLocation>${project.build.directory}/packaged</outputLocation>
</packager>
</configuration>
<executions>
<!--为了完毕集成測试,在開始集成測试之前启动容器和部署应用 -->
<execution>
                               <id>start-container</id>
                               <phase>pre-integration-test</phase>
                               <goals> <goal>start</goal> </goals>
                           </execution>
                           <!--測试完毕后,关闭容器 -->
                           <execution>
                               <id>stop-container</id>
                               <phase>post-integration-test</phase>
                               <goals> <goal>stop</goal> </goals>
                           </execution>
                           <!--关闭容器后打包容器留作日后分析 -->
                          <execution>
                               <id>package-container</id>
                               <phase>post-integration-test</phase>
                               <goals> <goal>package</goal> </goals>
                           </execution>
                         </executions>
                      </plugin>