How to run external war file with embedded tomcat of spring boot with gradle?

为君一笑 提交于 2020-01-01 17:09:11

问题


I am trying to deploy external war file into embedded tomcat of spring boot.I added gradle dependencies in the format of .war file and i want run this war with by spring boot app , but not running please anyone can help me out.


回答1:


Just try with this approach. add this code block in your spring boot application. your war file must be placed in a src/main/resources directory.

@Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        return new TomcatEmbeddedServletContainerFactory() {

            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

                new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();

                try {

                    tomcat.addWebapp("/cms", new ClassPathResource("cms.war").getFile().toString());

                } catch (Exception ex) {
                    throw new IllegalStateException("Failed to add webapp",ex);
                }
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }
        };

    }

-> change the base directory in the application.properties as

server.tomcat.basedir=temp-server



回答2:


A few classes have changed in Spring Boot 2 and hence you'll have to do:

@Bean
public ServletWebServerFactory servletContainer() {
    return new TomcatServletWebServerFactory() {
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();
            try {
                tomcat.addWebapp("/cms", new ClassPathResource("cms.war").getFile().toString());

            } catch (Exception ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatWebServer(tomcat);
        }
    };
}


来源:https://stackoverflow.com/questions/46150868/how-to-run-external-war-file-with-embedded-tomcat-of-spring-boot-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!