2

Lightning fast development with eclipse and jetty

11 September, 2009 by manuel | is it offensive?

In a company I used to work at we were developing a midsize web application that we built using maven. Build time was 5 minutes and resulted in a ~70Mb .war file. This lag was very painful when it was time to debug, and by the time the build was finished I would sometimes find myself trying to remember what I was working on, at the same time having descended into playing Tetris (or occasionally Barrage). Not to say that there aren't medium to large apps that must take their time to compile, but I think in our case we had overcomplicated things for ourselves.

Nowadays for building pagegoblin I am using ant (I like it better personally), and build time is 5 seconds. Yet even this is too slow when developing. I would like to make changes in eclipse and be able to instantly test the results in my browser, before my mind wanders off to visions of lattes.

To achieve this I am using an embedded jetty server.

public class EclipseMain {
    public static void main(String[] args) throws Exception {
        Server server = new Server();

        Context context = new Context(server, "/", Context.SESSIONS);

        // This allows me to have cookies that work over subdomains
        HashMap map = new HashMap();
        map.put("org.mortbay.jetty.servlet.SessionDomain", ".localhost.localhost");
        context.setInitParams(map);

        // I am using both http and https in my app and want to test both
        Connector connector = new SelectChannelConnector();
        connector.setPort(8080);

        SslSocketConnector sslConnector = new SslSocketConnector();
        sslConnector.setPort(8443);
        sslConnector.setKeystore("/path/to/my/keystore");
        sslConnector.setPassword("wouldntyouliketoknow");
        sslConnector.setKeyPassword("wouldntyouliketoknow");
        sslConnector.setTruststore("/path/to/my/keystore");
        sslConnector.setTrustPassword("wouldntyouliketoknow");  
        server.setConnectors(new Connector[] {connector, sslConnector});

        // enable persistent sessions, so we don't need to relogin after restart
        enablePersistentSessions(context, "/home/manuel/.jetty_test");

        // Using my own jurlmap library here
        context.addFilter(new FilterHolder(new GoblinDispatchFilter()), "/*", 
                          org.mortbay.jetty.Handler.REQUEST 
                          | org.mortbay.jetty.Handler.FORWARD);

        // Static files such as images, css
        context.setResourceBase("web"); 
        // Serve static files
        context.addServlet(DefaultServlet.class, "/"); 

        // Server is ready to start
        server.start();
    }

    public static void enablePersistentSessions(Context context, String folder) {
        File file = new File(folder);

        if (!file.exists()) {
            file.mkdirs();
        } else if (file.isFile()) {
            throw new RuntimeException("Session location not a folder: " + folder);
        }

        final HashSessionManager manager = new HashSessionManager();
        manager.setStoreDirectory(file);
        context.getSessionHandler().setSessionManager(manager);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                try {
                    manager.saveSessions();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

If you are not using JSP then you just need to add these three libraries from the jetty distribution to your project classpath:

jetty-6.1.14.jar
jetty-util-6.1.14.jar
servlet-api-2.5-6.1.14.jar

With this setup it takes about two seconds between making changes and being able to test the results. Just edit your code and run EclipseMain as a normal application (in eclipse Run As Java Application). I have configured persistent sessions which means I don't even have to log back in to my app after every server restart, I just hit refresh and I am right back where I left. Coding is fun again!


Share with:




Replies (3)




0

waiting for an app to build means it's really worthwhile.

11 September, 2009 by chris | link | is it offensive?



0

Coding a worthwhile app without waiting is even better.

11 September, 2009 by manuel | link | is it offensive?



0

Thanks for sharing!

23 December, 2009 by steve | link | is it offensive?

Login and post a reply