Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScalaWilliam/82ee7d832cc5bfd4d59d to your computer and use it in GitHub Desktop.
Save ScalaWilliam/82ee7d832cc5bfd4d59d to your computer and use it in GitHub Desktop.
Embedded Jetty + Scalatra with Webjars loading (loads META-INF/resources data from the classpath jars). Works without any configuration in Jetty, jetty-maven-plugin, but not so easily in embedded form. Make sure to create websresources/index.html in your src/main/resources/ directory. Examples provided in Scalatra documentation are strange - set…
def initialiseApp[T<:LifeCycle](port: Int) = {
val server = new Server(port)
val context = new WebAppContext()
context.setContextPath("/")
context.setResourceBase(new File(getClass.getResource("/webresources/index.html").getFile).getCanonicalFile.getParentFile.getCanonicalPath)
context.addEventListener(new ScalatraListener)
context.addServlet(classOf[DefaultServlet], "/")
context.setInitParameter(ScalatraListener.LifeCycleKey, classOf[ConfiguredBootstrap].getCanonicalName)
context.setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, ".*\\.jar$")
context.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*\\.jar$")
context.setConfigurations(Array(new WebInfConfiguration, new MetaInfConfiguration))
context.setHandler(new ResourceHandler)
server.setHandler(context)
server
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment