我第一次使用scalatra 2.6.3和scala 2.11.8开发了一个Web应用程序,并且它没有使用jar执行。以下是我在build.sbt
中的依赖项scalaVersion := "2.11.8"
val scalatraVersion = "2.6.3"
dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-core" % "2.9.4"
dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-databind" % "2.9.4"
dependencyOverrides += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.4"
libraryDependencies ++= Seq(
"com.microsoft.sqlserver" % "mssql-jdbc" % "6.4.0.jre8", //Microsoft JDBC Driver For SQL Server
"com.typesafe.play" %% "play-json" % "2.5.10",
"com.typesafe" % "config" % "1.2.1", //Configuration
"org.scalaj" %% "scalaj-http" % "2.3.0" % "compile",
"org.apache.spark" %% "spark-core" % "2.2.0",
"org.apache.spark" %% "spark-sql" % "2.2.0",
"org.scala-lang" % "scala-library" % "${scala.version}",
"org.apache.lucene" % "lucene-queryparser" % "7.2.1",
"org.apache.lucene" % "lucene-queries" % "7.2.1",
"org.apache.lucene" % "lucene-analyzers-common" % "7.2.1",
"org.apache.lucene" % "lucene-codecs" % "7.2.1" ,
"com.microsoft.azure" % "azure-storage" % "3.1.0",
"com.microsoft.azure" % "azure-eventhubs" % "0.6.6",
"org.apache.hadoop" % "hadoop-azure" % "2.7.0",
"com.github.scala-incubator.io" %% "scala-io-file" % "0.4.3-1",
"com.amazonaws" % "aws-java-sdk-logs" % "1.10.40",
//Logging
"org.slf4j" % "slf4j-api" % "1.7.25",
"ch.qos.logback" % "logback-classic" % "1.2.3",
"org.scalatra" %% "scalatra" % scalatraVersion,
"org.scalatra" %% "scalatra-scalatest" % scalatraVersion % "test",
"org.eclipse.jetty" % "jetty-webapp" % "9.4.10.RC1",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
"org.scalatra" %% "scalatra-scalate" % scalatraVersion
)
unmanagedResourceDirectories in Compile += { baseDirectory.value / "src/main/webapp" }
enablePlugins(ScalatraPlugin)
assemblyMergeStrategy in assembly := {
case PathList("META-INF", xs @ _*) =>
(xs map {_.toLowerCase}) match {
case "services" :: xs =>
MergeStrategy.first
case _ => MergeStrategy.discard
}
case x => MergeStrategy.first
}
我知道scalatra在内部使用jetty来运行App,我在项目中设置了webapp目录(src / main / webapp /),我可以使用intellij在本地运行App但是当我从jar文件运行它时(使用sbt assembly)然后它找不到web app文件夹。以下是主应用程序的代码。
val server = {
val listenPort = if (System.getenv.containsKey("PORT")) System.getenv.get("PORT").toShort else ConfigFactory.load.getString("api.port_num").toShort;
logger.info("Listen Port: " + listenPort)
new Server(listenPort)
}
val context: WebAppContext = new WebAppContext();
val webappDirLocation = "src/main/webapp/"
context.setServer(server)
context.setContextPath("/");
context.setDescriptor(webappDirLocation + "/WEB-INF/web.xml")
context.setResourceBase(webappDirLocation)
server.setHandler(context);
try {
server.start()
server.join()
} catch {
case e: Exception => logger.error(s"Error occurred while starting App : $e")
throw new RuntimeException(s"Unable to start App : $e")
}
请建议如何解决此错误,我完全坚持这一点。 尝试以下步骤来解决它,但没有运气: -set war location使用context.setWar方法。 - 在上下文中设置之前,保留src / main / webapp的绝对路径
提前致谢!
答案 0 :(得分:1)
如果您的程序集jar文件包含/WEB-INF/web.xml
,则可以按如下方式设置WebAppContext
:
val context: WebAppContext = new WebAppContext()
val webXml = getClass.getResource("/WEB-INF/web.xml")
val webappDirLocation = if(webXml != null){
// running the assembly jar
webXml.toString.replaceFirst("/WEB-INF/web.xml$", "/")
} else {
// running on the source tree
"src/main/webapp/"
}
context.setResourceBase(webappDirLocation)
context.setDescriptor(webappDirLocation + "WEB-INF/web.xml")
在上面的示例中,配置已切换,因为它需要不同的配置才能在源树上运行并运行程序集jar。
此外,请注意,您需要在build.sbt
中添加以下行,以便通过sbt-assembly-plugin将webapp/*
包含到您的程序集jar文件中:
unmanagedResourceDirectories in Compile += { baseDirectory.value / "src/main/webapp" }