我有一个闭包在遍历中正常工作,但另一个同类失败。我怀疑范围或时间正在导致失败。工作代码将文件系统中文件的大小相加。代码不工作是检查文件的内容,只打印一个匹配。Groovy文件解析文件时的遍历行为
def groovySrcDir = new File('.', 'plugins/')
def countSmallFiles = 0
def postDirVisitor = {
if (countSmallFiles > 0) {
println "Found $countSmallFiles files with small filenames in ${it.name}"
}
countSmallFiles = 0
}
groovySrcDir.traverse(type: FILES, postDir: postDirVisitor, nameFilter: ~/.*\.groovy$/) {
if (it.name.size() < 15) {
countSmallFiles++
}
}
问题代码:
def datamap = [:]
def printDomainFound = {
//File currentFile = new File(it.canonicalPath)
def fileText = it.text
if(fileText.indexOf("@Table ") > 0){
//println "Found a Table annotation in ${it.name} "
datamap.put(it.name, it.name)
}
}
groovySrcDir.traverse type: FILES, visit: printDomainFound, nameFilter: filterGroovyFiles
datamap.each {
println it.key
}
行为我看到是在满足正则表达式匹配的目录中的第一个文件是打印的唯一行,然后处理停止(仅一行),即使为每个子目录都打印countSmallFiles。我使用的是Grails 2.3.7。我会尝试使用旧版本的grails运行,看看我是否得到相同的结果。 – Duane5000 2014-11-17 20:43:33
有点不同的方法,但为我工作: 'def groovySrcDir = new File('。','plugins /') def result = new AntBuilder()。fileset(dir:'./ plugins /',包括: '**/*。groovy'){ containsregexp expression:'。* @ Table。*'' } *。文件 result.each(){f - > \t def fileText = f.text \t Pattern p = Pattern.compile(“(@ Table | @NamedQuery)。*?(name)。*?(=)。( \\ S +)(\\))“); \t Matcher m = p.matcher(fileText); /* @NamedQuery不起作用。 (m.find())打印“$ {f.name} \ treferences table:”+ m.group(4).replace()时,我怀疑string.string的命名是打破REGEX */ \t “\”“,”“); \t} }' – Duane5000 2014-11-17 20:47:57
我不明白,如果我运行代码,我会得到一行所有带有”@Table“的文件。 。 – fonkap 2014-11-17 22:03:21