Friday, May 6, 2016

Run a Maven Vaadin application on an embedded Jetty server in Eclipse

Source: Embedding Jetty

Add Maven Dependencies

<properties>
 <jetty.version>9.3.0.v20150612</jetty.version>
</properties>

<dependencies>
<dependency>
 <groupId>org.eclipse.jetty</groupId>
 <artifactId>jetty-servlet</artifactId>
 <version>${jetty.version}</version>
</dependency>
<dependency>
 <groupId>org.eclipse.jetty</groupId>
 <artifactId>jetty-continuation</artifactId>
 <version>${jetty.version}</version>
</dependency>
<dependency>
 <groupId>org.eclipse.jetty</groupId>
 <artifactId>jetty-server</artifactId>
 <version>${jetty.version}</version>
</dependency>
</dependencies>

Create a Server

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.resource.PathResource;

public class VaadinApplicationServer 
{
    public static void main( String[] args ) throws Exception
    {
        Server server = new Server(8080);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        context.setBaseResource(new PathResource(new File("src/main/webapp")));
        context.addServlet(MyUIServlet.class, "/*");
        server.setHandler(context);
        server.start();
        server.join();
    }
}

Run as Java application

Now it is possible to run your Vaadin project as a Java application with the Jetty VaadinApplicationServer class.

No comments:

Post a Comment