Groovy script for batch importing XML-Files into the JCR of Magnolia (CMS)

The importer of the jcr-tools is very limited. You can only import one file at time. Once you have uploaded a file, you also need to  select the corresponding workspace. With many files to import regulary this can be quite tedious. That's why I wrote a quick and dirty groovy script. It automatically imports all files from a folder into the correct workspace.


import info.magnolia.importexport.DataTransporter
import javax.jcr.ImportUUIDBehavior
import groovy.io.FileType

def list = []
def dir = new File('/home/marc/EOS_01112018/')
dir.eachFileRecurse (FileType.FILES) { file ->
  list << file
}

list.each {
  path = it.path
  fileName = it.name
  print 'fileName = ' + fileName
  // currently path may not contain "(" and "."
  int indexOf = fileName.lastIndexOf(" (")
  if (indexOf < 0) indexOf = fileName.indexOf(".")
  workspace = fileName.substring(0, indexOf)
  println ' workspace = ' + workspace


hm = ctx.getJCRSession(workspace)
workspaceRoot = hm.getRootNode()
    
xmlFile = new File(path)
DataTransporter.importFile(xmlFile, workspace, '/', false,
ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, true)
    
}

Feel free to contact me for any Magnolia (CMS) or OpenCms projects as a freelancer (Freiberufler).