Standalone deployment of Scalatra servlet

后端 未结 3 428
梦谈多话
梦谈多话 2020-12-16 03:18

I implemented a Scalatra servlet and now want to create an executable jar, just like described in this tutorial: http://www.scalatra.org/2.2/guides/deployment/standalone.htm

3条回答
  •  轮回少年
    2020-12-16 03:58

    I recently ran into trouble doing this.

    First, you need to make sure that jetty is available at compile time. These two lines:

    "org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container",
    "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" %     "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")),
    

    Need to have compile in them:

    "org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "compile;container",
    "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "compile;container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
    

    Second, from your description it sounds like sbt-assembly is not configured correctly. You need to remove (comment out) these lines:

    lazy val buildSettings = Defaults.defaultSettings ++ Seq(
      version := "0.1",
      organization := "de.foobar",
      scalaVersion := "2.10.1"
    )
    
    lazy val app = Project("app", file("app"),
      settings = buildSettings ++ assemblySettings) settings(
        // your settings here
    )
    

    You will need to add ++ assemblySettings to your foobar project immediately after scalateSettings. Your plugins.sbt file also needs to contain the following line in it:

    addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.9.0")
    

    For reference, I recommend against using sbt-assembly because you will most likely run into dependency conflicts that will need to be resolved with a merge strategy. Instead I suggest you use a task that collects your dependencies into a directory (examples here and here). And then add them to the java classpath using java -cp /lib/* ....

    Third, be wary of the Jetty project in Scalatra's GitHub. I used:

    import java.net.InetSocketAddress
    import org.eclipse.jetty.server.Server
    import org.eclipse.jetty.servlet.DefaultServlet
    
    import org.scalatra.servlet.ScalatraListener
    import org.eclipse.jetty.webapp.WebAppContext
    
    object Jetty {
      def main(args: Array[String]) = {
        val socketAddress = new InetSocketAddress(8080)
        val server = new Server(socketAddress)
        val context = new WebAppContext()
        context.setContextPath("/")
        context.setResourceBase("src/main/webapp")
        context.addEventListener(new ScalatraListener)
        context.addServlet(classOf[DefaultServlet], "/")
        server.setHandler(context)
        server.start()
        server.join()
      }
    }
    

    Finally, it might be worth double checking your ScalatraBootstrap is in the usual place.

    Hope that helps. If not I can post my entire build.scala for you.

提交回复
热议问题