Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 478
Adrian Avatar asked Nov 07 '18 13:11

Adrian


People also ask

What is FileTree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is afterEvaluate in Gradle?

> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.

What is Gradle doLast?

The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build.


1 Answers

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
}
like image 51
ToYonos Avatar answered Jan 04 '23 00:01

ToYonos