有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Groovy和XMLSulrper数据复制java目录

请帮忙,这样我就不会把头发扯下来了

我需要使用从XML获取数据的groovy将folderA简单地复制到FolderB。我必须使用XMLSlurper或XMLParser

就是这个-

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)

然后我得到-

> 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)

我试图在复制之前使用-

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

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

然后我得到-

> 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)

我也尝试过使用FileUtils,但得到的错误更多

我做错了什么

如果我使用“XMLParser”会更好吗

谢谢, 伊莱


共 (2) 个答案

  1. # 1 楼答案

    在同事的帮助下找到了答案-

    将从XMLSlurper获得的变量声明为字符串时应使用“def”

    def sourceDir = "${it.@source}"
    def destinationDir = "${it.@target}"
    
  2. # 2 楼答案

    当然,将一个文件夹的内容复制到另一个文件夹有多种选项/方法

    最简单的方法是使用FileUtils.copyDirectory(),内置到Java中,如本文所述:Copy entire directory contents to another directory?

    或者,您可以使用Java的`文件。复制',此处描述:http://docs.oracle.com/javase/tutorial/essential/io/copy.html

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

    您可以使用AntBuilder,您正在示例中尝试使用它。您根本不需要使用XMLSlurperAntBuilder能够复制整个文件夹而不处理单个文件(或者可以过滤掉文件,即通过扩展名)。例如:

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

    或者不进行过滤,只需:

    new AntBuilder().copy(todir: destinationDir)