Spring Boot: How to add another WAR files to the embedded tomcat?
You can add a war file to embedded Tomcat using Tomcat.addWebapp
. As its javadoc says, it's the "equivalent to adding a web application to Tomcat's web apps directory". To use this API in Spring Boot, you need to use a custom TomcatEmbeddedServletContainerFactory
subclass:
@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
// Ensure that the webapps directory exists
new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();
try {
Context context = tomcat.addWebapp("/foo", "/path/to/foo.war");
// Allow the webapp to load classes from your fat jar
context.setParentClassLoader(getClass().getClassLoader());
} catch (ServletException ex) {
throw new IllegalStateException("Failed to add webapp", ex);
}
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
}
The accepted answer covers Spring Boot 1.x. The class mentioned is no longer present in Spring Boot 2.x. When using version 2, you need to use a different one:
@Bean
@ConditionalOnProperty(name = "external.war.file")
public TomcatServletWebServerFactory servletContainerFactory(@Value("${external.war.file}") String path,
@Value("${external.war.context:}") String contextPath) {
return new TomcatServletWebServerFactory() {
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();
Context context = tomcat.addWebapp(contextPath, path);
context.setParentClassLoader(getClass().getClassLoader());
return super.getTomcatWebServer(tomcat);
}
};
}
Also, Spring boot enbedded Tomcat does not by default contain dependencies for JSPs. If you are using JSPs in your external war, you need to include them.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
UPDATE: I've written a more detailed blog post on how to set this up for both Spring Boot 1 and 2.