1

我正在寻找一种方法来初始化 groovysh,然后进入常规的 groovysh 交互会话。那是:

  • 运行一些脚本(例如导入或其他)
  • 继续在 groovysh 中运行,就好像之前的脚本是从会话开始时手动输入的一样。

看到这个:

但我希望能够在每个应用程序而不是每个用户级别上执行此操作。即不同的应用程序通常不会导入相同的东西(或以相同的方式初始化)。那可能吗?

4

3 回答 3

1

从 Groovy 2.4 开始,您可以使用groovysh -e '... your code here ...'.

例子:

$ groovysh -e '2+2'
Groovy Shell (2.4.7, JVM: 1.8.0_131)
Type ':help' or ':h' for help.
----------------------------------------------------------
groovy:000> 2+2
===> 4

如果要预加载脚本,请:load像这样使用:

$ cat preamble.groovy 
def hello(x) {
  "Hello ${x}"
}
println hello("world")

$ groovysh -e ':load preamble.groovy'
Groovy Shell (2.4.7, JVM: 1.8.0_131)
Type ':help' or ':h' for help.
----------------------------------------------------------
groovy:000> :load preamble.groovy
===> true
Hello world
===> null
groovy:000> hello('stackoverflow')
===> Hello stackoverflow

(在最后一个示例中,我手动输入了最后一行,我可以使用hello序言中定义的函数)

于 2017-06-14T21:57:52.463 回答
0

根据这篇文章:

看起来 Groovy <= 2.3 不支持这一点,它将使用以下语法出现在 Groovy 2.4 中:

groovysh -e foo.groovy
于 2014-10-24T16:44:43.877 回答
0

使用load关键字似乎有效:

$ cat test.groovy
myStr = "hello World!"
$
$ groovysh
groovy:000> load test.groovy
===> hello World!
groovy:000> println myStr
hello World!
===> null
groovy:000>
于 2017-06-14T19:56:25.123 回答