在 sbt shell 中设置环境变量

2022-01-24 00:00:00 scala build java sbt

我希望能够在交互式 sbt shell 中设置一个环境变量,但我似乎找不到这样做的方法.(我查看了官方 sbt 文档以及 stackoverflow 均未成功).

I would like to be able to set an environment variable from within the interactive sbt shell, and I can't seem to find a way to do that. (I have looked on the official sbt docs as well as on stackoverflow without success).

我想明确一点,我不想必须在 build.sbt 文件中设置这个环境变量,而是能够改变它在我的交互式 sbt shell 会话中即时运行,以便环境变量用于我运行的下一个 sbt 命令.

I want to make it clear that I don't want to have to set this environment variable in the build.sbt file, but rather be able to change it on the fly on my interactive sbt shell session, so that the environment variable is used for the next sbt commands I run.

例如,我希望能够(从 sbt shell 中)执行以下操作:

For example, I would like to be able to do something like (from within the sbt shell):

> set_environment_variable("foo", "foo_value")
> `mymodule`/run
> (... program runs and completes)
> set_environment_variable("foo", "another_foo_value")
> `mymodule`/run

这可能吗?如果是,我该怎么做?

Is this possible? And if it is, how can I do it?

推荐答案

使用 sbt 0.13.15 我发现唯一对我有用的是使用 eval.

Using sbt 0.13.15 the only thing I've found which has worked for me is to use eval.

> eval System.setProperty("foo", "foo_value")
> `mymodule`/run
> ...
> eval System.setProperty("foo", "another_foo_value")
> `mymodule`/run

相关文章