简体   繁体   中英

How to run a sbt-managed application project without sbt?

My question is about running an application that's in a project managed by sbt 0.10.1 and hence relies on its automatic dependency management (to download and set up appropriate classpath to run).

When using the automatic dependency management, it appears that the only way to run the application is using sbt itself because it knows how to set up the classpath (with a help of Ivy2).

How can I run the application without sbt?

You can also use Typesafe's xsbt-start-script-plugin (edit: now sbt-start-script ) to generate a shell script with the correct class path:

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.

This is what Heroku uses to run Scala apps.

I've used retronym 's sbt-onejar plugin to create executable jars with all dependencies included (much like the Assembly plugin for Maven). It's very simple and well documented.

If you want to create a 'fat' jar containing all your application and your dependencies, you can use the sbt-assembly plugin . Then you can run your application as a standard jar without sbt.

You can also create a task to create a file to launch the app. @Kipton Barros posted this in How do I run an sbt main class from the shell as normal command-line program? :

  val MkUnixlauncher = config("mkunixlauncher") extend(Compile)
  val mkunixlauncher = TaskKey[Unit]("mkunixlauncher")
  val mkunixlauncherTask = mkunixlauncher <<= (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(System.getProperty("path.separator"))
    val launchString = """
CLASSPATH="%s"
scala -usejavacp -Djava.class.path="${CLASSPATH}" "$@"
""".format(cpString)
    val targetFile = (target / "scala-sbt").asFile
    writeFile(targetFile, launchString)
    targetFile.setExecutable(true)
  }

That creates a file named scala-sbt in your target directory that has the classpath set properly to run the app. Tweak to taste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM