DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Groovy Goodness: Extend ConfigSlurper with Custom Environments Sections

Groovy Goodness: Extend ConfigSlurper with Custom Environments Sections

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
May. 13, 14 · Java Zone · Interview
Like (0)
Save
Tweet
5.79K Views

Join the DZone community and get the full member experience.

Join For Free

Groovy contains the useful ConfigSlurper for reading in configuration files where settings can be different for different environments. We only have to include an environments block and define values for configuration properties per environment. Since Groovy 2.3 we can add our own conditional configuration blocks and therefore don't have to put everything in an environments block.

In the following sample we define our own configuration block servers and use two environments local and prod. We given the propertymail.host different values per environment:

// Configuration script.
def config = '''
// Custom block with setting
// conditional per environment.
servers {
    local {
        mail.host = 'greenmail'
    }

    prod {
        mail.host = 'mail.server'
    }
}

environments {
    local {
        appName = 'local'
    }
    prod {
        appName = 'production'
    }
}
'''

// Helper closure to create a new
// ConfigSlurper for the given environment and
// register servers as section with configuration
// per environment.
def createConfig = { env ->
    def configSlurper = new ConfigSlurper(env)
    configSlurper.registerConditionalBlock('servers', env)
    configSlurper
}

// Create configuration slurper and
// set environment to prod.
def configuration = createConfig('prod').parse(config)

assert configuration.mail.host == 'mail.server'
assert configuration.appName == 'production'


// Create configuration slurper and
// set environment to local.
configuration = createConfig('local').parse(config)

assert configuration.mail.host == 'greenmail'
assert configuration.appName == 'local'

Code written with Groovy 2.3.

Groovy (programming language)

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Your Old Laptop Is Your New Database Server
  • Choosing Between GraphQL Vs REST
  • Synchronization Methods for Many-To-Many Associations

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo