How do I run an sbt main class from the shell as normal command-line program?

旧街凉风 提交于 2019-11-27 11:35:33

The start-script SBT plugin is now at:

https://github.com/sbt/sbt-start-script

It requires a few steps to set up and generates scripts that do not work on OS X, but that can be easily fixed if you're on that platform (see below).

Setup

  1. Install greadlink (OS X only):

    a) brew install coreutils

    b) map readlink to the new function (greadlink) by adding these lines to ~/.bashrc:

    function readlink() { greadlink "$@"; }

    export -f readlink`

  2. Add start-script plugin to ~/.sbt/plugins/build.sbt:

    addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.8.0")

  3. Add start-script task to current project:

    $ sbt add-start-script-tasks # execute from directory where build.sbt resides

  4. Add start-script support to current build.sbt:

    import com.typesafe.sbt.SbtStartScript

    seq(SbtStartScript.startScriptForClassesSettings: _*)

Note the blank line in between statements (de rigueur for SBT build files).

Generate Start Script

Then, whenever you want to create a script to start your app like sbt run-main, but without sbt, execute:

$ sbt start-script

Run

target/start mypackage.MyMainClass

Here's what I have in my SBT (version 0.10) project definition,

  val Mklauncher = config("mklauncher") extend(Compile)
  val mklauncher = TaskKey[Unit]("mklauncher")
  val mklauncherTask = mklauncher <<= (target, fullClasspath in Runtime) map { (target, cp) =>
    def writeFile(file: File, str: String) {
      val writer = new PrintWriter(file)
      writer.println(str)
      writer.close()
    }
    val cpString = cp.map(_.data).mkString(":")
    val launchString = """
CLASSPATH="%s"
scala -usejavacp -Djava.class.path="${CLASSPATH}" "$@"
""".format(cpString)
    val targetFile = (target / "scala-sbt").asFile
    writeFile(targetFile, launchString)
    targetFile.setExecutable(true)
  }

  ... // remember to add mklauncherTask to Project Settings

The mklauncher task creates a script target/scala-sbt that executes scala with the project classpath already set. It would be nice to have mklauncher executed automatically whenever the classpath changes, but I haven't looked into doing this yet.

(I use the Java classpath, rather than Scala's, for ease of creating embedded interpreters.)

Time flies by and a lot have changed since the other answers. It's currently SBT 0.13.6 time.

I think what you may need is the sbt-onejar plugin or the SBT Native Packager plugin.

sbt-onejar "is a simple-build-tool plugin for building a single executable JAR containing all your code and dependencies as nested JARs."

SBT Native Packager's "goal is to be able to bundle up Scala software built with SBT for native packaging systems, like deb, rpm, homebrew, msi."

Just discovered the sbt start script plugin: https://github.com/typesafehub/xsbt-start-script-plugin:

This plugin allows you to generate a script target/start for a project. The script will run the project "in-place" (without having to build a package first).

The target/start script is similar to sbt run but it doesn't rely on SBT. sbt run is not recommended for production use because it keeps SBT itself in-memory. target/start is intended to run an app in production.

The plugin adds a task start-script which generates target/start. It also adds a stage task, aliased to the start-script task.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!