在开发 war 项目时,您通常会构建 war 并将其部署到已安装的 Tomcat 实例。这会消耗时间和资源,而且还需要一个本地 Tomcat 实例。
通过在 Maven 构建中嵌入的 Tomcat 实例中运行 war,run mojo 为您提供了避免这些工作的机会。
注意如果您有一个多模块 Maven 项目并使用 Maven3,您无需在使用 run 目标之前安装所有模块,只需从根模块使用 tomcat6/7:run,插件将自动从各个模块中检测构建输出目录,并用这些目录替换 webapp 类加载器中的依赖。
使用插件版本配置您的 pom(有关其他 mojo 参数,请参阅每个 mojo 的文档)。
并使用:mvn tomcat6/7:run
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<!-- or if you want to use tomcat 6.x
<artifactId>tomcat6-maven-plugin</artifactId>
-->
<version>2.2</version>
<configuration>
<!-- http port -->
<port>9090</port>
<!-- application path always starts with /-->
<path>/</path>
<!-- optional path to a context file -->
<contextFile>${tomcatContextXml}</contextFile>
<!-- optional system propoerties you want to add -->
<systemProperties>
<appserver.base>${project.build.directory}/appserver-base</appserver.base>
<appserver.home>${project.build.directory}/appserver-home</appserver.home>
<derby.system.home>${project.build.directory}/appserver-base/logs</derby.system.home>
<java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
</systemProperties>
<!-- if you want to use test dependencies rather than only runtime -->
<useTestClasspath>false</useTestClasspath>
<!-- optional if you want to add some extra directories into the classloader -->
<additionalClasspathDirs>
<additionalClasspathDir></additionalClasspathDir>
</additionalClasspathDirs>
</configuration>
<!-- For any extra dependencies needed when running embedded Tomcat (not WAR dependencies) add them below -->
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>\${derbyVersion}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</plugin>
pom.xml (top level pom with packaging pom) my-api/pom.xml (API project with packaging jar) my-api-impl/pom.xml (API implementation project with packaging jar) my-webapp/pom.xml (webapp project with packaging war)
使用上面给出的结构,从顶级目录使用 mvn tomcat6/7:run -pl :my-webapp -am。
您可以使用此 mojo 在 Tomcat 实例中启动您的应用程序,并针对此实例运行您的 selenium 测试。
以下配置将在预集成测试中启动嵌入式 Tomcat,并在集成测试后停止它。
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<!-- or if you want to use tomcat 6.x
<artifactId>tomcat6-maven-plugin</artifactId>
-->
<version>2.2</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
....
<fork>true</fork>
....
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>