Tiven Wang
Wang Tiven May 09, 2018
425 favorite favorites
bookmark bookmark
share share

如何部署 Scala 应用程序到 CloudFoundry 平台?

Buildpack for Scala

可以使用 Heroku Buildpack for Scala 作为自定义的 Buildpack 来发布 Scala 应用程序

---
applications:
- name: play-scala-starter
  memory: 512M
  instances: 1
  host: play-scala-starter
  path: .
  buildpack: https://github.com/heroku/heroku-buildpack-scala.git

How It Works

The buildpack will detect your app as Scala if it has

  • /*.sbt
  • /project/*.scala
  • /project/build.properties
  • /.sbt/*.scala

. It vendors a version of sbt into your slug (if you are not using sbt-native-packager, it also includes your popluated .ivy/cache in the slug). The .ivy2 directory will be cached between builds to allow for faster build times.

It is strongly recommended that you use sbt-native-packager with this buildpack instead of sbt-start-script. The latter is deprecated, and will result in exessively large slug sizes.

Build behavior

Applications must include a /project/build.properties file with the sbt.version property specifying a version of SBT between 0.11.0 and 1.x. SBT release candidates, betas and other pre-release versions are allowed.

The Heroku Scala buildpack will run sbt compile stage to build the application. Applications must include a stage task, which performs any tasks needed to prepare an application to be run in-place. For example, Typesafe’s sbt-native-packager adds a stage task to SBT that generates start scripts for an application. To use the plugin, create a /project/plugins.sbt file containing:

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.4")

Or you can write a custom stage task by putting something like this in your build.sbt:

val stage = taskKey[Unit]("Stage task")

val Stage = config("stage")

stage := {
  (packageWar in Compile).value
  (update in Stage).value.allFiles.foreach { f =>
    if (f.getName.matches("webapp-runner-[0-9\\.]+.jar")) {
      println("copying " + f.getName)
      IO.copyFile(f, baseDirectory.value / "target" / "webapp-runner.jar")
    }
  }
}

You can test your task by running sbt compile stage locally.

Runtime behavior

By default, Scala applications are launched with a start script generated by the sbt-native-packager:

web: target/universal/stage/bin/appname

If an application is not using the sbt-native-packager or should be launched in a different way, a custom Procfile can be included in the root of the project specifying a different entry for the web process.

The Play framework automatically generates a start script for you, so no additional plugins are needed.

Java Buildpack

如果你打算使用纯的 Java Buildpack 的话,可以做以下步骤

  • Add the sbt-native-packager to your project
  • Execute the action universal:packageBin building by hand or configure your build server to do so
  • Change the buildpack in the manifest.yml and add some parameters, if necessary. Configure the path of the artifact to deploy

    ---
    applications:
    - name: play-scala-starter
      memory: 1024M
      instances: 1
      host: play-scala-starter
      path: target/universal/play-scala-starter-example-1.0-SNAPSHOT.zip
      buildpack: https://github.com/cloudfoundry/java-buildpack.git
    
  • Run cf push or let the build server do so.

Similar Posts

Comments

Back to Top