问题
I am using the fileTree utility to get a list of files but need to exclude all directories except a select few from the list.
My directory structure:
node_modules/
react-native/
react/
third-party-package/
another-package/
I need to exclude all directories except the ones named react-native and react.
Currently I am excluding all node_modules, but this is not what I want...
fileTree(dir: '.', excludes: [ 'node_modules/**'] )
How would I do this with the fileTree?
回答1:
You can use a closure for this. Something like this:
fileTree(dir: '.').exclude {details ->
(details.file.canonicalPath.contains("node_modules")
&& !details.file.canonicalPath.contains('react'))}
You can provide your own condition for files, you want to exlude. Here is a simpliest one, where the files paths are checked, whether they contain node_modules
and not react
and if condition is true, this files are excluded.
Sure, you have to make it more specific, but you are free to provide any condition for you requirements.
来源:https://stackoverflow.com/questions/38207029/gradle-filetree-exclude-all-except-certain-directories