-1

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

Elyahu
  • 226
  • 1
  • 2
  • 15

2 Answers2

0

There are of course multiple options/ways to copy the contents of a folder to another folder.

The most simple method is to use FileUtils.copyDirectory(), built into Java as described in this SO post: Copy entire directory contents to another directory?

Alternatively, you can use Java's `Files.copy', described here: http://docs.oracle.com/javase/tutorial/essential/io/copy.html

import static java.nio.file.StandardCopyOption.*;
...
Files.copy(source, target, REPLACE_EXISTING);

You could use AntBuilder, which you are attempting within your example. You don't need to use XMLSlurper at all. AntBuilder is capable of copying an entire folder without handling individual files (or can filter out files, i.e. by extension). For example:

String sourceDir = SOURCE_DIR_PATH
String destinationDir = DESTINATION_DIR_PATH

new AntBuilder().copy(todir: destinationDir) {
     fileset(dir : sourceDir) {
         exclude(name:"*.java")
     }

Or with no filtering, simply:

new AntBuilder().copy(todir: destinationDir)
Community
  • 1
  • 1
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • I update my question, I have to use the XMLParser or XMLSlurper, for reading and using other properties, with no connection to the copy. As for your other suggetions, Tried them, but the object I get from XMLSlurper can't be converted to a FILE so I can't use your suggestions/ – Elyahu May 21 '17 at 04:15
  • And @pczeus as for you AntBuilder suggetions, You can see the errors I get when using it – Elyahu May 21 '17 at 04:58
0

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}"
Elyahu
  • 226
  • 1
  • 2
  • 15