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.html
I use IntelliJ IDEA with the Scala plugin for development and sbt to build and run my servlet (I used sbt-idea to generate the project files). My problem is that the jetty packages that the JettyLauncher in the tutorial uses cannot be found when I try to compile my project.
UPDATE: Using Matt's answer I was able to compile and run the JettyLauncher. However, I still have problems with sbt-assembly (https://github.com/sbt/sbt-assembly). I followed the instruction in the readme, but I get the following error when trying to execute the assembly task:
[error] Not a valid command: assembly
[error] No such setting/task
[error] assembly
[error] ^
UPDATE 2: Thanks to Matt I now have a working build.scala and I can generate a executable jar using the assembly task. However, sbt-assembly does not add the content of /src/main/webapp to the jar. I use this folder to store my HTML, CSS, and JavaScript files. If Scalatra can't match a route, it serves these files, which works when running the servlet using container:start. Additionally, I store some files that the server needs in /src/main/webapp/WEB-INF. These files are also not added to the jar.
My build.scala looks like this:
import sbt._
import Keys._
import org.scalatra.sbt._
import org.scalatra.sbt.PluginKeys._
import com.mojolly.scalate.ScalatePlugin._
import ScalateKeys._
import sbtassembly.Plugin._
import AssemblyKeys._
object SketchlinkBuild extends Build {
val Organization = "de.foobar"
val Name = "Foobar"
val Version = "0.1"
val ScalaVersion = "2.10.0"
val ScalatraVersion = "2.2.0"
lazy val project = Project (
"foobar",
file("."),
settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ assemblySettings ++ Seq(
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
resolvers += Classpaths.typesafeReleases,
libraryDependencies ++= Seq(
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
"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")),
/* Apache commons libraries */
"commons-codec" % "commons-codec" % "1.7",
"commons-io" % "commons-io" % "2.4",
/* JSON support */
"org.scalatra" %% "scalatra-json" % "2.2.1",
"org.json4s" %% "json4s-jackson" % "3.2.4",
/* thumbnail library */
"net.coobird" % "thumbnailator" % "0.4.3"
),
// ignore about.html in jars (needed for sbt-assembly)
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) => {
case "about.html" => MergeStrategy.discard
case x => old(x) }
},
scalateTemplateConfig in Compile <<= (sourceDirectory in Compile){ base =>
Seq(
TemplateConfig(
base / "webapp" / "WEB-INF" / "templates",
Seq.empty, /* default imports should be added here */
Seq(
Binding("context", "_root_.org.scalatra.scalate.ScalatraRenderContext", importMembers = true, isImplicit = true)
), /* add extra bindings here */
Some("templates")
)
)
}
)
)
}
Thanks in advance!
I recently ran into trouble doing this.
First, you need to make sure that jetty is available at compile time. These two lines:
Need to have compile in them:
Second, from your description it sounds like sbt-assembly is not configured correctly. You need to remove (comment out) these lines:
You will need to add
++ assemblySettings
to your foobar project immediately afterscalateSettings
. Your plugins.sbt file also needs to contain the following line in it: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:
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.
There are two options of standalone deployment currently:
scalatra-sbt
plugin, contains a start shell script, the runtime resources and the webapp resources in folders.1. Standalone JAR
For a standalone .jar file using
sbt-assembly
you need to add the plugin first toproject/build.sbt
:Then you need to modify the project build, e.g.
project/build.scala
. Import the plugin's settings and keys:With that you can create settings for the
sbt-assembly
plugin:The first defines a merge strategy, the last one copies the static web resources from
src/main/webapp
to<resourceManaged>/main/webapp
. They will be included in the final .jar in a sub-folder/webapp
.Include the settings in your project:
Now the launcher needs to be created. Note how the resource base is set:
2. .zip Distribution using
scalatra-sbt
PluginYou need to add those imports to your SBT
build.scala
:Then you need to add the plugin's settings to your project. The settings are in
DistPlugin.distSettings
.You can also customize your distribution and add custom memory settings, exports and command line options. Note that those are all optional:
On the SBT prompt you can then type
dist
. The .zip file will be in thetarget
folder.For an up to date answer, please refer to these two files (Credits go to Scalatra in Action book):
https://github.com/scalatra/scalatra-in-action/blob/master/chapter09-standalone/src/main/scala/ScalatraLauncher.scala
and
https://github.com/scalatra/scalatra-in-action/blob/master/chapter09-standalone/project/Build.scala