Vaadin 10/11 and embedded Jetty

854 Views Asked by At

I developed middle-size application on Vaadin 8.5.1. Jetty embedded 9.4.8 was used as Servlet container for Vaadin servlet. In Java code i initialize Jetty instance, create Vaadin servlet and attach it to Jetty. In Maven i use 'vaadin-maven-plugin' which helps me make correct settings to folders, also packaging is 'jar'. The Spring (not Spring Boot) are used for application configuration purposes and IoC.

Now i want to migrate project to Vaadin 10/11. I was tried all Vaadin Starter Packs which generates output JAR. But didn't understand how can i modify those packs to remove Spring Boot and get a simple Maven project with Jetty embedded.

Already asked question in Vaadin forum: Vaadin 10 + Jetty embedded

1

There are 1 best solutions below

0
On

You have to configure the Jetty Server as follows:

public class Application {

    public static void main(String... args) throws Exception {
        new Application().run(8080, "/");
    }

    public void run(int port, String contextPath) throws Exception {
        URL webRootLocation = this.getClass().getResource("/META-INF/resources/");
        URI webRootUri = webRootLocation.toURI();

        WebAppContext context = new WebAppContext();
        context.setBaseResource(Resource.newResource(webRootUri));
        context.setContextPath(contextPath);
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*");
        context.setConfigurationDiscovered(true);
        context.setConfigurations(new Configuration[]{
                new AnnotationConfiguration(),
                new WebInfConfiguration(),
                new WebXmlConfiguration(),
                new MetaInfConfiguration(),
                new FragmentConfiguration(),
                new EnvConfiguration(),
                new PlusConfiguration(),
                new JettyWebXmlConfiguration()
        });
        context.getServletContext().setExtendedListenerTypes(true);
        context.addEventListener(new ServletContextListeners());

        Server server = new Server(port);
        server.setHandler(context);

        server.start();
        server.join();
    }

}

Also, you need to use the maven-shade-plugin if you want to package the artifact as an uber-jar.

You can find an example for Vaadin 12+ at https://github.com/alejandro-du/embedded-jetty-demo