Gradle学习[5]——文件操作

Gradle学习[5]——文件操作

本文会列举几个Gradle中常用的文件操作方式,在Groovy中不能这样操作,需要引入gradle-core-api-7.5.jar

文件操作

在Gradle中可以使用Project.file(java.lang.Object)方法(Project可省略),指定文件路径获取文件对象来进行操作,对象API为Java的File对象。

1
2
3
4
5
6
7
8
9
//使用相对路径
File configFile = file('src/conf.xml')
configFile.createNewFile();
// 使用绝对路径
configFile = file('D:\\conf.xml')
println(configFile.createNewFile())
// 使用一个文件对象
configFile = new File('src/config.xml')
println(configFile.exists())

文件集合

在Gradle中可以使用Project.files(java.lang.Object[])方法获取文件集合对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def collection = files('src/test1.txt',new File('src/test2.txt'),['src/test3.txt', 'src/test4.txt']) collection.forEach(){File it ->
it.createNewFile() //创建该文件
println it.name //输出文件名
}
Set set1 = collection.files // 把文件集合转换为java中的Set类型
Set set2 = collection as Set
List list = collection as List// 把文件集合转换为java中的List类型
for (item in list) {
println item.name
}
def union = collection + files('src/test5.txt') // 添加或者删除一个集合
def minus = collection - files('src/test3.txt') union.forEach(){
println it.name
}

文件树

文件树能够表示文件的层级结构,文件树对象是从文件集合继承过来的,具有文件集合的所有功能。

在Gradle中可以使用Project.fileTree(java.util.Map)方法创建文件树对象。

1
2
3
4
5
6
7
8
9
10
tree = fileTree('src/main').include('**/*.java')// 第一种方式:使用路径创建文件树对象,同时指定包含的文件
//第二种方式:通过闭包创建文件树:
tree = fileTree('src/main') {
include '**/*.java'
}
tree = fileTree(dir: 'src/main', include: '**/*.java') //第三种方式:通过路径和闭包创建文件树:具名参数给map传值tree = fileTree(dir: 'src/main', includes: ['**/*.java', '**/*.xml', '**/*.txt'], exclude: '**/*test*/**')
tree.each {
// 遍历文件树的所有文件
println file println file.name
}

对Zip压缩包创建文件树对象

1
FileTree zip = zipTree('someFile.zip')

对Tar压缩包创建文件树对象

1
FileTree tar = tarTree('someFile.tar')

文件复制

在Gradle中可以使用默认提供的Copy任务来完成文件复制操作。

1
2
3
4
task 'copyTask'(type:Copy) {
from './Groovy' // 目录
into './Groovy1'
}

它还可以复制压缩包中的文件

1
2
3
4
5
6
7
8
9
10
task copyTask(type: Copy) {
// 拷贝src/main/webapp目录下所有的文件
from 'src/main/webapp'
// 拷贝单独的一个文件
from 'src/staging/index.html'
// 从Zip压缩文件中拷贝内容
from zipTree('src/main/assets.zip')
// 拷贝到的目标目录
into 'build/explodedWar'
}

还可以指定包含、排除条件

1
2
3
4
5
task copyTaskWithPatterns(type: Copy) { 
from 'src/main/webapp'
into 'build/explodedWar' include '**/*.html' include '**/*.jsp'
exclude { details -> details.file.name.endsWith('.html') }
}

还可以进行重命名操作

1
2
3
4
5
6
task rename(type: Copy) { 
from 'src/main/webapp'
into 'build/explodedWar'
// 使用一个闭包方式重命名文件
rename { String fileName -> fileName.replace('-staging-', '') }
}

除了在定义任务时指定type外,还有其他的使用方式,如在一个task中

1
2
3
4
5
6
7
8
9
10
task copyMethod { 
doLast {
copy {
from 'src/main/webapp'
into 'build/explodedWar'
include '**/*.html'
include '**/*.jsp'
}
}
}

或者直接在脚本最外层Project对象中

1
2
3
4
5
copy {
//相对路径或者绝对路径
from file('src/main/resources/ddd.txt') //file也可以换成new File()
into this.buildDir.absolutePath
}

文件归档

可以将多个文件打成War、Jar、Zip、Tar包,对应的包都有对应的任务实现了。

Zip包

1
2
3
4
5
task myZip(type: Zip) { 
from 'src/main‘
into ‘build’ //保存到build目录中
baseName = 'myGame'
}

Gradle学习[5]——文件操作
https://blog.gugu.dev/2024-02-21/Gradle学习-5-——文件操作/
作者
MinMin
发布于
2024年2月21日
许可协议