简体   繁体   English

向Spring Boot嵌入式tomcat添加战争

[英]Add war to spring boot embedded tomcat

I have a spring-boot 2.1.2.RELEASE application that uses embedded tomcat webserver and uses OpenKM via it's SDK. 我有一个spring-boot 2.1.2.RELEASE应用程序,该应用程序使用嵌入式tomcat Web服务器并通过其SDK使用OpenKM

Now, I have some integration tests that use restassured lib to make REST calls and verify response structure. 现在,我进行了一些集成测试,这些测试使用可restassured lib进行REST调用并验证响应结构。 My idea is to integrate OpenKM.war into this wmbedded tomcat and be able to run this tests without need to have openkm application running on some different server. 我的想法是将OpenKM.war集成到此嵌入式tomcat中,并能够运行此测试,而无需在其他服务器上运行openkm应用程序。

this is how I make embedded tomcat to read and deploy openkm.war: 这就是我使嵌入式tomcat读取和部署openkm.war的方式:

@Configuration
public class TomcatConfig {
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }

            @Override
            protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                new File(tomcat.getServer().getCatalinaBase(), "webapp").mkdirs();
                try {
                    tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
                } catch (Exception ex) {
                    throw new IllegalStateException("Failed to add okm", ex);
                }
                return super.getTomcatWebServer(tomcat);
            }
        };

        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
}

deploy takes way to longer than on the standalone tomcat webserver that is shipped with openkm, but it deploys. 与openkm附带的独立tomcat Web服务器上相比,deploy需要更长的时间,但是它可以部署。

However deployment process fails with: 但是,部署过程因以下原因而失败:

IOException parsing XML document from URL [file:/tmp/tomcat.2352216859213135410.8080/openkm.xml]; nested exception is java.io.FileNotFoundException: /tmp/tomcat.2352216859213135410.8080/openkm.xml (No such file or directory)

I have this file (plus server.xml, context.xml) alongside openkm.war file, but it seems like embedded tomcat doesn't know about it. 我在openkm.war文件旁边有这个文件(加上server.xml,context.xml),但似乎嵌入式tomcat对此一无所知。

All these files are located in src/main/resources/webapp . 所有这些文件都位于src/main/resources/webapp

So, I would like to know what configuration piece am I missing or whether there is whole other better to achieve what I want? 因此,我想知道我缺少什么配置,或者是否还有其他更好的方法可以实现我想要的?

You can try doing something like; 您可以尝试做类似的事情;

StandardContext ctx = (StandardContext) tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
WebResourceRoot resources = new StandardRoot(ctx);
resources.addPreResources(new DirResourceSet(resources, "{target mount path}",
        new File("src/main/resources/webapp").getAbsolutePath(), "/"));
ctx.setResources(resources);

So that you can have your src/main/resources/webapp/ folder mounted on {target mount path} in tomcat deployment. 这样您就可以在Tomcat部署中将src/main/resources/webapp/文件夹安装在{target mount path}上。 Though I am unsure on this path? 虽然我不确定这条路吗? Can you try with "/" , and maybe "/WEB-INF/" ? 您可以尝试使用"/" ,也可以尝试使用"/" "/WEB-INF/"吗? I have no way to test in my local currently but I hope this will help somehow. 我目前无法在本地进行测试,但我希望这会有所帮助。

You can mount individual files with FileResourceSet as well, if you need different files in different folders in tomcat. 如果需要在tomcat中不同文件夹中的其他文件,也可以使用FileResourceSet挂载单个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM