如何从Shell作为常规命令行程序运行sbt主类? [英] How do I run an sbt main class from the shell as normal command-line program?
问题描述
如何从外壳运行sbt应用程序,以便可以将我的应用程序作为普通的命令行程序运行(就像直接通过 scala
运行但没有
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 enormous classpath)?
我知道我可以做到:
echo hello | sbt 'run-main com.foo.MyMain3 arg1 arg2' > out.txt
但这(1)需要永远启动,因为它会启动sbt,(2)导致所有stdout和stderr都转到stdout,并且(3)使所有输出都用记录器[info]或[error]装饰。
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].
我看着 https://github.com/harrah/xsbt/wiki/Launcher ,但它似乎过于繁重,因为它会下载依赖项并建立一个新的环境等等。我只想在现有的开发环境中运行此应用程序。
I looked at https://github.com/harrah/xsbt/wiki/Launcher but 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.
到目前为止,我已经将自己的脚本拼凑在一起以构建类路径,您也可以做一些修改项目文件以获取sbt以打印原始类路径但我觉得必须有更好的方法。
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插件现在位于:
https://github.com/sbt/sbt-start-script
它需要一些设置步骤并生成在OS X上不起作用的脚本,但是如果您在该平台上(请参见下文),则可以轻松修复该脚本。
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).
-
安装greadlink(仅适用于OS X):
Install greadlink (OS X only):
a) brew install coreutils
b)将读取链接映射到通过将以下行添加到〜/ .bashrc中来创建新函数( g readlink):
b) map readlink to the new function (greadlink) by adding these lines to ~/.bashrc:
function readlink(){greadlink $ @; }
function readlink() { greadlink "$@"; }
export -f readlink`
export -f readlink`
将开始脚本插件添加到〜/ .sbt / plugins / build.sbt:
Add start-script plugin to ~/.sbt/plugins/build.sbt:
addSbtPlugin( com.typesafe.sbt% sbt-start-script% 0.8.0)
addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.8.0")
将启动脚本任务添加到当前项目:
Add start-script task to current project:
$ sbt add-start-script -tasks
#从build.sbt所在的目录执行
$ sbt add-start-script-tasks
# execute from directory where build.sbt resides
向当前build.sbt添加启动脚本支持:
Add start-script support to current build.sbt:
导入com.typesafe.sbt.SbtStartScript
import com.typesafe.sbt.SbtStartScript
seq(SbtStartScript.startScriptForClassesSettings:_ *)
seq(SbtStartScript.startScriptForClassesSettings: _*)
请注意语句之间的空白行(SBT构建文件的必要条件)。
Note the blank line in between statements (de rigueur for SBT build files).
然后,每当您要创建脚本来启动应用程序(如 sbt run-main
,但没有sbt,请执行:
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
这篇关于如何从Shell作为常规命令行程序运行sbt主类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!