title: Spring Boot: How to add another WAR files to the embedded tomcat?
body: <p>Spring Boot's embedded tomcat is very handy, for both development and deploy.</p>
  
  <p>But what if an another (3rd-party) WAR file (for example, GeoServer) should be added?</p>
  
  <p>Perhaps the following is the normal procedure:</p>
  
  <ol>
  <li>Install a normal Tomcat server.</li>
  <li>Build the Spring Boot application as a WAR file, and add it to the webapps folder of the Tomcat.</li>
  <li>Also add an another (3rd-party) WAR file to the webapps folder.</li>
  </ol>
  
  <p>But it would be nice if the following configuration were possible.</p>
  
  <ol>
  <li>Build the Spring boot application as a standalone Jar, which includes the embedded Tomcat.</li>
  <li>Deploy the Spring boot application Jar.</li>
  <li>Add an another (3rd-party) WAR file to a folder which the embedded Tomcat recognizes.</li>
  <li>Serve both the Spring boot application contents and the another WAR's contents using the embedded Tomcat.</li>
  </ol>
  
  <p>How can it be done?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When the spring boot application is made of fat jar(=executable jar), the code in the answer is not enough. The revised one is as follows:</p>
  
  <pre><code>@Bean
  public EmbeddedServletContainerFactory servletContainerFactory() {
      return new TomcatEmbeddedServletContainerFactory() {
  
          @Override
          protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                  Tomcat tomcat) {
              try {
                  Context context = tomcat.addWebapp("/foo", "/path/to/foo.war");
                  WebappLoader loader =
                      new WebappLoader(Thread.currentThread().getContextClassLoader());
                  context.setLoader(loader);
              } catch (ServletException ex) {
                  throw new IllegalStateException("Failed to add webapp", ex);
              }
              return super.getTomcatEmbeddedServletContainer(tomcat);
          }
  
      };
  }
  </code></pre>
  
  <p>Since the jar files in a fat jar cannot be loaded by the system classloader, an explicit parent classloader must be specified. Otherwise, the additional WAR cannot load the library jars in the fat jar of the spring boot application that added the WAR.</p>