How to expand war in Spring Boot's embedded to

2020-03-31 03:55发布

I have a war archive with a web application. I want to start that application within my Spring Boot application. I therefore followed the advice from that question:

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

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            try {
                tomcat.addWebapp("blog", "/tmp/roller.war");

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

    };
}

That works quite well, except the war doesn't get extracted:

Unable to create the directory [/tmp/tomcat.9153500015669016883.8080/webapps/blog]

That's the layout of the tomcat working directory:

$ find /tmp/tomcat.9153500015669016883.8080/
/tmp/tomcat.9153500015669016883.8080/
/tmp/tomcat.9153500015669016883.8080/work
/tmp/tomcat.9153500015669016883.8080/work/Tomcat
/tmp/tomcat.9153500015669016883.8080/work/Tomcat/localhost
/tmp/tomcat.9153500015669016883.8080/work/Tomcat/localhost/blog
/tmp/tomcat.9153500015669016883.8080/work/Tomcat/localhost/ROOT

A breakpoint in ExpandWar.expand() reveils that it wants to create a directory in webapps/. This non existing webapps folder is taken from Host.getAppBaseFile() (which comes from the ContextConfig.context).

So for me it looks like something is weird configured and it should be expanded into work/Tomcat/localhost/blog. How can I do that?

1条回答
疯言疯语
2楼-- · 2020-03-31 04:23

It seems that this is not spring related, but expected behaviour of an embedded tomcat. I found this related issue:

The user is expected to define a valid directory for a Host's appBase.

So simply creating the default appBase directory before deployment does the trick:

tomcat.getHost().getAppBaseFile().mkdir();
tomcat.addWebapp("blog", "/tmp/roller.war");
查看更多
登录 后发表回答