Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 在groovy中使用单独的数据文件读取变量_Variables_Groovy - Fatal编程技术网

Variables 在groovy中使用单独的数据文件读取变量

Variables 在groovy中使用单独的数据文件读取变量,variables,groovy,Variables,Groovy,我试图将变量数据从实际的脚本groovy文件中分离出来 def test = [a,b,c] def test2 = ['foo': [a,x,y], 'bar': [q,w,e]] def function(String var){} def function2 { test.each { item -> print test } } 因为变量中的值不断变化,但脚本中的值却没有变化。如何让groovy读取变量文件并在运行时加载它 我希望它看起来像这样 variable

我试图将变量数据从实际的脚本groovy文件中分离出来

def test = [a,b,c]
def test2 = ['foo': [a,x,y], 'bar': [q,w,e]]


def function(String var){}
def function2 {
test.each
{ 
    item ->
print test


}
}
因为变量中的值不断变化,但脚本中的值却没有变化。如何让groovy读取变量文件并在运行时加载它

我希望它看起来像这样

variables.properties
def test = [a,b,c]
def test2 = ['foo': [a,x,y], 'bar': [q,w,e]]
main.groovy

load ( variable.properties)

def function(String var){}
def function2 {
test.each
{ 
    item ->
print test


}
}

在Groovy中,可以将数据作为Groovy代码进行求值。这是一种强大的技术。这对你的目标来说可能有点过分,但会奏效

考虑这个
config.data
文件(它是一个Groovy文件,但可以命名为任何名称):

以及
App.groovy
文件。它在
GroovyShell
中设置变量的
绑定。
Config
在shell中进行计算,我们可以在主应用程序中引用变量

def varMap = [:]
varMap["test"] = [] 
varMap["test2"] = [:] 

def binding = new Binding(varMap)
def shell = new GroovyShell(binding)

def function2 = { test, test2 ->
    test.each { println "test item: ${it}" }
    test2.each { println "test2 item: ${it}" }
}

// ------ main

// load Config and evaluate it
def configText = new File(args[0]).getText()
shell.evaluate(configText)

def test = varMap["test"] 
def test2 = varMap["test2"] 

function2(test, test2)
用法示例:

$ groovy App.groovy config.data 
test item: a
test item: b
test item: c
test2 item: foo=[a, x, y]
test2 item: bar=[q, w, e]

您希望该文件是什么样子?@daggett我更新了我的问题来回答这个问题您需要显式地使用Groovy变量定义,还是可以使用任何常规数据格式(例如JSON属性)?任何形式的数据。最好是亚马尔@Davenewton几乎所有的序列化方法都支持集合。`java.io.FileNotFoundException:config.data(没有这样的文件或目录)`I收到这个错误
config.data
文件必须与
App.groovy
文件位于同一目录中,并如上所述通过命令行传递。太好了。请单击答案左侧对您最有帮助的复选标记来结束您的问题。如果我想在代码中指定文件,我假设我只是将文件名替换为字符串文字?
$ groovy App.groovy config.data 
test item: a
test item: b
test item: c
test2 item: foo=[a, x, y]
test2 item: bar=[q, w, e]