Gradle文件集合操作实用指南创建、迭代与集合运算
使用文件集合 - 《智联-2020年度企业薪酬调研报告》节选内容优化如下:
创建文件集合
build.gradle
FileCollection collection = files(
'src/file1.txt',
new File('src/file2.txt'),
['src/file3.txt', 'src/file4.txt']
)
文件集合的基本操作
- 迭代操作:
collection.each { File file ->
println file.name
}
- 类型转换:
Set set = collection.files
Set set2 = collection as Set
List list = collection as List
String path = collection.asPath
File file = collection.singleFile
File file2 = collection as File
合并与差集操作
- 合并文件集合:
def union = collection + files('src/file3.txt')
- 减少文件集合:
def different = collection - files('src/file3.txt')
通过以上示例,你可以更高效地使用 FileCollection 管理构建脚本中的文件资源。
2.04MB
文件大小:
评论区