【问题标题】:Get values from properties file using Groovy使用 Groovy 从属性文件中获取值
【发布时间】:2014-01-19 05:19:17
【问题描述】:

如何使用 Groovy 从属性文件中获取值?

我需要一个属性文件 (.properties),它以文件名作为键,以它们的目标路径作为值。我需要在运行时解析密钥,具体取决于需要移动的文件。

到目前为止,我能够加载看起来的属性,但无法从加载的属性中“获取”值。

我提到了线程:groovy: How to access to properties file? 以下是我到目前为止的代码 sn-p

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'])

    }

属性文件现在有以下条目:

jan-feb-mar.jsp=/XX/Test/1
XX-1.0.0-SNAPSHOT.jar=/XX/Test/1
a=b
c=d

如果我使用 props.getProperty('a') 查找,则返回正确的值 要么, 配置['a'] 还尝试了代码:notation

但是一旦切换到使用变量“key”,就像在 config[key] 中一样,它会返回 --> [:]

我是 groovy 的新手,不能说我在这里缺少什么。

【问题讨论】:

  • 提示:没有像“Java”属性文件这样的东西。它要么是属性文件,要么不是。使用哪种语言(或文本编辑器)编写它并不重要。
  • @tim_yates 谢谢。我见过那个线程。不是这样的:(

标签: groovy properties-file


【解决方案1】:

在我看来,你把事情复杂化了。

这里有一个简单的例子应该可以完成这项工作:

对于给定的test.properties 文件:

a=1
b=2

这段代码运行良好:

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'

【讨论】:

  • 感谢您的回复。问题是: 1. 该文件将有很多条目 2. 我不想将所有键/值都写为 groovy 脚本中的断言。我不能在运行时解析密钥吗?并从属性中相应地选择值。密钥将基于我读取的 ob 文件名,它们可以是 1 个或更多,大约几百个(我真的需要记住,在这里按“输入”会提交我的评论)
  • 断言只是为了表明它有效。当然,您的代码中不需要它们。在答案中添加了评论。
  • 谢谢@JBaruch 我问的不够清楚我意识到:) 我关心的是如何让“.a”部分变得动态? “a” 是一个例子,它将是一个大的关键字符串,我将从文件名中获取。我如何将变量附加到“属性”,例如。我可以写吗? - 断言 properties.xyz == '/isekk/123456' 其中 xyz 是一个包含一些字符串的变量。我上面的代码 sn-p 详细说明了这个场景。感谢您花时间回复。
  • 知道了。示例已更改。
  • 我想他想知道一种方法来列出属性文件中的所有键,他想稍后对其进行迭代。
【解决方案2】:

除非File是必须的,如果要加载的文件在src/main/resourcessrc/test/resources文件夹或类路径中,getResource()是另一种解决方法。

例如。

    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】:

    遇到了类似的问题,我们解决了:

    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
    

    【讨论】:

    • 这在 Jenkins 流水线脚本中运行良好,但在普通 Groovy 中则不行,因为 readFile 等不可用。此外,如果已经在 J​​enkins 中运行,我倾向于使用“readProperties”方法不直接处理输入流,而是使用一个自我解释的单行代码。
    【解决方案4】:

    以防万一……

    如果属性键包含点 (.),请记住将键放在引号中。

    属性文件:

    a.x = 1
    

    时髦的:

    Properties properties ...
    
    println properties."a.x"
    

    【讨论】:

      【解决方案5】:
      Properties properties = new Properties()
      
      properties.load(new File("path/to/file.properties").newReader())
      

      【讨论】:

        【解决方案6】:

        只是另一种方式。如果它适合您,请使用它。 :)

        Properties properties = new Properties()
        
        //loading property file
        
        File propertiesFile = new File(this.class.getResource('application.properties').getPath())
        
        propertiesFile.withInputStream {
        
            properties.load(it)
        
        }
        
        //Accessing the value from property file
        
        properties.getProperty('userName')
        

        【讨论】:

          【解决方案7】:

          带有静态方法扩展:

          Properties.metaClass.static.fromFile =
              {file -> new Properties().with{new File(file).withInputStream it.&load;it}}
          
          def properties = Properties.fromFile('test.properties')
          

          【讨论】:

            【解决方案8】:

            通过给出键从“local.properties”获取属性值的 Groovy。

            示例-查找此属性键的值是“mail.smtp.server”

            在 V5 中

            ctx.getBean("configurationService")

            configurationService = ctx.getBean("configurationService")

            字符串值 = configurationService.getConfiguration().getString("mail.smtp.server","")

            1905 年

            spring.getBean("configurationService")

            configurationService = spring.getBean("configurationService")

            字符串值 = configurationService.getConfiguration().getString("mail.smtp.server","")

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-02-08
              • 1970-01-01
              • 1970-01-01
              • 2020-08-18
              • 1970-01-01
              • 1970-01-01
              • 2012-01-01
              • 2013-05-07
              相关资源
              最近更新 更多