我已阅读如何在另一个Groovy脚本中简单导入Groovy文件的内容
我想将常用函数定义在一个Groovy文件中,并从其他Groovy文件中调用这些函数。
我知道这将使用Groovy作为脚本语言,即我不需要类/对象。我正在尝试像Groovy中可以完成的dsl一样做。所有变量都将由Java进行断言,我希望在Shell中执行Groovy脚本。
这有可能吗?有人能提供一些示例吗?
我已阅读如何在另一个Groovy脚本中简单导入Groovy文件的内容
我想将常用函数定义在一个Groovy文件中,并从其他Groovy文件中调用这些函数。
我知道这将使用Groovy作为脚本语言,即我不需要类/对象。我正在尝试像Groovy中可以完成的dsl一样做。所有变量都将由Java进行断言,我希望在Shell中执行Groovy脚本。
这有可能吗?有人能提供一些示例吗?
evaluate(new File("../tools/Tools.groovy"))
将此内容放在脚本顶部。这将引入一个Groovy文件的内容(只需在双引号之间替换文件名为您的Groovy脚本即可)。
我使用一个非常惊奇地被称为"Tools.groovy"的类来完成这个操作。
另一种方法是在Groovy类中定义函数,并在运行时解析并将文件添加到类路径中:
File sourceFile = new File("path_to_file.groovy");
Class groovyClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile);
GroovyObject myObject = (GroovyObject) groovyClass.newInstance();
GroovyObject
,而不是您自己的类名作为占位符。 - haventchecked从Groovy 2.2版本开始,可以使用新的@BaseScript
AST转换注释来声明基础脚本类。
示例:
文件 MainScript.groovy:
abstract class MainScript extends Script {
def meaningOfLife = 42
}
文件 test.groovy:
import groovy.transform.BaseScript
@BaseScript MainScript mainScript
println "$meaningOfLife" //works as expected
MainScript.groovy
的路径? - QkiZ我认为最好的选择是将实用工具以 Groovy 类的形式组织起来,将它们添加到 classpath 中,并且让主脚本通过 import 关键字引用它们。
例子:
scripts/DbUtils.groovy
class DbUtils{
def save(something){...}
}
脚本/scripts/script1.groovy:
import DbUtils
def dbUtils = new DbUtils()
def something = 'foobar'
dbUtils.save(something)
运行脚本:
cd scripts
groovy -cp . script1.groovy
lib
和src
目录这样的目录结构,这个会怎么工作。 - Gi0rgi0s把外部脚本看作Java类怎么样?基于这篇文章:https://www.jmdawson.net/blog/2014/08/18/using-functions-from-one-groovy-script-in-another/
getThing.groovy 外部脚本
def getThingList() {
return ["thing","thin2","thing3"]
}
printThing.groovy 主脚本
thing = new getThing() // new the class which represents the external script
println thing.getThingList()
结果
$ groovy printThing.groovy
[thing, thin2, thing3]
我使用 GroovyShell
来完成这个任务。
GroovyShell shell = new GroovyShell()
def Util = shell.parse(new File('Util.groovy'))
def data = Util.fetchData()
// This is the 'include file'
// Testmain.groovy will load it as an implicit class
// Each method in here will become a method on the implicit class
def myUtilityMethod(String msg) {
println "myUtilityMethod running with: ${msg}"
}
Testmain.groovy
// Run this file
// evaluate implicitly creates a class based on the filename specified
evaluate(new File("./Testutils.groovy"))
// Safer to use 'def' here as Groovy seems fussy about whether the filename (and therefore implicit class name) has a capital first letter
def tu = new Testutils()
tu.myUtilityMethod("hello world")
methodMissing(...)
,这使你基本上可以使用映射或任何你想用的方法进行动态分派。
这里演示了几种方法:http://www.nextinstruction.com/blog/2012/01/08/creating-dsls-with-groovy/。如果你想看某种技术的例子,请告诉我。
$ cat lib/Lib.groovy
package lib
class Lib {
static saySomething() { println 'something' }
def sum(a,b) { a+b }
}
$ cat app.gvy
import lib.Lib
Lib.saySomething();
println new Lib().sum(37,5)
$ groovy app
something
42
结合@grahamparks和@snowindy的答案,加上一些修改,适用于我的在Tomcat上运行的Groovy脚本:
Utils.groovy
class Utils {
def doSth() {...}
}
MyScript.groovy:
/* import Utils --> This import does not work. The class is not even defined at this time */
Class groovyClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(new File("full_path_to/Utils.groovy")); // Otherwise it assumes current dir is $CATALINA_HOME
def foo = groovyClass.newInstance(); // 'def' solves compile time errors!!
foo.doSth(); // Actually works!