FileTree and default excludes?

(mauromol) #1

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: Is there a way to disable this behaviour?


(Szczepan Faber) #2

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.

(mauromol) #3

I do not want to exclude anything, so that’s not a viable solution.

I think I have to open an issue on Gradle Jira…

(Matias Bjarland) #4

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.

You can reset them by using:



(Matias Bjarland) #5

For reference, gradle uses the default excludes from the ant directory scanner. Below is a groovysh session listing the whole shebang:

nadurra:gradle mbjarland$ groovysh  Groovy Shell (1.8.2, JVM: 1.6.0_26)  Type 'help' or '\h' for help.  ------------------------------------------------------------------------------------------------------------------------------  groovy:000> new { println it }  **/*~  **/#*#  **/.#*  **/%*%  **/._*  **/CVS  **/CVS/**  **/.cvsignore  **/SCCS  **/SCCS/**  **/vssver.scc  **/.svn  **/.svn/**  **/.git  **/.git/**  **/.gitattributes  **/.gitignore  **/.gitmodules  **/.hg  **/.hg/**  **/.hgignore  **/.hgsub  **/.hgsubstate  **/.hgtags  **/.bzr  **/.bzr/**  **/.bzrignore  **/.DS_Store  ===> [Ljava.lang.String;@56b4d39c  groovy:000>

you can get the same listing using:

fileTree('.').patternSet.GLOBAL_EXCLUDES.each {
    println it

in your gradle build file.

(mauromol) #6

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…

(Matias Bjarland) #7

Yeah, I saw that post mortem. What I get for answering questions in the wee hours.

(Matias Bjarland) #8

Mauro, does this answer your question?

(mauromol) #9

Well, sort of… I will mark your answer as “good answer”, thank you. Anyway, I opened GRADLE-1883, asking for an improvement.

(Matias Bjarland) #10

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

(gus.heck) #11

Or you can use the ant defaultexcludes task to modify the default excludes however you like:

(Matias Bjarland) #12

And so we learn something new : ) Thanks for the pointer.