Looking for example of Vaadin 10+ in an embedded Jetty

Hi Vaadin community

Does anyone have an example of how to launch Vaadin 10+ in an embedded Jetty? There are hundreds of examples around to do this using a Maven plugin (mvn jetty:run) but what I’m looking for is a snippet to successfully launch using a plain old Java main method.

So far, I’m able to launch Jetty with a VaadinServlet but it seems that Route annotations are not located and static resources cannot be found. Can anyone help?

		Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.addServlet(VaadinServlet.class, "/*");
		server.setHandler(handler);
		server.start()

Many thanks

Interesting too.

Hi,
Just curious : why to you need this? Why would you not use Spring Boot or embedded Jetty?

Well Spring (+Boot) is out of the question because my team started the project years ago on Guice so it was not practical to migrate.

The embedded Jetty route, launched via code, is exactly what I have for Vaadin 8 and works just fine but with the newer Flow based Vaadin(s), though there isn’t much guidance around about how to start it manually without the automatic help of Spring or Maven.

Now the snippet I posted was sufficient for older Vaadins but with the current version, @Route("") annotation scanning and various other bits of automation isn’t really documented properly which is why I’m asking for help in this area to help run up a normal non-Spring application.

Thanks

There is my questions too:

[Vaadin 10/11 and embedded Jetty]
(https://stackoverflow.com/questions/52420045/vaadin-10-11-and-embedded-jetty)

[Vaadin 10 + Jetty embedded.]
(https://vaadin.com/forum/thread/17266527/vaadin-10-jetty-embedded)

I have the same question. Can anyone please provide a snippet/example of how to do this?

This server configuration can be used as starting point.

class JettyStarter{
    fun run(){
        val server = Server(8080)
        //val path = "build/classes/kotlin/main"
        val path = "out/production/classes"
        val context = WebAppContext(path, "/")
       
        context.isConfigurationDiscovered = true
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*")
        context.configurations = arrayOf(
                AnnotationConfiguration(),
                WebInfConfiguration(),
                WebXmlConfiguration(),
                MetaInfConfiguration(),
                FragmentConfiguration(),
                EnvConfiguration(),
                PlusConfiguration(),
                JettyWebXmlConfiguration()
        )
        context.servletContext.isExtendedListenerTypes = true
        context.addEventListener(ServletContextListeners())
		server.handler = context
        server.start()
        server.join()
    }
}

This way I have test form rendered correctly:

@Viewport("width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes")
@Theme(value = Lumo::class)
@Route("")
class MainView : FormLayout() {
    init {
        textField {
            label = "Login"
            placeholder = "your login"
        }
        passwordField {
            label = "password"
        }
        button("Go").onLeftClick {
            dialog {
                label("Dialog here")
            }.open()
        }
    }
}

Here’s an example that shows how to create an executable uber-jar using Vaadin 12+ and an embedded Jetty server: https://github.com/alejandro-du/embedded-jetty-demo

Hope that helps!

Here is an up to date step by step tutorial on how to get embedded Jetty into your Vaadin Flow app: https://vaadin.com/tutorials/embedded-jetty-server-in-vaadin-flow

Thanks to everyone contributing to this thread with examples. This was really useful and answers my question fully.