Gradle文件樹

2020-07-24 16:00 更新

文件樹就是一個(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"
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)