Created
July 6, 2018 12:17
-
-
Save nejckorasa/84bc9fb6c70cb2c37bd7bcb2d1feafd7 to your computer and use it in GitHub Desktop.
Deploy war alongside jar. Add webapp to Tomcat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.util.Arrays; | |
| import javax.servlet.ServletException; | |
| import org.apache.catalina.startup.Tomcat; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; | |
| import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer; | |
| import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.context.annotation.Profile; | |
| /** | |
| * @author Nejc Korasa | |
| */ | |
| @Configuration | |
| @Profile("grab-client") | |
| public class TomcatConfig | |
| { | |
| private static final Logger LOG = LoggerFactory.getLogger(TomcatConfig.class); | |
| @Bean | |
| public EmbeddedServletContainerFactory servletContainerFactory() | |
| { | |
| return new TomcatEmbeddedServletContainerFactory() | |
| { | |
| @Override | |
| protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(final Tomcat tomcat) | |
| { | |
| try | |
| { | |
| final String contextPath = "/client"; | |
| final String warPath = getWarPath(tomcat.getHost().getAppBaseFile(), "client.war"); | |
| LOG.info(String.format("Adding client war to Tomcat [contextPath, warPath] : %s, %s", contextPath, warPath)); | |
| tomcat | |
| .addWebapp(contextPath, warPath) | |
| .setParentClassLoader(getClass().getClassLoader()); | |
| LOG.info(String.format("Catalina base set to :%s", tomcat.getServer().getCatalinaBase())); | |
| } | |
| catch (final ServletException ex) | |
| { | |
| throw new IllegalStateException("Failed to add web app", ex); | |
| } | |
| return super.getTomcatEmbeddedServletContainer(tomcat); | |
| } | |
| }; | |
| } | |
| private static String getWarPath(final File base, final String warName) | |
| { | |
| final File[] files = base.listFiles((dir, name) -> name.equals(warName)); | |
| return files == null ? null : Arrays.stream(files).findFirst().map(File::getAbsolutePath).orElse(null); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment