Reading several config-files via Groovy

Reading configuration files with groovy can be easy if you know how 🙂
There are multiple formats the configuration can be served to an application, here we’ll have a look on xml, json and yaml.

config as XML

config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<inventory>
    <department>java-webapp</department>
    <artifact-repo>
        <tools>
            <name>artifactory</name>
            <vendor>jfrog</vendor>
            <version>5.0</version>
        </tools>
        <tools>
            <name>nexus</name>
            <vendor>sonatype</vendor>
            <version>6.2</version>
        </tools>
        <tools>
            <name>archiva</name>
            <vendor>apache</vendor>
            <version>2.2.1</version>
        </tools>
    </artifact-repo>
    <source-repo>
        <tools>
            <name>subversion</name>
            <license>GNU GPLv2</license>
            <version>1.9.4</version>
        </tools>
        <tools>
            <name>git</name>
            <license>apache 2.0</license>
            <version>2.11.0</version>
        </tools>
    </source-repo>
</inventory>

the xml-reader

import groovy.util.XmlSlurper

def xmlFile = new File('03_example.xml')

def xmlSlurper = new XmlSlurper()
def inventory = xmlSlurper.parse(xmlFile)

// -----------------
assert 'java-webapp' == inventory.'department'.toString()
assert 3 == inventory.'artifact-repo'.'tools'.size()
assert 2 == inventory.'source-repo'.'tools'.size()
assert 'nexus' == inventory.'artifact-repo'.'tools'[1].'name'.toString()

config as YAML

config.yaml

---
department: java-webapp
artifact-repo:
  tools:
  - name: artifactory
    vendor: jfrog
    version: '5.0'
  - name: nexus
    vendor: sonatype
    version: '6.2'
  - name: archiva
    vendor: apache
    version: 2.2.1
source-repo:
  tools:
  - name: subversion
    license: GNU GPLv2
    version: 1.9.4
  - name: git
    license: apache 2.0
    version: 2.11.0

the yaml-reader

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.*

def yamlFile = new File('02_example.yaml')

Yaml yaml = new Yaml()

def inventory = yaml.load(new FileInputStream(yamlFile))

// -----------------
assert 'java-webapp' == inventory.'department'.toString()
assert 3 == inventory.'artifact-repo'.'tools'.size()
assert 2 == inventory.'source-repo'.'tools'.size()
assert 'nexus' == inventory.'artifact-repo'.'tools'[1].'name'.toString()

Config as JSON

config.json

{
  "department": "java-webapp",
  "artifact-repo" : {
    "tools" : [
      {
        "name" : "artifactory",
        "vendor" : "jfrog",
        "version" : "5.0"
      },
      {
        "name" : "nexus",
        "vendor" : "sonatype",
        "version" : "6.2"
      },
      {
        "name" : "archiva",
        "vendor" : "apache",
        "version" : "2.2.1"
      }
    ]
  },
  "source-repo" : {
    "tools": [
      {
        "name" : "subversion",
        "license" : "GNU GPLv2",
        "version" : "1.9.4"
      },
      {
        "name" : "git",
        "license" : "apache 2.0",
        "version" : "2.11.0"
      }
    ]
  }
}

the json-reader

import groovy.json.JsonSlurper

def jsonFile = new File('01_example.json')

def jsonSlurper = new JsonSlurper()
def inventory = jsonSlurper.parse(jsonFile)

// -----------------
assert 'java-webapp' == inventory.'department'.toString()
assert 3 == inventory.'artifact-repo'.'tools'.size()
assert 2 == inventory.'source-repo'.'tools'.size()
assert 'nexus' == inventory.'artifact-repo'.'tools'[1].'name'.toString()