I’m trying to use FileTree to navigate a file structure. I need to navigate inside .svn folders, but it seems like the .svn content is excluded by default. If I do something like this:
FileTree tree = fileTree {
from ‘.’
include ‘**/.svn/*.txt’
}
tree.each { File file ->
println file
}
even if, as a proof of concept, I have some .txt files inside .svn folders, they’re not found. If I rename .svn folders to .svn1, and I change that inclusion pattern, .txt files are found. It sounds like Gradle is applying the Ant default excludes (see: http://ant.apache.org/manual/dirtasks.html#defaultexcludes). Is there a way to disable this behaviour?
Did you try other setters from the FileTree interface (e.g. excludes = […] for complete overwrite of the existing excludes)? If that does not help you might need to fall back to ant.
As groovy has no respect for privates, we can access the patternSet which does the including/excluding for the DefaultConfigurableFileTree you get out from the ‘fileTree’ call.
Try the following:
FileTree tree = fileTree {
from '.'
include '* * /.svn/ * *' //remove the spaces, the forum format messes this up
patternSet.globalExcludes = []
}
tree.each { File file ->
println file
}
it aint pretty but should work.
If any of the gurus would like to chime in with the ‘correct’ way to do this (as I’m assuming somebody set that member variable to private for a reason), I would also like to know the answer.
[EDIT: and this is actually worse than I thought. 2am so I wasn’t thinking straight. The above code is actually resetting the static global excludes for all patternSets in gradle. Nasty. You might want to perform your operation and then reset the globals back to what they were before.
Actually, the public getter getPatterns() exposed by ConfigurableFileTree (and inherited from DirectoryTree) avoids the need to access the private patternSet property of DefaultConfigurableFileTree. However, the problem that global excludes are defined as static remain…
I hear you on this not being optimal. Voted for the JIRA.
While we wait for the solution it might be worth considering an excursion into ant-land. Since ant does give us the option of disabling the default excludes we can write code like the following:
task scan << {
ant.fileScanner {
fileset(dir: ".", defaultExcludes: false) {
include name: "** /* .txt" //remove spaces, forum syntax issue
}
}.each { File f ->
println f
}
// or if you like concise and don't have too many patterns,
// you can stick the includes on the same line
ant.fileScanner {
fileset(dir: ".", defaultExcludes: false, includes: "** /* .zip") //remove spaces, forum syntax issue
}.each { File f ->
println f
}
}