Saturday, August 10, 2019

Working with Groovy Nodes from Kotlin

Yesterday I was translating some Groovy Gradle buildscript to Kotlin in a project deployed to Maven Central using code like this. The block starting on line 105 is some kind of Groovy structure literal (?) that adds to a Node for an XML document. Using that from Kotlin mostly involves appendNode calls (for tags containing only text content) and Node constructions (which automatically insert the new node in the specified parent). To add desired nodes to the document, I used code like this:
asNode().apply {
  appendNode("description", "Description of the library")
  Node(this, "licenses").apply {
    Node(this, "license").apply {
      appendNode("name", "Some License")
      appendNode("url", "http://example.com/license")
    }
  }
  // and so on
}

No comments:

Post a Comment