jettyjandex

Enable Jandex index for Jetty


I went through Jetty documentation, unfortunately I failed to find a documentation on how to hook into Jetty classpath scanner and modify it to use a Jandex index instead. Therefore I'm asking for help for the Jetty community.

I am the author of Vaadin Boot and I'm looking for ways to speed up Jetty bootup. I am aware of Jetty Quickstart however it's definitely not easy to produce quickstart-web.xml from Gradle. Therefore, I thought that I could build the Jandex Index of the app, then hook into Jetty classpath scanner to use that prebuilt Jandex Index.

I went through Jetty sources, however unfortunately I failed to find the way to reconfigure/replace the classpath scanner. Would you please have a couple of pointers for me on where to start, which class to override and how to register my custom classpath scanner to WebAppContext? Thanks a ton!


Solution

  • If you want to speed up startup, using Jandex is a not an easy solution. (you'll be chasing all of your third party jars that do scanning, and jakarta jars too that do scanning, and you'll have to subvert the entire Servlet configuration layer to even get started effectively, and then you'll need to add the servlet classloader isolation logic on top of Jandex too)

    Use quickstart, but use it in embedded mode with a custom main(String[] args)

    Example:

    package org.example.project;
    
    import org.eclipse.jetty.annotations.AnnotationConfiguration;
    import org.eclipse.jetty.plus.webapp.EnvConfiguration;
    import org.eclipse.jetty.plus.webapp.PlusConfiguration;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.util.resource.Resource;
    import org.eclipse.jetty.webapp.WebAppContext;
    import org.eclipse.jetty.xml.XmlConfiguration;
    
    public class Quickstart
    {
        public static void main(String... args) throws Exception
        {
            if (args.length < 1)
                error("No WAR file or directory given");
    
            //war file or dir to start
            String war = args[0];
    
            //optional jetty context xml file to configure the webapp
            Resource contextXml = null;
            if (args.length > 1)
                contextXml = Resource.newResource(args[1]);
    
            Server server = new Server(8080);
    
            WebAppContext webapp = new WebAppContext();
            webapp.addConfiguration(new QuickStartConfiguration(),
                                        new EnvConfiguration(),
                                        new PlusConfiguration(),
                                        new AnnotationConfiguration());
            webapp.setAttribute(QuickStartConfiguration.MODE, QuickStartConfiguration.Mode.GENERATE);
            webapp.setWar(war);
            webapp.setContextPath("/");
    
            //apply context xml file
            if (contextXml != null)
            {
                XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml);
                xmlConfiguration.configure(webapp);
            }
    
            server.setHandler(webapp);
    
            server.start();
    
            server.join();
        }
    
        private static void error(String message)
        {
            System.err.println("ERROR: " + message);
            System.err.println("Usage: java -jar QuickStartWar.jar <war-directory> <context-xml>");
            System.err.println("       java -jar QuickStartWar.jar <war-file> <context-xml>");
            System.exit(1);
        }
    }
    

    This main() takes a war + context-xml, and generates the WEB-INF/quickstart-web.xml.