Andrey Hihlovskiy

Professional blog on groovy, gradle, Java, Javascript and other stuff.

Monthly Archives: October 2013

groovy script for running jetty server

The following script starts jetty server and opens the folder, specified on command line, for http access (read-only):

#!/usr/bin/env groovy

@Grab('javax.servlet:javax.servlet-api:3.0.1')
@Grab(group='org.eclipse.jetty', module='jetty-webapp', version='8.1.8.v20121106')
@Grab(group='org.eclipse.jetty', module='jetty-server', version='8.1.8.v20121106', transitive=false)
@Grab(group='org.eclipse.jetty', module='jetty-servlet', version='8.1.8.v20121106', transitive=false)
@GrabExclude('org.eclipse.jetty.orbit:javax.servlet')

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.*
import groovy.servlet.*

def publishedFolder = args ? args[0] : '.'

def server = new Server(8080)
def context = new ServletContextHandler(server, '/', ServletContextHandler.SESSIONS)
def webappContext = new org.eclipse.jetty.webapp.WebAppContext(publishedFolder, '/jetty')
context.setHandler(webappContext)
server.start()
println 'Jetty server started. Press Ctrl+C to stop.'

Usage:

  1. Save this script to file “jetty.groovy”
  2. Invoke on command-line:
    groovy jetty.groovy /path/to/some/folder"
  3. Enter address in web-browser:
    http://localhost:8080/jetty

Expected result: you see the content of the folder “/path/to/some/folder” in the web-browser.

Advertisements