I have following task written in Groovy in Gradle that merges order.xml file into inventory.xml as shown in the code below, how can I accomplish the same in Kotlin?
initial inventory.xml
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car>GM</car>
<car>Ford</car>
</cars>
order.xml
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car>Acura</car>
<car>Honda</car>
</cars>
// How to convert following code into Kotlin?
task mergeXml {
final inventory = new File("$rootDir/src/main/resources/inventory.xml")
final inventoryContent = inventory.getText()
final order = new File("$rootDir/src/main/resources/order.xml")
final orderContent = order.getText()
doLast {
def rootNode = new XmlParser().parseText(inventoryContent)
def printWriter = new PrintWriter(inventory)
def xmlNodePrinter = new XmlNodePrinter(printWriter)
new XmlParser().parseText(orderContent).children().each { rootNode.append(it) }
printWriter.print(inventoryContent.split("(?=<cars)")[0])
xmlNodePrinter.print(rootNode)
}
}
Expected inventory.xml after processing
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car>
GM
</car>
<car>
Ford
</car>
<car>
Acura
</car>
<car>
Honda
</car>
</cars>