Sunday, September 12, 2010

Groovy: how to parse all property files in a directory

I also group the files by their suffix (dev2, dev3...)
and I print the property values, for names like admin_host, admin_port...


package com.acme.propertyprocessor
import java.io.File;


class PropertyProcessor {

    static main(args) {
        def envs = ["dev2", "dev3", "pp", "prod", "tst1", "tst2"];
        envs.each { env->
            println "

" + env + "

" new File("c:/properties").eachFile() { file-> if (file.getName().startsWith(env)) { def prop = new Properties() prop.load(new FileInputStream(file)) println prop.domain_prefix + " http://" + prop.admin_host + ":" + prop.admin_port + " http://" + prop.frontend_host + ":" + prop.frontend_port + " " + prop.ora_host1 + ":" + prop.ora_port + " " + prop.ora_user } } } } }


I suspect it could be made even simpler, for instance using eachFileMatch

Groovy rocks! Too bad I still haven't found a really professional IDE.

No comments: