Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Scalatra Port

This sounds basic, but its actually cost me a whole day: I want to change to change the port that scalatra runs on, in development. I started with the hello world g8 template, and have been building from there.

Here's what I've tried so far:

Changing the port in build.scala, ala documentation: http://www.scalatra.org/guides/deployment/configuration.html This doesn't compile, because port is undefined.

Changing the port in build.scala, ala these two examples: https: gist.github.com dozed 58af6cfbfe721a562a48 https://github.com/JamesEarlDouglas/xsbt-web-plugin/blob/master/src/sbt-test/web/servlet/project/Build.scala Same problem: port is undefined

Redefining the entry point, ala http: www.scalatra.org guides deployment standalone.html Still runs on port 8080

Changing init params in bootstrap, ala http: www.scalatra.org guides deployment configuration.html Still runs on port 8080

Any help appreciated. I can't post more than 2 links for some reason, so replace spaces with forward slashes to follow the urls.

Here's my build.scala in case it helps.

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")
          )
        )
      }
    )
  )
}

Thanks guys,

~Erik

like image 871
Kire321 Avatar asked Jul 25 '13 10:07

Kire321


1 Answers

Update 23.01.2016: scalatra-sbt uses xsbt-web-plugin 2.0.4 and a few settings changed. You can find the xsbt-web-plugin docs here (related note: integrations for xsbt-web-plugin, sbt-web, docker and standalone builds can be found in https://github.com/scalatra/scalatra-in-action, see chapter09-* directories).

For a Scalatra app this means:

  • use jetty:start, jetty:stop instead container:start, container:stop
  • enable JettyPlugin
  • use new keys, e.g. containerPort in Jetty := 8090, target in webappPrepare, sourceDirectory in webappPrepare
  • only .scala based SBT build definition: use the correct imports to import the plugin and setting keys

A .sbt build definition:

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

A .scala based SBT build definition does have bit less magic, and we need to import the plugin and its settings:

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._

The actual build definition with enablePlugins(JettyPlugin) and a custom port:

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: _*)

}

Make sure you are using the imports:

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

With those imports you can use then the correct key and configuration:

port in container.Configuration := 9000

This goes in the settings block:

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

scalatra-sbt builds on xsbt-web-plugin whose settings are documented here: https://github.com/JamesEarlDouglas/xsbt-web-plugin/wiki/Settings

like image 156
Stefan Ollinger Avatar answered Sep 27 '22 16:09

Stefan Ollinger