2

I'm looking for groovy specific way to read file and store its content as different variables. Example of my properties file:

#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx

At the moment I'm using this java code to read the file and use variables:

  Properties prop = new Properties();
  InputStream input = null;

  try {
            input = new FileInputStream("config.properties");
            prop.load(input);
            this.postgresqlUser = prop.getProperty("postgresql.username")
            this.postgresqlPass = prop.getProperty("postgresql.password")
            this.postgresqlUrl = prop.getProperty("postgresql.url")
            this.consoleUrl = prop.getProperty("console.url")

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
    }

My colleague recommended to use groovy way to deal with this and mentioned streams but I can't seem to find much information about on how to store data in separate variables, what I know so far is that def text = new FileInputStream("config.properties").getText("UTF-8") could read whole file and store it in one variable, but not separate. Any help would be appreciated

Tomas
  • 1,131
  • 2
  • 12
  • 25
  • 2
    http://stackoverflow.com/questions/20871441/get-values-from-properties-file-using-groovy – tim_yates Jan 21 '16 at 15:39
  • @tim thanks, solved my problem instead of all these try and catch now I can use `propertiesFile.withInputStream` closure. – Tomas Jan 21 '16 at 16:04

2 Answers2

3

If you're willing to make your property file keys and class properties abide by a naming convention, then you can apply the property file values quite easily. Here's an example:

def config = '''
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
'''

def props = new Properties().with {
    load(new StringBufferInputStream(config))
    delegate
}

class Foo {
    def postgresqlUsername
    def postgresqlPassword
    def postgresqlUrl
    def consoleUrl

    Foo(Properties props) {
        props.each { key, value ->
            def propertyName = key.replaceAll(/\../) { it[1].toUpperCase() }
            setProperty(propertyName, value)
        }
    }
}

def a = new Foo(props)

assert a.postgresqlUsername == 'xxxxxxx'
assert a.postgresqlPassword == 'xxxxxxx'
assert a.postgresqlUrl == 'xxxx.xxxx.xxxx'
assert a.consoleUrl == 'xxxxx.xxxx.xxx'

In this example, the property keys are converted by dropping the '.' and capitalizing the following letter. So postgresql.url becomes postgresqlUrl. Then it's just a matter for iterating through the keys and calling setProperty() to apply the value.

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
1

Take a look at the ConfigSlurper:

http://mrhaki.blogspot.de/2009/10/groovy-goodness-using-configslurper.html

rdmueller
  • 10,742
  • 10
  • 69
  • 126
  • 1
    Agreed. ConfigSlurper is a much better option to using traditional Java properties files. Makes it a breeze. – pczeus Jan 21 '16 at 15:51