| 使用 Project.file(java.lang.Object)方法,通过指定文件的相对路径或绝对路径来对文件的操作,其中相对路径为相对当前project[根 project 或者子 project]的目录。 |
| 其实使用 Project.file(java.lang.Object)方法创建的 File 对象就是 Java 中的 File 对象,我们可以使用它就像在 Java 中使用一样。 |
| |
| # 案例 |
| |
| 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中, 文件集合用FileCollection接口表示。我们可以使用Project.files(java.lang.Object[])方法来获得 |
| 一个文件集合对象 |
| |
| 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 |
| |
| Set set2 = collection as Set |
| |
| List list = collection as List |
| |
| |
| for (item in list) { |
| println item.name |
| } |
| |
| def union = collection + files('src/test5.txt') |
| |
| |
| def minus = collection - files('src/test3.txt') |
| union.forEach(){File it -> |
| println it.name |
| } |
| 文件树是有层级结构的文件集合,一个文件树它可以代表一个目录结构或一 ZIP 压缩包中的内容结构。文件树是从文件集合继承过来的, |
| 所以文件树具有文件集合所有的功能。我们可以使用 Project.fileTree(java.util.Map)方法来创建文件树对象,还可以使用过虑条件 |
| 来包含或排除相关文件 |
| |
| tree = fileTree('src/main').include('**/*.java') |
| |
| |
| tree = fileTree('src/main') { |
| include '**/*.java' |
| } |
| |
| tree = fileTree(dir: 'src/main', include: '**/*.java') |
| |
| tree = fileTree(dir: 'src/main', includes: ['**/*.java', '**/*.xml', '**/*.txt'], exclude: '**/*test*/**') |
| tree.each {File file -> |
| println file |
| println file.name |
| } |
| 我们可以使用 Copy 任务来拷贝文件,通过它可以过虑指定拷贝内容,还能对文件进行重命名操作等。Copy 任务必须指定一组需要拷贝的文件和 |
| 拷贝到的目录,这里使用 CopySpec.from(java.lang.Object[])方法指定原文件;使用CopySpec.into(java.lang.Object)方法指定目标目录 |
| |
| task copyTask(type: Copy) { |
| from 'src/main/resources' |
| into 'build/config' |
| } |
| |
| from()方法接受的参数和文件集合时 files()一样。当参数为一个目录时,该目录下所有的文件都会被拷贝到指定目录下(目录自身不会被拷贝); |
| 当参数为一个文件时,该文件会被拷贝到指定目录;如果参数指定的文件不存在,就会被忽略;当参数为一个 Zip 压缩文件,该压缩文件的内容会 |
| 被拷贝到指定目录 |
| into()方法接受的参数与本地文件时 file()一样。 |
| |
| task copyTask(type: Copy) { |
| |
| from 'src/main/webapp' |
| |
| from 'src/staging/index.html' |
| |
| from zipTree('src/main/assets.zip') |
| |
| into 'build/explodedWar' |
| } |
| |
| 在拷贝文件的时候还可以添加过虑条件来指定包含或排除的文件 |
| task copyTaskWithPatterns(type: Copy) { |
| from 'src/main/webapp' |
| into 'build/explodedWar' |
| include '**/*.html' |
| include '**/*.jsp' |
| exclude { |
| details -> details.file.name.endsWith('.html') |
| } |
| } |
| |
| 在拷贝文件的时候还可以对文件进行重命名操作 |
| |
| task rename(type: Copy) { |
| from 'src/main/webapp' |
| into 'build/explodedWar' |
| |
| rename { String fileName -> |
| fileName.replace('-staging-', '') |
| } |
| } |
| |
| Project.copy(org.gradle.api.Action)方法完成拷贝功能 |
| |
| task copyMethod { |
| doLast { |
| copy { |
| from 'src/main/webapp' |
| into 'build/explodedWar' |
| include '**/*.html' |
| include '**/*.jsp' |
| } |
| } |
| } |
| |
| 使用 project 对象的 copy 方法 |
| copy { |
| |
| from file('src/main/resources/ddd.txt') |
| into this.buildDir.absolutePath |
| } |
| |
| # 执行 gradle build 指令即可。去 build 目录的本地磁盘查看 |
| |
| 通常一个项目会有很多的 Jar 包,我们希望把项目打包成一个 WAR,ZIP 或 TAR 包进行发布,这时我们就可以使用Zip,Tar,Jar,War 和 Ear 任务 |
| 来实现,不过它们的用法都一样,所以在这里我只介绍 Zip 任务的示例。首先,创建一个 Zip 压缩文件,并指定压缩文件名称 |
| |
| apply plugin: 'java' |
| version=1.0 |
| task myZip(type: Zip) { |
| from 'src/main‘ |
| into ‘build’ |
| baseName = 'myGame' |
| } |
| println myZip.archiveName |
| |
| # 命令行执行 |
| gradle -q myZ |
| |
| # 输出结果 |
| myGame-1.0.zip |
| |
| 可以使用 Project.zipTree(java.lang.Object)和 Project.tarTree(java.lang.Object)方法来创建访问 Zip 压缩包的文件树对象 |
| |
| |
| FileTree zip = zipTree('someFile.zip') |
| |
| FileTree tar = tarTree('someFile.tar') |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术