Get values from properties file using Groovy

老子叫甜甜 提交于 2019-11-26 12:25:16

问题


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.


回答1:


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'



回答2:


Unless File is necessary, and if the file to be loaded is in src/main/resources or src/test/resources folder or in classpath, getResource() is another way to solve it.

eg.

    def properties = new Properties()
    //both leading / and no / is fine
    this.getClass().getResource( '/application.properties' ).withInputStream {
        properties.load(it)
    }

    //then: "access the properties"
    properties."my.key"



回答3:


Just in case...

If a property key contains dot (.) then remember to put the key in quotes.

properties file:

a.x = 1

groovy:

Properties properties ...

println properties."a.x"



回答4:


Had a similar problem, we solved it with:

def content = readFile 'gradle.properties'

Properties properties = new Properties()
InputStream is = new ByteArrayInputStream(content.getBytes());
properties.load(is)

def runtimeString = 'SERVICE_VERSION_MINOR'
echo properties."$runtimeString"
SERVICE_VERSION_MINOR = properties."$runtimeString"
echo SERVICE_VERSION_MINOR



回答5:


Properties properties = new Properties()

properties.load(new File("path/to/file.properties").newReader())


来源:https://stackoverflow.com/questions/20871441/get-values-from-properties-file-using-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!