Deploy WAR in embedded Tomcat 7

后端 未结 1 1063
北荒
北荒 2020-12-16 17:51

I currently need to create a server in order to run a number of unit test. To simplify this process I would like to embed Tomcat into my code and load an instance of Tomcat

1条回答
  •  一个人的身影
    2020-12-16 18:17

    This code works with Tomcat 8.0:

    File catalinaHome = new File("..."); // folder must exist
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080); // HTTP port
    tomcat.setBaseDir(catalinaHome.getAbsolutePath());
    tomcat.getServer().addLifecycleListener(new VersionLoggerListener()); // nice to have
    

    You have now two options. Automatically deploy any web app in catalinaHome/webapps:

    // This magic line makes Tomcat look for WAR files in catalinaHome/webapps
    // and automatically deploy them
    tomcat.getHost().addLifecycleListener(new HostConfig());
    

    Or you can manually add WAR archives. Note: They can be anywhere on the hard disk.

    // Manually add WAR archives to deploy.
    // This allows to define the order in which the apps are discovered
    // plus the context path.
    File war = new File(...);
    tomcat.addWebapp("/contextPath", war.getAbsolutePath());
    

    0 讨论(0)
提交回复
热议问题