在Groovy中将闭包传递给函数时出错
我在Groovy脚本中编写了一个闭包来复制文件,然后将该闭包传递给eachFileMatch(regex,closure),以复制与给定regex匹配的所有文件。当我在Groovy控制台中原型化它时,一切正常,但现在在Eclipse中运行它时,我得到以下错误:在Groovy中将闭包传递给函数时出错,groovy,Groovy,我在Groovy脚本中编写了一个闭包来复制文件,然后将该闭包传递给eachFileMatch(regex,closure),以复制与给定regex匹配的所有文件。当我在Groovy控制台中原型化它时,一切正常,但现在在Eclipse中运行它时,我得到以下错误: groovy.lang.MissingMethodException: No signature of method: java.lang.String.eachFileMatch() is applicable for argument
groovy.lang.MissingMethodException: No signature of method: java.lang.String.eachFileMatch() is applicable for argument types: (java.util.regex.Pattern, file_copy$_run_closure3)
下面是对eachFileMatch()的闭包和调用
从异常中,
sourceDir
是一个字符串,而不是eachFileRecurse
所需的文件。从异常中,sourceDir
是一个字符串,而不是eachFileRecurse尝试新文件(sourceDir)。eachFileMatch(regex,fileCopyClosure)
尝试新文件(sourceDir)。每个文件匹配(regex,fileCopyClosure)
只要你指出它,它看起来是如此明显。谢谢你的第二双眼睛。当你指出它的时候,它看起来很明显。谢谢你的第二双眼睛。
def fileCopyClosure = {
if(it.canRead()){
def destFolder = new File("${outputDirectory}")
if(!destFolder.exists()){
println "Creating directory"
destFolder.mkdir()
}
def desti = new File("${outputDirectory}\\${it.name}")
output = desti.newOutputStream()
it.eachByte(1024, write)
output.close()
}
}
sourceDir.eachFileMatch(regex, fileCopyClosure)