Search code examples
javaxmlgroovyxmlslurper

Copying directory using Groovy & XMLSulrper data


Please help, so I'll avoid tearing my hair out ...

I need to simple Copy folderA to FolderB using a groovy that gets the data from an XML. I have to use XMLSlurper or XMLParser

This is it -

println "Start"

def Folders = new XmlSlurper().parse(new File("CopyParams.xml"))

Folders.Folder.each
{
it.DirectoryToCopy.each
{
println "Copying ${it.@source} into folder ${it.@target}"

new AntBuilder().copy (
todir: "${it.@target}")
{
fileset(
dir: "${it.@source}" )
}
}
}

println "End"

System.exit(0)

Then I get -

> Copying C:\Temp\Groovy\Source_Folders into folder C:\Temp\Groovy\Target_Foler
Caught: groovy.lang.MissingFieldException: No such field: source for class: org.codehaus.groovy.runtime.NullObject
groovy.lang.MissingFieldException: No such field: source for class: org.codehaus.groovy.runtime.NullObject
at testCopy$_run_closure1_closure2_closure3.doCall(testCopy.gvy:14)
at testCopy$_run_closure1_closure2_closure3.doCall(testCopy.gvy)
at testCopy$_run_closure1_closure2.doCall(testCopy.gvy:11)
at testCopy$_run_closure1.doCall(testCopy.gvy:7)
at testCopy.run(testCopy.gvy:5)

I tried to use before the Copy -

String src = ${it.@source[0]}
String dst = ${it.@target[0]}

Or

String src = new XmlNodePrinter().print(${it.@source})
String dst = new XmlNodePrinter().print(${it.@target})

Then I get -

> Copying C:\Temp\Groovy\Source_Folders into folder C:\Temp\Groovy\Target_Foler
Caught: groovy.lang.MissingMethodException: No signature of method: testCopy.$() is applicable for argument types: (testCopy$_run_
closure1_closure2_closure3) values: [testCopy$_run_closure1_closure2_closure3@1b06cab]
Possible solutions: is(java.lang.Object), run(), run(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;)
groovy.lang.MissingMethodException: No signature of method: testCopy.$() is applicable for argument types: (testCopy$_run_closure1
_closure2_closure3) values: [testCopy$_run_closure1_closure2_closure3@1b06cab]
Possible solutions: is(java.lang.Object), run(), run(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;)
at testCopy$_run_closure1_closure2.doCall(testCopy.gvy:11)
at testCopy$_run_closure1.doCall(testCopy.gvy:7)
at testCopy.run(testCopy.gvy:5)

I also tried using FileUtils but got even more none understood errors

What am I doing wrong ?

Will it be better if I'll use "XMLParser" ?

Thanks, Eli


Solution

  • Found the answer with the help of a collegue -

    declaring the variables you get from XMLSlurper to be a strings should be with "def"

    def sourceDir = "${it.@source}"
    def destinationDir = "${it.@target}"