改变Scalatra端口

时间:2013-07-25 10:00:52

标签: scala port sbt scalatra

这听起来很基本,但它实际上花了我一整天的时间:我想改变以改变scalatra在开发中运行的端口。我开始使用hello world g8模板,并从那里开始构建。

这是我到目前为止所尝试的内容:

更改build.scala中的端口,ala文档: http://www.scalatra.org/guides/deployment/configuration.html 这不能编译,因为端口未定义。

更改build.scala中的端口,这两个例子: https:gist.github.com打瞌睡58af6cfbfe721a562a48 https://github.com/JamesEarlDouglas/xsbt-web-plugin/blob/master/src/sbt-test/web/servlet/project/Build.scala 同样的问题:端口未定义

重新定义入口点,ala http:www.scalatra.org指导部署standalone.html 仍在端口8080上运行

在bootstrap中更改init参数,ala http:www.scalatra.org指导部署配置 仍在端口8080上运行

任何帮助表示赞赏。由于某种原因,我不能发布超过2个链接,所以用正斜杠替换空格以跟随网址。

这是我的build.scala,以防万一。

import sbt._
import Keys._
import org.scalatra.sbt._
import org.scalatra.sbt.PluginKeys._
import com.mojolly.scalate.ScalatePlugin._
import ScalateKeys._
import com.earldouglas.xsbtwebplugin._
import WebPlugin._

object YesManBuild extends Build {
  val Organization = "com.prezi"
  val Name = "Yes Man"
  val Version = "0.1.0-SNAPSHOT"
  val ScalaVersion = "2.10.2"
  val ScalatraVersion = "2.2.1"

  //def Conf = config("container")

  lazy val project = Project (
    "yes-man",
    file("."),
    settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
      //port in Conf := 8081,
      mainClass := Some("com.prezi.eureka.JettyLauncher.main"),
      organization := Organization,
      name := Name,
      version := Version,
      scalaVersion := ScalaVersion,
      resolvers += Classpaths.typesafeReleases,
      libraryDependencies ++= Seq(
        "org.slf4j" % "slf4j-log4j12" % "1.7.5",
        "com.netflix.eureka" % "eureka-client" % "1.1.97",
        "com.netflix.ribbon" % "ribbon-httpclient" % "0.1.10",
        "com.netflix.ribbon" % "ribbon-eureka" % "0.1.11",
        "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" % "container",
        "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")),
        "org.eclipse.jetty.aggregate" % "jetty-all" % "9.0.4.v20130625"
      ),
      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")
          )
        )
      }
    )
  )
}

谢谢你们,

〜埃里克

3 个答案:

答案 0 :(得分:15)

2016年1月23日更新:scalatra-sbt使用xsbt-web-plugin 2.0.4并更改了一些设置。您可以找到xsbt-web-plugin docs here(相关说明:xsbt-web-plugin,sbt-web,docker和独立版本的集成可以在https://github.com/scalatra/scalatra-in-action中找到,请参阅chapter09- *目录)。

对于Scalatra应用程序,这意味着:

  • 使用jetty:startjetty:stop代替容器:start,container:stop
  • 启用JettyPlugin
  • 使用新密钥,例如containerPort in Jetty := 8090target in webappPreparesourceDirectory in webappPrepare
  • 仅基于.scala的SBT构建定义:使用正确的导入来导入插件和设置键

.sbt构建定义:

organization := "org.foo"
name := "My build"
version := "0.1.0-SNAPSHOT"
scalaVersion := "2.11.6"

val ScalatraVersion = "2.4.0"

libraryDependencies ++= Seq(
  "org.scalatra" %% "scalatra" % ScalatraVersion,
  "org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
  "com.typesafe" % "config" % "1.2.1",
  "ch.qos.logback" % "logback-classic" % "1.1.3" % "runtime",
  "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
)

enablePlugins(JettyPlugin)

containerPort in Jetty := 8090

基于.scala的SBT构建定义确实没那么神奇,我们需要导入插件及其设置:

import sbt._
import Keys._

import org.scalatra.sbt._

import com.earldouglas.xwp.JettyPlugin
import com.earldouglas.xwp.JettyPlugin.autoImport._
import com.earldouglas.xwp.ContainerPlugin.autoImport._

enablePlugins(JettyPlugin)的实际构建定义和自定义端口:

object MyBuild extends Build {
  val Organization = "org.foo"
  val Name = "My Build"
  val Version = "0.1.0-SNAPSHOT"
  val ScalaVersion = "2.11.6"
  val ScalatraVersion = "2.4.0"

  val mySettings =
    ScalatraPlugin.scalatraSettings ++ Seq(
      organization := Organization,
      name := Name,
      version := Version,
      scalaVersion := ScalaVersion,
      libraryDependencies ++= Seq(
        "org.scalatra" %% "scalatra" % ScalatraVersion,
        "org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
        "com.typesafe" % "config" % "1.2.1",
        "ch.qos.logback" % "logback-classic" % "1.1.3" % "runtime",
        "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
      ),
      containerPort in Jetty := 8090
    )

  lazy val project = Project("chapter09", file("."))
    .enablePlugins(JettyPlugin)
    .settings(mySettings: _*)

}

确保您使用的是导入:

import com.earldouglas.xsbtwebplugin.PluginKeys._
import com.earldouglas.xsbtwebplugin.WebPlugin._

使用这些导入,您可以使用正确的密钥和配置:

port in container.Configuration := 9000

这是settings块:

...
lazy val project = Project (
  "example",
  file("."),
  settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
    port in container.Configuration := 9000,
    organization := Organization,
...

scalatra-sbt以xsbt-web-plugin为基础,其设置记录在此处:https://github.com/JamesEarlDouglas/xsbt-web-plugin/wiki/Settings

答案 1 :(得分:1)

我这样做并且工作了:

....
val port = SettingKey[Int]("port")
val Conf = config("container")

lazy val project = Project (
    port in Conf := 8081,
    ....
)

答案 2 :(得分:0)

我在项目的根目录中创建了一个空的build.sbt文件并放入:

port in container.Configuration := 8090

在文件中供它工作。