Create nested map from key in groovy
<p>I'm relatively new to groovy and am using it in the context of a gradle build. So please don't be harsh if there is an easy out-of-the-box solution for this.</p>

<p>Basically I'm trying to accomplish the reverse of <a href="https://stackoverflow.com/questions/25916607/return-nested-key-in-groovy">Return Nested Key in Groovy</a>. That is, I have some keys read from the <code>System.properties</code> map for example <code>user.home</code> and  corresponding values like <code>C:\User\dpr</code>. Now I want to create a map that reflects this structure to use it in a <code>groovy.text.SimpleTemplateEngine</code> as bindings:</p>

<pre><code>[user : [home : 'C:\Users\dpr']]
</code></pre>

<p>The keys may define an arbitrary deep hierarchy. For example <code>java.vm.specification.vendor=Oracle Corporation</code> should become:</p>

<pre><code>[java : [vm : [spec : [vendor : 'Oracle Corporation']]]]
</code></pre>

<p>Additionally there are properties with the same parents such as <code>user.name=dpr</code> and <code>user.country=US</code>:</p>

<pre><code>[
   user: [
     name: 'dpr',
     country: 'US'
  ]
]
</code></pre>

<hr>

<p><strong>Edit</strong>: While <code>ConfigSlurper</code> is really nice, it is somewhat too defensive with creating the nested maps as it stops nesting at the minimum depth of a certain key.</p>

<p>I currently ended up using this</p>

<pre><code>def bindings = [:]
System.properties.sort().each {
  def map = bindings
  def split = it.key.split("\\.")
  for (int i = 0; i &lt; split.length; i++) {
    def part = split[i];

    // There is already a property value with the same parent
    if (!(map instanceof Map)) {
      println "Skipping property ${it.key}"
      break;
    }

    if (!map.containsKey(part)) {
      map[part] = [:]
    }

    if (i == split.length - 1) {
      map[part] = it.value
    } else {
      map = map[part]
    }
  }
  map = it.value
}
</code></pre>

<p>With this solution the properties <code>file.encoding.pkg</code>, <code>java.vendor.url</code> and <code>java.vendor.url.bug</code> are discarded, which is not nice but something I can cope with.</p>

<p>However the above code is not very groovyish.</p>