Created
November 21, 2012 15:27
-
-
Save meniku/4125432 to your computer and use it in GitHub Desktop.
Embbedding a jetty Server Patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 491b16f4fd6c5ec319fc6b756743a6deee756478 Mon Sep 17 00:00:00 2001 | |
From: "nils.kuebler" <nils.kuebler@memonews.com> | |
Date: Wed, 21 Nov 2012 16:26:09 +0100 | |
Subject: [PATCH] add 'assembly' task which builds a jar with embedded jetty | |
--- | |
README.md | 7 ++++ | |
build.sbt | 37 ++++++++++++++++++-- | |
project/plugins.sbt | 2 ++ | |
.../com/memonews/detectr/run/AppLauncher.scala | 33 +++++++++++++++++ | |
4 files changed, 76 insertions(+), 3 deletions(-) | |
create mode 100644 src/main/scala/com/memonews/detectr/run/AppLauncher.scala | |
diff --git a/README.md b/README.md | |
index 6fb33f6..1c5072b 100644 | |
--- a/README.md | |
+++ b/README.md | |
@@ -31,4 +31,11 @@ $ ./sbt package | |
... | |
+``` | |
+ | |
+# ASSEMBLY | |
+ | |
+```sh | |
+$ ./sbt assembly | |
+$ java -jar target/Detectr-assembly-0.1.0-SNAPSHOT.jar | |
``` | |
\ No newline at end of file | |
diff --git a/build.sbt b/build.sbt | |
index 53f30fe..0d03388 100644 | |
--- a/build.sbt | |
+++ b/build.sbt | |
@@ -15,9 +15,40 @@ libraryDependencies ++= Seq( | |
"org.scalatra" % "scalatra-scalate" % "2.1.1", | |
"org.scalatra" % "scalatra-specs2" % "2.1.1" % "test", | |
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime", | |
- "org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container", | |
- "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")), | |
+ "org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "compile,container", | |
+ "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" artifacts (Artifact("javax.servlet", "jar", "jar")), | |
"com.cybozu.labs" % "langdetect" % "1.1-20120112", | |
"commons-io" % "commons-io" % "2.0", | |
"org.scalatra" % "scalatra-lift-json" % "2.1.1" | |
-) | |
\ No newline at end of file | |
+) | |
+ | |
+// Settings for building JAR with embedded Jetty | |
+ | |
+assemblySettings | |
+ | |
+mainClass in AssemblyKeys.assembly := Some("com.memonews.detectr.run.AppLauncher") | |
+ | |
+test in AssemblyKeys.assembly := {} | |
+ | |
+// Exclude about.html which causes problems | |
+ | |
+AssemblyKeys.mergeStrategy in AssemblyKeys.assembly <<= (AssemblyKeys.mergeStrategy in AssemblyKeys.assembly) { (old) => | |
+ { | |
+ case "about.html" => MergeStrategy.discard | |
+ case x => old(x) | |
+ } | |
+} | |
+ | |
+// Copy webapp Resources to resource directory | |
+ | |
+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 | |
+ } | |
+} | |
\ No newline at end of file | |
diff --git a/project/plugins.sbt b/project/plugins.sbt | |
index 0377c60..2aee5f3 100644 | |
--- a/project/plugins.sbt | |
+++ b/project/plugins.sbt | |
@@ -7,3 +7,5 @@ libraryDependencies <+= sbtVersion(v => v match { | |
}) | |
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0") | |
+ | |
+addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.8.4") | |
diff --git a/src/main/scala/com/memonews/detectr/run/AppLauncher.scala b/src/main/scala/com/memonews/detectr/run/AppLauncher.scala | |
new file mode 100644 | |
index 0000000..3764fab | |
--- /dev/null | |
+++ b/src/main/scala/com/memonews/detectr/run/AppLauncher.scala | |
@@ -0,0 +1,33 @@ | |
+package com.memonews.detectr.run | |
+ | |
+import org.eclipse.jetty.server.Server | |
+import org.eclipse.jetty.webapp.WebAppContext | |
+import org.slf4j.LoggerFactory | |
+ | |
+object AppLauncher { | |
+ def main(args: Array[String]) { | |
+ val thisArgs = if (0 == args.size) { | |
+ // Default Arguments: | |
+ Array("8080") | |
+ } else args | |
+ | |
+ val Array(port) = thisArgs | |
+ | |
+ val server = new Server(port.toInt) | |
+ | |
+ val context = getClass.getClassLoader.getResource("webapp").toExternalForm | |
+ | |
+ val root = new WebAppContext(context, "/") | |
+ | |
+ Runtime.getRuntime.addShutdownHook(new Thread() { | |
+ override def run() { | |
+ server.stop() | |
+ } | |
+ }) | |
+ | |
+ server.setHandler(root) | |
+ server.start() | |
+ server.join() | |
+ | |
+ } | |
+} | |
\ No newline at end of file | |
-- | |
1.7.9.6 (Apple Git-31.1)+GitX |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment