简体   繁体   English

Groovy从文件获取变量。 变量=值

[英]Groovy get variables from file. variable=value

Seems like this question for most of others looks too simple. 对于大多数其他人来说,这个问题似乎太简单了。 I need a help with following problem. 我需要以下问题的帮助。 Has file(p.envs) with set of variables and values like: 具有文件(p.envs),其中包含一组变量和值,例如:

VARIABLE=foo
NOW=19 October 2014

How to read this file with Groovy to get an output like: 如何使用Groovy读取此文件以获取如下输出:

    new File('p.envs').eachLine { line ->
       println line
       ...
    }
    ...
    println "NOW=$NOW"
    println "$NOW"

Output: 输出:

NOW=19 October 2014
19 October 2014

This issue can help someone to inject custom variables to groovy template in Editable Email Notification Plugin in Jenkins. 此问题可以帮助某人在Jenkins中的可编辑电子邮件通知插件中向groovy模板注入自定义变量。 Thanks! 谢谢!

Since you are working with a file that follows the Java properties format you can use ConfigSlurper to parse it: 由于您正在使用遵循Java属性格式的文件,因此可以使用ConfigSlurper对其进行解析:

def config = new ConfigSlurper().parse(new File('p.envs').toURL())

The config object implements Map so you can print the values like this: config对象实现Map,因此您可以打印如下值:

config.each { key, value -> println "$key=$value" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM