Gradle基本使用(5):文件操作

Gradle 文件操作


构建任务中经常会涉及到文件操作,gradle 除了可以直接使用 groovy 的文件操作类,还提供了一系列的 API task 使得这个过程更加简便;

文件定位

可以使用 Project.file() 来查找一个基于项目相对路径的文件:
1
//将相对路径查找的 file 对象转化对应绝对路径创建的 file 对象
2
File configFile = file('src/config.xml')
3
configFile = file(configFile.absolutePath)
在 file() 中可以除了可以直接传入路径字符串,还可以传入以下参数:
  • FileCollection:文件集合;
  • Task:任务的输出文件;


文件集合

可以使用 FileCollection 接口表示一个文件集合,用于 Gradle API 中许多对象的配置参数,文件集合的声明方式如下:
1
FileCollection collection = files(参数1,参数2....)
2
//这些参数可以是文件路径字符串、File对象、文件路径字符串数组、File对象数组,甚至可以混合使用如下:
3
FileCollection collection = files('src/file1.txt', new File('src/file2.txt'), ['src/file3.txt', 'src/file4.txt'])  
也可以使用直接创建某个路径下的文件集合
1
File dir = file ('src')
2
FileCollectio collection = files{ dir.listFiles() }
文件集合的遍历
1
collection.each {File file ->
2
    println file.name
3
 }
文件集合可以进行合并、排除等集合操作,如下:
1
FileCollection collection = files { file('src').listFiles() }
2
def union = collection  + files('./file1.txt');           //合并操作
3
def different = collection - files('src/file1.txt');      //排除操作



文件树

文件树是按层次结构排序的文件集合,使用 FileTree 接口(继承自 FileCollection 接口),文件树可能表示一个目录树或 ZIP 文件的内容;
创建文件树
1
//方式1:使用一个基本目录创建
2
FileTree tree = fileTree(dir: 'src/main')
3
tree.include '**/*.java'           //向文件树添加文件
4
tree.exclude '**/Abstract*'        //从文件树排除文件
5
    
6
//方式2:可以对方式1实现链式调用
7
FileTree tree = fileTree(dir: 'src/main').include('**/*.java').exclude('**/Abstract*')
8
    
9
//方式3:使用map创建
10
FileTree tree = fileTree(dir: 'src', include: '**/*.java', exclude: '**/*test*/**')  
11
FileTree tree = fileTree(dir: 'src', include: ['**/*.java','**/*.xml'] exclude: '**/*test*/**')      
使用文件树
1
//遍历文件树
2
tree.each {File file ->
3
    println file
4
}
5
6
//过滤文件树
7
FileTree filtered = tree.matching {
8
    include 'org/gradle/api/**'
9
    exclude '**/Abstract*' 
10
}
11
12
//合并文件树
13
FileTree sum = tree + fileTree(dir: 'src/test')

创建 ZIP、TAR 等文件归档的文件树
1
FileTree zip = zipTree('someFile.zip')
2
FileTree tar = tarTree('someFile.tar')



复制文件 


复制文件
gradle 提供了 copy type 的 task api 用于实现简便的文件复制,基本使用如下:
1
task copyTask(type: Copy) {
2
    from 'src/main/webapp'
3
    into 'build/explodedWar'
4
}  
from,into 方法都可以接收 文件字符串类型参数,file对象,FileCollection对象,FileTree 对象,task返回参数等;
其中 form 参数可以有多个,可以是一般的文件,目录,ZIP、TAR等文件归档,如下:
1
task anotherCopyTask(type: Copy) {
2
    from 'src/main/webapp'
3
    from 'src/staging/index.html'
4
    from task3
5
    from copyTaskWithPatterns.outputs
6
    from zipTree('src/main/assets.zip')
7
    into { getDestDir() }
8
}  
同时在复制过程中可以对文件进行筛选
1
task copyTaskWithPatterns(type: Copy) {
2
    from 'src/main/webapp'
3
    into 'build/explodedWar'
4
    include '**/*.html'
5
    include '**/*.jsp'
6
    exclude { details -> details.file.name.endsWith('.html') && details.file.text.contains('staging') }  
7
    //其中exclude使用了一个闭包作为参数,details参数代指复制过程中的file
8
}  

重命名文件
1
task rename(type: Copy) {
2
    from 'src/main/webapp'
3
    into 'build/explodedWar'
4
        
5
    //使用闭包作为参数
6
    rename { String fileName ->
7
        fileName.replace('-staging-', '')
8
    }
9
    //使用正则表达式作为参数
10
    rename '(.+)-staging-(.+)', '$1$2'
11
}  













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值