W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
文件樹就是一個(gè)按照層次結(jié)構(gòu)分布的文件集合,例如,一個(gè)文件樹可以代表一個(gè)目錄樹結(jié)構(gòu)或者一個(gè) ZIP 壓縮文件的內(nèi)容.它被抽象為 FileTree 結(jié)構(gòu),FileTree 繼承自 FileCollection,所以你可以像處理文件集合一樣處理文件樹, Gradle 有些對(duì)象實(shí)現(xiàn)了FileTree 接口,例如 源集合. 使用 Project.fileTree() 方法可以得到 FileTree 的實(shí)例,它會(huì)創(chuàng)建一個(gè)基于基準(zhǔn)目錄的對(duì)象,然后視需要使用一些 Ant-style 的包含和去除規(guī)則.
例 15.5 創(chuàng)建文件樹 build.gradle
/以一個(gè)基準(zhǔn)目錄創(chuàng)建一個(gè)文件樹
FileTree tree = fileTree(dir: 'src/main')
// 添加包含和排除規(guī)則
tree.include '**/*.java'
tree.exclude '**/Abstract*'
// 使用路徑創(chuàng)建一個(gè)樹
tree = fileTree('src').include('**/*.java')
// 使用閉合創(chuàng)建一個(gè)數(shù)
tree = fileTree('src') {
include '**/*.java'
}
// 使用map創(chuàng)建一個(gè)樹
tree = fileTree(dir: 'src', include: '**/*.java')
tree = fileTree(dir: 'src', includes: ['**/*.java', '**/*.xml'])
tree = fileTree(dir: 'src', include: '**/*.java', exclude: '**/*test*/**')
就像使用文件集合一樣,你可以訪問文件樹的內(nèi)容,使用 Ant-style 規(guī)則選擇一個(gè)子樹。
例 15.6 使用文件樹 build.gradle
// 遍歷文件樹
tree.each {File file ->
println file
}
// 過濾文件樹
FileTree filtered = tree.matching {
include 'org/gradle/api/**'
}
// 合并文件樹A
FileTree sum = tree + fileTree(dir: 'src/test')
// 訪問文件數(shù)的元素
tree.visit {element ->
println "$element.relativePath => $element.file"
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: