在嵌入式Tomcat 7中部署WAR [英] Deploy WAR in embedded Tomcat 7
问题描述
我目前需要创建一个服务器才能运行多个单元测试。为了简化这个过程,我想在运行单元测试(使用@BeforeClass表示法)之前将Tomcat嵌入到我的代码中并加载一个Tomcat实例(然后加载我的WAR文件)。
I currently need to create a server in order to run a number of unit test. To simplify this process I would like to embed Tomcat into my code and load an instance of Tomcat (which in turn loads my WAR file) before running the unit test (using the @BeforeClass notation).
我的问题是如何将我的WAR文件部署到嵌入式Tomcat中?
My issue is how can I deploy my WAR file into the embedded Tomcat?
您可能会注意到我不能使用tomcat maven插件,因为我想要它与自动化测试一起运行。
As you might notice I cannot use the tomcat maven plugin since I want it to run with the automated tests.
推荐答案
此代码适用于Tomcat 8.0:
This code works with Tomcat 8.0:
File catalinaHome = new File("..."); // folder must exist
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080); // HTTP port
tomcat.setBaseDir(catalinaHome.getAbsolutePath());
tomcat.getServer().addLifecycleListener(new VersionLoggerListener()); // nice to have
你现在有两个选择。在 catalinaHome / webapps
中自动部署任何Web应用程序:
You have now two options. Automatically deploy any web app in catalinaHome/webapps
:
// This magic line makes Tomcat look for WAR files in catalinaHome/webapps
// and automatically deploy them
tomcat.getHost().addLifecycleListener(new HostConfig());
或者您可以手动添加WAR档案。注意:它们可以位于硬盘上的任何位置。
Or you can manually add WAR archives. Note: They can be anywhere on the hard disk.
// Manually add WAR archives to deploy.
// This allows to define the order in which the apps are discovered
// plus the context path.
File war = new File(...);
tomcat.addWebapp("/contextPath", war.getAbsolutePath());
这篇关于在嵌入式Tomcat 7中部署WAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!