如何从 shell 运行一个 sbt 主类作为普通的命令行程序? [英] How do I run an sbt main class from the shell as normal command-line program?

查看:33
本文介绍了如何从 shell 运行一个 sbt 主类作为普通的命令行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 shell 运行一个 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) 导致所有输出都用 logger [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.

推荐答案

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

  1. 安装 greadlink(仅限 OS X):

  1. Install greadlink (OS X only):

a) brew install coreutils

b) 通过将以下行添加到 ~/.bashrc 将 readlink 映射到新函数 (greadlink):

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

运行

target/start mypackage.MyMainClass

这篇关于如何从 shell 运行一个 sbt 主类作为普通的命令行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆