Gradle - how to iterate in fileTree only for certain type of file
Asked Answered
P

1

7

in my gradle task I iterate through fileTree and all works good:

myTask {
  fileTree("${project.projectDir}/dir").visit { FileVisitDetails details ->
    exec {
      //do some operations
    }
  }
}

but now I have different types of files in my directory:

dir
├── sub1
│   ├── file1.json
│   └── file2.js
├── sub2
│   ├── file1.json
│   └── file2.js
└── sub3
    ├── file1.js
    └── file2.json

How to iterate for only certain type of files? Because

"${project.projectDir}/folder/dir/**/*.json"

doesnt work.

Thanks for any advice

Perales answered 7/11, 2018 at 13:41 Comment(0)
A
9

You should use the matching method from FileTree. It uses a PatternFilterable as parameter.

Try that :

fileTree("${project.projectDir}/dir").matching {
    include "**/*.json"
}.each {
    // do some operations
}
Amelioration answered 7/11, 2018 at 13:51 Comment(1)
With some changes: fileTree("${project.projectDir}/dir/").matching { include "**/*.json" }.visit { FileVisitDetails details -> exec { //do some operations } it works! :) Thanks for helpPerales

© 2022 - 2024 — McMap. All rights reserved.