scala 如何从 shell 作为普通命令行程序运行 sbt 主类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7134993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run an sbt main class from the shell as normal command-line program?
提问by Yang
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 scalabut without having to set up an enormous classpath)?
如何从 shell 运行 sbt 应用程序,以便我可以将我的应用程序作为普通命令行程序运行(就像直接通过scala但无需设置巨大的类路径一样)?
I know I can do:
我知道我可以做到:
echo hello | sbt 'run-main com.foo.MyMain3 arg1 arg2' > out.txt
But this (1) takes forever to start because it starts sbt, (2) causes all stdout and stderr to go to stdout, and (3) causes all output to be decorated with a logger [info] or [error].
但是这 (1) 需要永远启动,因为它启动 sbt,(2) 导致所有 stdout 和 stderr 转到 stdout,并且 (3) 导致所有输出都被记录器 [info] 或 [error] 修饰。
I looked at https://github.com/harrah/xsbt/wiki/Launcherbut it seems too heavyweight, since it downloads dependencies and sets up a new environment and whatnot. I just want to run this app within my existing development environment.
我查看了https://github.com/harrah/xsbt/wiki/Launcher但它似乎太重量级了,因为它下载依赖项并设置新环境等等。我只想在我现有的开发环境中运行这个应用程序。
Thus far I've cobbled together my own script to build up a classpath, and you can also do some other things like modify your project file to get sbt to print the raw classpath, but I feel like there must be a better way.
到目前为止,我已经拼凑了自己的脚本来构建类路径,您还可以执行其他一些操作,例如修改项目文件以使 sbt 打印原始类路径,但我觉得必须有更好的方法。
采纳答案by Brent Faust
The start-script SBT pluginis now at:
在启动脚本SBT插件现在是:
https://github.com/sbt/sbt-start-script
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).
它需要几个步骤来设置和生成不适用于 OS X 的脚本,但如果您在该平台上,则可以轻松修复(见下文)。
Setup
设置
Install greadlink (OS X only):
a)
brew install coreutilsb) map readlink to the new function (greadlink) by adding these lines to ~/.bashrc:
function readlink() { greadlink "$@"; }
export -f readlink`
Add start-script plugin to ~/.sbt/plugins/build.sbt:
addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.8.0")
Add start-script task to current project:
$ sbt add-start-script-tasks# execute from directory where build.sbt residesAdd start-script support to current build.sbt:
import com.typesafe.sbt.SbtStartScript
seq(SbtStartScript.startScriptForClassesSettings: _*)
安装 greadlink(仅限 OS X):
一个)
brew install coreutilsb)通过将这些行添加到 ~/.bashrc 来将 readlink映射到新函数 ( greadlink):
函数 readlink() { greadlink "$@"; }
导出 -f 阅读链接`
将启动脚本插件添加到 ~/.sbt/plugins/build.sbt:
addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.8.0")
将启动脚本任务添加到当前项目:
$ sbt add-start-script-tasks# 从 build.sbt 所在目录执行为当前的 build.sbt 添加启动脚本支持:
导入 com.typesafe.sbt.SbtStartScript
seq(SbtStartScript.startScriptForClassesSettings:_*)
Note the blank line in between statements (de rigueur for SBT build files).
注意语句之间的空行(SBT 构建文件的必要条件)。
Generate Start Script
生成启动脚本
Then, whenever you want to create a script to start your app like sbt run-main, but without sbt, execute:
然后,每当你想创建一个脚本来启动你的应用程序sbt run-main,但没有 sbt 时,执行:
$ sbt start-script
Run
跑
target/start mypackage.MyMainClass
回答by Kipton Barros
Here's what I have in my SBT (version 0.10) project definition,
这是我在 SBT(0.10 版)项目定义中的内容,
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 mklaunchertask creates a script target/scala-sbtthat executes scalawith the project classpath already set. It would be nice to have mklauncherexecuted automatically whenever the classpath changes, but I haven't looked into doing this yet.
该mklauncher任务创建一个脚本target/scala-sbt,该脚本scala在已设置的项目类路径下执行。mklauncher每当类路径发生变化时自动执行会很好,但我还没有考虑这样做。
(I use the Java classpath, rather than Scala's, for ease of creating embedded interpreters.)
(我使用 Java 类路径,而不是 Scala 的,以便于创建嵌入式解释器。)
回答by Jacek Laskowski
Time flies by and a lot have changed since the other answers. It's currently SBT 0.13.6time.
时间飞逝,自从其他答案以来发生了很多变化。现在是 SBT0.13.6时间。
I think what you may need is the sbt-onejar pluginor the SBT Native Packager plugin.
我认为您可能需要的是sbt-onejar 插件或SBT Native Packager 插件。
sbt-onejar"is a simple-build-tool plugin for building a single executable JAR containing all your code and dependencies as nested JARs."
sbt-onejar “是一个简单的构建工具插件,用于构建单个可执行 JAR,其中包含作为嵌套 JAR 的所有代码和依赖项。”
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."
SBT Native Packager的“目标是能够将使用 SBT 构建的 Scala 软件捆绑到本地打包系统中,例如 deb、rpm、homebrew、msi。”
回答by Yang
Just discovered the sbt start script plugin: https://github.com/typesafehub/xsbt-start-script-plugin:
刚刚发现了 sbt 启动脚本插件: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.
此插件允许您为项目生成脚本目标/启动。该脚本将“就地”运行项目(无需先构建包)。
目标/启动脚本类似于 sbt run 但它不依赖于 SBT。不建议将 sbt run 用于生产用途,因为它会将 SBT 本身保留在内存中。target/start 旨在在生产中运行应用程序。
该插件添加了一个生成目标/启动的任务启动脚本。它还添加了一个阶段任务,别名为 start-script 任务。

