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

前端 未结 4 1676
轻奢々
轻奢々 2020-12-02 15:00

How can I run an sbt app from the shell, so that I can run my app as a normal command-line program (as if run directly via scala but without having to set up an

相关标签:
4条回答
  • 2020-12-02 15:06

    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
    
    0 讨论(0)
  • 2020-12-02 15:23

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

    0 讨论(0)
  • 2020-12-02 15:23

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

    0 讨论(0)
  • 2020-12-02 15:30

    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.

    0 讨论(0)
提交回复
热议问题