简体   繁体   English

如何在没有sbt的情况下运行sbt管理的应用程序项目?

[英]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). 我的问题是关于运行由sbt 0.10.1管理的项目中的应用程序,因此依赖于它的自动依赖管理(下载并设置适当的类路径来运行)。

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). 使用自动依赖关系管理时, 似乎运行应用程序的唯一方法是使用sbt本身,因为它知道如何设置类路径(在Ivy2的帮助下)。

How can I run the application without sbt? 如何在没有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: 您还可以使用Typesafe的xsbt-start-script-plugin (编辑:now sbt-start-script )生成具有正确类路径的shell脚本:

This plugin allows you to generate a script target/start for a project. 此插件允许您为项目生成脚本target/start 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. target/start脚本类似于sbt run但它不依赖于SBT。 sbt run is not recommended for production use because it keeps SBT itself in-memory. sbt run不建议用于生产用途,因为它将SBT本身保留在内存中。 target/start is intended to run an app in production. target/start旨在运行生产中的应用程序。

The plugin adds a task start-script which generates target/start . 该插件添加了一个生成target/start的任务start-script It also adds a stage task, aliased to the start-script task. 它还添加了一个stage任务,别名为start-script任务。

This is what Heroku uses to run Scala apps. 这是Heroku用于运行Scala应用程序的内容。

I've used retronym 's sbt-onejar plugin to create executable jars with all dependencies included (much like the Assembly plugin for Maven). 我用返璞词SBT-onejar插件具有所有依赖性包括(很像创建可执行罐子Assembly插件 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 . 如果要创建包含所有应用程序和依赖项的“胖”jar,可以使用sbt-assembly插件 Then you can run your application as a standard jar without sbt. 然后,您可以将应用程序作为标准jar而无需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? @Kipton Barros在如何从shell运行sbt主类作为普通的命令行程序? :

  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. 这会在目标目录中创建一个名为scala-sbt的文件,该文件已正确设置类路径以运行应用程序。 Tweak to taste. 调整味道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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