1

My application runs fine on embedded Jetty 9.2.11, but after assembling with the maven assembly plugin, only servlets declared in web.xml are served.

My jar contains WEB-INF/web.xml using Servlet 3.0 format A number of jars are placed in WEB-INF/lib, each with META-INF/web-fragment.xml. These are discovered perfectly well when run from NetBeans.

QueuedThreadPool pool = new QueuedThreadPool(20);
Server server = new Server(pool);
server.setStopAtShutdown(true);
ServerConnector connector = new ServerConnector(server);
connector.setPort(8888);
server.addConnector(connector);
pool.start();
connector.start();

WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor("WEB-INF/web.xml");
webapp.setDefaultsDescriptor("webdefault.xml");
webapp.setClassLoader(MyURIClassLoader);
webapp.setExtractWAR(false);
webapp.setCopyWebDir(false);
webapp.setConfigurationDiscovered(true);

List<String> configs = new ArrayList<>(5);
configs.add("org.eclipse.jetty.webapp.WebInfConfiguration");
configs.add("org.eclipse.jetty.webapp.WebXmlConfiguration");
configs.add("org.eclipse.jetty.webapp.MetaInfConfiguration");
configs.add("org.eclipse.jetty.webapp.FragmentConfiguration");
configs.add("org.eclipse.jetty.webapp.JettyWebXmlConfiguration");
webapp.setConfigurationClasses(configs);

ContextHandlerCollection container = new ContextHandlerCollection();
container.setHandlers(handlers.toArray(new Handler[handlers.size()]));
server.setHandler(new HTTPGlobalHandler(container));
server.start();

All web.xml and web-fragment.xml files are pretty standard. Nothing but servlets and security settings including CORS.

The structure of the assembly is as follows:

/boot.jar (our in-house launcher with a custom URIClassLoader)
  |- /modules/ (a folder including our target jar with ALL dependencies provided)
    |- target.jar (containing the WEB-INF/lib folder with fragment-holding jars)
Justin Howard
  • 5,504
  • 1
  • 21
  • 48
  • What is the `WebAppContext` base resource in this situation? – Joakim Erdfelt Feb 29 '16 at 21:39
  • Joakim Erdfelt I do set the base resource (sorry it was omitted here) as the URL where WEB-INF/web.xml is located. I use a URLClassLoader to find the URL of this file and remove the end portion, leaving a string ending with a slash. – Sameh Atawy Feb 29 '16 at 22:44

1 Answers1

0

Having Servlet 3 compatible servlet container (Jetty 9) you can don't user web.xml at all and have only Java Configuration in your application. Here is a good starting point how to do this.

Community
  • 1
  • 1
Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41
  • Thanks. I realize that. However, the project dates back to pre 3.0 days and an older version of Jetty so I have a lot of code that will need to be revisited. Another point is that I haven't found a way to declare CORS settings in annotations. I could try it but will it help? If web fragments aren't scanned, why would annotations be any different? – Sameh Atawy Feb 29 '16 at 22:48