Add resources to Jetty programmatically

后端 未结 2 682
梦谈多话
梦谈多话 2021-02-09 07:27

I have a main class that configures and fires up Jetty. (That\'s standard practice from Wicket for testing a webapp, but it\'s really not wicket-specific.)

final         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-09 08:27

    For completeness, here's the workaround I have been using:

    final Server server = new Server();
    //skipped socketconnector initialization
    
    final WebAppContext bb = new WebAppContext();
    bb.setServer(server);
    bb.setContextPath("/");
    bb.setWar("src/main/webapp");
    final URL url = new File("target/classes").getAbsoluteFile().toURI().toURL();
    final Resource resource = new FileResource(url);
    final ResourceHandler handler = new ResourceHandler();
    handler.setBaseResource(resource);
    server.addHandler(handler);
    server.addHandler(bb);
    server.start();
    

    But it's not very elegant, it mounts everything in target/classes and uses the webapp only as fallback.

提交回复
热议问题