使用Groovy从属性文件获取值 [英] Get values from properties file using Groovy
问题描述
如何使用Groovy从属性文件中获取值?
我需要有一个属性文件(.properties),它将文件名称作为关键字,并且它们的目的地路径作为价值。我需要在运行时解决密钥,具体取决于需要移动的文件。
到目前为止,我可以加载看起来似乎属性的属性,但无法从加载的属性中获取值。
我提到的线程: groovy:如何访问属性文件?,以下是目前为止的代码片段
$ p $
文件propFile =
新文件('D:/XX/XX_Batch/XX_BATCH_COMMON/src/main/resources/patchFiles.properties')
props.load(propFile.newDataInputStream())
def config = new ConfigSlurper()。parse(props)
def ant = new AntBuilder()
def list = ant.fileScanner {
fileset(dir:getSrcPath()){
include(名称:** / *)
}
}
(列表中的f){
def key = f.name
println(道具)
println(config [key])
println(config)
def destn = new File(config ['a'])
}
现在,属性文件具有以下条目:
jan-feb-mar.jsp = / XX / Test / 1
XX-1.0.0-SNAPSHOT.jar = / XX / Test / 1
a = b
c = d
如果使用props.getProperty ('a')
或者
config ['a']
也尝试过t他的代码:notation
但是,只要切换到使用变量key,就像在config [key]中那样返回 - > [:]
我是新来的groovy,不能说我在这里错过了什么。
<看起来你太复杂了。
以下是一个简单的例子,应该完成这项工作:
给定 test。属性
file:
a = 1
b = 2
$
Properties properties = new Properties()
File propertiesFile = new File('test.properties')
propertiesFile.withInputStream {
properties.load(it)
}
def runtimeString ='a'
assert属性。$ runtimeString=='1'
assert properties.b =='2'
How to get values from properties file using Groovy?
I require to have a property file (.properties) which would have file names as key, and their destination path as the value. I will need the key to be resolved at runtime, depending on file that needs to be moved.
So far I am able to load the properties it seems but can't "get" the value from the loaded properties.
I referred to the thread : groovy: How to access to properties file? and following is the code snippet i have so far
def props = new Properties();
File propFile =
new File('D:/XX/XX_Batch/XX_BATCH_COMMON/src/main/resources/patchFiles.properties')
props.load(propFile.newDataInputStream())
def config = new ConfigSlurper().parse(props)
def ant = new AntBuilder()
def list = ant.fileScanner {
fileset(dir:getSrcPath()) {
include(name:"**/*")
}
}
for (f in list) {
def key = f.name
println(props)
println(config[key])
println(config)
def destn = new File(config['a'])
}
the properties file has the following entries for now :
jan-feb-mar.jsp=/XX/Test/1
XX-1.0.0-SNAPSHOT.jar=/XX/Test/1
a=b
c=d
Correct values are returned if I look up using either props.getProperty('a') or, config['a'] Also tried the code: notation
But as soon as switch to using the variable "key", as in config[key] it returns --> [:]
I am new to groovy, can't say what am i missing here.
It looks to me you complicate things too much.
Here's a simple example that should do the job:
For given test.properties
file:
a=1
b=2
This code runs fine:
Properties properties = new Properties()
File propertiesFile = new File('test.properties')
propertiesFile.withInputStream {
properties.load(it)
}
def runtimeString = 'a'
assert properties."$runtimeString" == '1'
assert properties.b == '2'
这篇关于使用Groovy从属性文件获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!