Scalatra servlet的独立部署 [英] Standalone deployment of Scalatra servlet
问题描述
我实现了Scalatra servlet,现在想创建一个可执行jar,就像本教程中所述: http://www.scalatra.org/2.2/guides/deployment/standalone.html
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
我将IntelliJ IDEA与Scala插件一起用于开发和sbt来构建和运行我的servlet(我使用sbt-idea生成了项目文件). 我的问题是,当我尝试编译项目时,找不到本教程中JettyLauncher使用的码头软件包.
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.
更新:使用Matt的答案,我能够编译并运行JettyLauncher.但是,我仍然遇到sbt-assembly问题( https://github.com/sbt/sbt-assembly ).我按照自述文件中的说明进行操作,但是在尝试执行汇编任务时出现以下错误:
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] ^
更新2:多亏了Matt,我现在有了一个可以正常工作的build.scala,并且我可以使用汇编任务来生成一个可执行jar.但是,sbt-assembly不会将/src/main/webapp的内容添加到jar中. 我使用此文件夹存储我的HTML,CSS和JavaScript文件. 如果Scalatra无法匹配路由,则它将提供这些文件,这些文件在使用container:start运行servlet时起作用. 此外,我将服务器需要的一些文件存储在/src/main/webapp/WEB-INF中.这些文件也不会添加到jar中.
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.
我的build.scala看起来像这样:
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")
)
)
}
)
)
}
提前谢谢!
推荐答案
当前有两种独立部署选项:
There are two options of standalone deployment currently:
- 使用sbt-assembly单个.jar,其中包含运行时和webapp资源.从我的经验来看,从.jar文件加载资源相当慢.
- 使用
scalatra-sbt
插件分发.zip文件,该文件在文件夹中包含一个启动Shell脚本,运行时资源和Webapp资源.
- Single .jar using sbt-assembly which contains runtime and webapp resources. Loading resources from the .jar file is quite slow in my experience.
- Distribution .zip file using
scalatra-sbt
plugin, contains a start shell script, the runtime resources and the webapp resources in folders.
1.独立的JAR
对于使用sbt-assembly
的独立.jar文件,您需要先将该插件添加到project/build.sbt
:
1. Standalone JAR
For a standalone .jar file using sbt-assembly
you need to add the plugin first to project/build.sbt
:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.9.0")
然后您需要修改项目版本,例如project/build.scala
.导入插件的设置和键:
Then you need to modify the project build, e.g. project/build.scala
. Import the plugin's settings and keys:
import sbtassembly.Plugin._
import sbtassembly.Plugin.AssemblyKeys._
使用它可以为sbt-assembly
插件创建设置:
With that you can create settings for the sbt-assembly
plugin:
// settings for sbt-assembly plugin
val myAssemblySettings = assemblySettings ++ Seq(
// handle conflicts during assembly task
mergeStrategy in assembly <<= (mergeStrategy in assembly) {
(old) => {
case "about.html" => MergeStrategy.first
case x => old(x)
}
},
// copy web resources to /webapp folder
resourceGenerators in Compile <+= (resourceManaged, baseDirectory) map {
(managedBase, base) =>
val webappBase = base / "src" / "main" / "webapp"
for {
(from, to) <- webappBase ** "*" x rebase(webappBase, managedBase / "main" / "webapp")
} yield {
Sync.copy(from, to)
to
}
}
)
第一个定义合并策略,最后一个将静态Web资源从src/main/webapp
复制到<resourceManaged>/main/webapp
.它们将包含在子文件夹/webapp
中的最终.jar中.
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:
lazy val project = Project("myProj", file(".")).settings(mySettings: _*).settings(myAssemblySettings:_*)
现在需要创建启动器.请注意如何设置资源库:
Now the launcher needs to be created. Note how the resource base is set:
import org.eclipse.jetty.server.nio.SelectChannelConnector
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext
import org.scalatra.servlet.ScalatraListener
object JettyMain {
def run = {
val server = new Server
val connector = new SelectChannelConnector
connector.setPort(8080)
server.addConnector(connector)
val context = new WebAppContext
context.setContextPath("/")
val resourceBase = getClass.getClassLoader.getResource("webapp").toExternalForm
context.setResourceBase(resourceBase)
context.setEventListeners(Array(new ScalatraListener))
server.setHandler(context)
server.start
server.join
}
}
2. .zip使用scalatra-sbt
插件
分发
您需要将这些导入添加到您的SBT build.scala
:
2. .zip Distribution using scalatra-sbt
Plugin
You need to add those imports to your SBT build.scala
:
import org.scalatra.sbt.DistPlugin._
import org.scalatra.sbt.DistPlugin.DistKeys._
然后,您需要将插件的设置添加到您的项目中.设置在DistPlugin.distSettings
中.
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:
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")
)
在SBT提示符下,您可以键入dist
. .zip文件将位于target
文件夹中.
On the SBT prompt you can then type dist
. The .zip file will be in the target
folder.
这篇关于Scalatra servlet的独立部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!