Scalatra cannot find view when deploying as stand-alone

647 views Asked by At

I'm using the scalatra documentation and the sbt-assembly plugin to create a stand-alone jar. It works on my local machine (Debian Wheezy with OpenJdk 1.7.0), but fails on the remote EC2 machine (Ubuntu 12.04.2 LTS) running the same Jdk.

The exception message is:

org.fusesource.scalate.util.ResourceNotFoundException: Could not load resource: [index.ssp]; are you sure it's within [/home/abc/src/main/webapp]?
    at org.fusesource.scalate.servlet.ServletResourceLoader.createNotFoundException(ServletResourceLoader.scala:79)
    at org.fusesource.scalate.util.ResourceLoader$class.resourceOrFail(ResourceLoader.scala:55)
    at org.fusesource.scalate.servlet.ServletResourceLoader.resourceOrFail(ServletResourceLoader.scala:37)
    at org.fusesource.scalate.util.UriResource.delegate(Resource.scala:91)
    at org.fusesource.scalate.util.DelegateResource.text(Resource.scala:216)
    at org.fusesource.scalate.ssp.SspCodeGenerator.generate(SspCodeGenerator.scala:152)
    at org.fusesource.scalate.TemplateEngine.compileAndLoad(TemplateEngine.scala:750)
    at org.fusesource.scalate.TemplateEngine.compileAndLoadEntry(TemplateEngine.scala:699)
    at org.fusesource.scalate.TemplateEngine.liftedTree1$1(TemplateEngine.scala:419)

It says it cannot find the view, but the exact same jar file works on my local machine.

1

There are 1 answers

1
Stefan Ollinger On

You need to set the correct resource base on the embedded Jetty. Here are three options:

  1. attach resources to .jar file, by following the instructions in https://stackoverflow.com/a/17913254/1590415

    val rb = getClass.getClassLoader.getResource("webapp").toExternalForm
    context.setResourceBase(rb)
    
  2. put the resources in a folder on the filesystem

    val rb = "webapp"
    context.setResourceBase(rb)
    
  3. use the scalatra-sbt plugin, it has a dist task which creates a .zip distribution. For a working project see here. You can call the task by typing dist on the SBT prompt.

    val myDistSettings = DistPlugin.distSettings ++ Seq(
      mainClass in Dist := Some("ScalatraLauncher"),
      memSetting in Dist := "2g",
      permGenSetting in Dist := "256m",
      envExports in Dist := Seq("LC_CTYPE=en_US.UTF-8", "LC_ALL=en_US.utf-8"),
      javaOptions in Dist ++= Seq("-Xss4m", "-Dfile.encoding=UTF-8")
    )
    

In my experience Jetty loads web resources from a .jar really slow, so you may be better off putting them in a folder on the filesystem. Option 2 and 3 both load the resources from the filesystem.