2

My embedded tomcat class recognizes the web.xml and ServletContainerInitializers, but it doesn't recognize the web-fragments. Very simple code. What flag/config should be turned on for web-fragments to initialized?

    public static void main( String[] args ){

    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(Level.INFO);

    String webappDirLocation = "src/main/webapp/";
    Tomcat tomcat = new Tomcat();

    tomcat.getHost().setAutoDeploy(true);
    tomcat.getHost().setDeployOnStartup(true);
    tomcat.setPort(8080);


    StandardContext ctx = null;
    try {
        ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
    } catch (ServletException e) {
        e.printStackTrace();
    }


    System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

    File additionWebInfClasses = new File("target/classes");
    WebResourceRoot resources = new StandardRoot(ctx);
    resources.addPreResources(new DirResourceSet(resources,
                                "/WEB-INF/classes",
                                additionWebInfClasses.getAbsolutePath(),
                                "/"));

    ctx.setResources(resources);
    ctx.setAltDDName( webappDirLocation + "WEB-INF/web.xml");
    ServletContextListener scl = new AdminServletContextListener();
    ctx.addApplicationEventListener(scl);

    try {

        tomcat.start();
        tomcat.getServer().start();
    } catch (LifecycleException e) {
        e.printStackTrace();
    }

    tomcat.getServer().await();
}
Nambi
  • 2,688
  • 8
  • 28
  • 37

0 Answers0