Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy WAR in embedded Tomcat 7

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).

My issue is how can I deploy my WAR file into the embedded Tomcat?

As you might notice I cannot use the tomcat maven plugin since I want it to run with the automated tests.

like image 874
stikku Avatar asked Jul 23 '13 12:07

stikku


People also ask

Can we deploy WAR file in Tomcat?

Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.

How do you deploy a WAR in TomEE?

Deploy a WAR in TomEE The easiest way to add and deploy an application to TomEE is to drop the application package into a deployment directory. For that purpose you have available two directories: webapps and apps. To deploy an Application, just copy the WAR file into the webapps or apps directories.


1 Answers

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

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());

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());
like image 77
Aaron Digulla Avatar answered Oct 19 '22 02:10

Aaron Digulla