在另一个 groovy 中包含一个 groovy 脚本 [英] Including a groovy script in another groovy
问题描述
我已阅读如何简单地导入另一个 groovy 脚本中的 groovy 文件
我想在一个 groovy 文件中定义常用函数,并从其他 groovy 文件中调用这些函数.
I want to define common functions in one groovy file and call those functions from other groovy files.
我知道这会像脚本语言一样使用 Groovy,即我不需要类/对象.我正在尝试像 dsl 这样可以在 groovy 中完成的事情.所有变量都将从 Java 断言,我想在 shell 中执行 groovy 脚本.
I understand this would be using Groovy like a scripting language i.e, I dont need classes/objects. I am trying to something like dsl that can be done in groovy. All variables will be asserted from Java and I want to execute groovy script in a shell.
这可能吗?有人可以提供一些例子吗.
Is this possible at all ? Can someone provide some example.
推荐答案
evaluate(new File("../tools/Tools.groovy"))
将其放在脚本的顶部.这将引入 groovy 文件的内容(只需用您的 groovy 脚本替换双引号之间的文件名).
Put that at the top of your script. That will bring in the contents of a groovy file (just replace the file name between the double quotes with your groovy script).
我用一个名为Tools.groovy"的类来做这件事.
I do this with a class surprisingly called "Tools.groovy".
这篇关于在另一个 groovy 中包含一个 groovy 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!