Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h1>ConfigSlurper</h1><p><a href="http://groovy.codehaus.org/gapi/groovy/util/ConfigSlurper.html">ConfigSlurper</a> is a utility class within Groovy for writing properties file like scripts for performing configuration. Unlike regular Java properties files ConfigSlurper scripts support native Java types and are structured like a tree.</p><p>Below is an example of how you could configure Log4j with a ConfigSlurper script:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>log4j.appender.stdout = "org.apache.log4j.ConsoleAppender" log4j.appender."stdout.layout"="org.apache.log4j.PatternLayout" log4j.rootLogger="error,stdout" log4j.logger.org.springframework="info,stdout" log4j.additivity.org.springframework=false </pre></td></tr></table><p>To load this into a readable config you can do:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def config = new ConfigSlurper().parse(new File('myconfig.groovy').toURL()) assert "info,stdout" == config.log4j.logger.org.springframework assert false == config.log4j.additivity.org.springframework </pre></td></tr></table><p>As you can see from the example above you can navigate the config using dot notation and the return values are Java types like strings and booleans.</p><p>You can also use scoping in config scripts to avoid repeating yourself. So the above config could also be written as:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>log4j { appender.stdout = "org.apache.log4j.ConsoleAppender" appender."stdout.layout"="org.apache.log4j.PatternLayout" rootLogger="error,stdout" logger { org.springframework="info,stdout" } additivity { org.springframework=false } } </pre></td></tr></table><h2>Converting to and from Java properties files</h2><p>You can convert ConfigSlurper configs to and from Java properties files. For example:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>java.util.Properties props = // load from somewhere def config = new ConfigSlurper().parse(props) props = config.toProperties() </pre></td></tr></table><h2>Merging configurations</h2><p>You can merge config objects so if you have multiple config files and want to create one central config object you can do:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def config1 = new ConfigSlurper().parse(..) def config2 = new ConfigSlurper().parse(..) config1 = config1.merge(config2) </pre></td></tr></table><h2>Serializing a configuration to disk</h2><p>You can serialize a config object to disk. Each config object implements the groovy.lang.Writable interface that allows you to write out the config to any java.io.Writer:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def config = new ConfigSlurper().parse(..) new File("..").withWriter { writer -> config.writeTo(writer) } </pre></td></tr></table><h2>Special "environments" Configuration</h2><p>The ConfigSlurper class has a special constructor other than the default constructor that takes an "environment" parameter. This special constructor works in concert with a property setting called environments. This allows a default setting to exist in the property file that can be superceded by a setting in the appropriate environments closure. This allows multiple related configurations to be stored in the same file.</p><p>Given this groovy property file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=groovy|title=Sample.groovy" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9U2FtcGxlLmdyb292eXxsYW5ndWFnZT1ncm9vdnl9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>sample { foo = "default_foo" bar = "default_bar" } environments { development { sample { foo = "dev_foo" } } test { sample { bar = "test_bar" } } } </pre></td></tr></table><p>Here is the demo code that exercises this configuration:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def config = new ConfigSlurper("development").parse(new File('Sample.groovy').toURL()) assert config.sample.foo == "dev_foo" assert config.sample.bar == "default_bar" config = new ConfigSlurper("test").parse(new File('Sample.groovy').toURL()) assert config.sample.foo == "default_foo" assert config.sample.bar == "test_bar" </pre></td></tr></table><p>Note: the environments closure is not directly parsable. Without using the special environment constructor the closure is ignored.</p><p>The value of the environment constructor is also available in the configuration file, allowing you to build the configuration like this:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> switch (environment) { case 'development': baseUrl = "devServer/" break case 'test': baseUrl = "testServer/" break default: baseUrl = "localhost/" } </pre></td></tr></table><h1>Further information</h1><p><a href="http://jroller.com/page/0xcafebabe?entry=using_groovy_configslurper_to_configure">Using Groovy ConfigSlurper to Configure Spring Beans</a></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced