gradle--第十五章 使用文件3
来源:互联网 发布:html5网页小游戏源码 编辑:程序博客网 时间:2024/06/06 18:56
15.6 拷贝文件
你可以使用一些拷贝任务来拷贝文件。拷贝任务是非常灵活的,例如,它允许你在复制文件的时候过滤文件内容,并且映射到另外一个文件。
要使用拷贝任务,你必须提供一些要拷贝的源文件,以及拷贝后文件要存放的目录。你也可以描述文件在拷贝的过程中是怎么传输的,你可以使用一个copy spec来完成这些任务。一个copy spec 是由CopySpec接口表示的.CopyTask实现了这个接口。你可以通过CopySpec.from()方法来描述源文件,并使用Copy.into()来设置拷贝到的目录。
Example 15.10. Copying files using the copy task
build.gradle
task copyTask(type: Copy) {
from 'src/main/webapp'
into 'build/explodedWar'
}
from()方法接受和files()方法相同的任意参数。当参数是一个目录时,所有在这个目录下的文件都会递归的拷贝到新目录(除了目录本身自己)。当参数描述的是一个文件时,文件被拷贝到目标目录。当参数描述的文件不存在时,该参数被忽略。如果参数是一个任务,该任务的输出文件(例如任务创建的文件)都会被拷贝,并且该任务会添加到Copy任务的依赖中.into()方法接受和file()相同的任意参数。如下是一个例子:
Example 15.11. Specifying copy task source files and destination directory
build.gradle
task anotherCopyTask(type: Copy) {
// Copy everything under src/main/webapp
from 'src/main/webapp'
// Copy a single file
from 'src/staging/index.html'
// Copy the output of a task
from copyTask
// Copy the output of a task using Task outputs explicitly.
from copyTaskWithPatterns.outputs
// Copy the contents of a Zip file
from zipTree('src/main/assets.zip')
// Determine the destination directory later
into { getDestDir() }
}
你可以使用Ant风格的 include 或者exclude或者使用闭包来选择文件:
Example 15.12. Selecting the files to copy
build.gradle
task copyTaskWithPatterns(type: Copy) {
from 'src/main/webapp'
into 'build/explodedWar'
include '**/*.html'
include '**/*.jsp'
exclude { details -> details.file.name.endsWith('.html') &&
details.file.text.contains('staging') }
}
你也可以使用Project.copy()方法来复制文件。除了有一些限制外,它跟前面的复制任务一样工作。首先,copy()不是增量式的(查看第14.9章“Skipping tasks that are up-to-date”)
Example 15.13. Copying files using the copy() method without up-to-date check
build.gradle
task copyMethod << {
copy {
from 'src/main/webapp'
into 'build/explodedWar'
include '**/*.html'
include '**/*.jsp'
}
}
第二,copy()方法不能影响任务依赖当一个任务被使用为一个复制的源文件(例如作为from()的一个参数),因为他是一个方法而不是一个任务,如果使用copy方法作为任务动作的一部分,你必须明确定义所有的输入和输出来获取正确的行为。
Example 15.14. Copying files using the copy() method with up-to-date check
build.gradle
task copyMethodWithExplicitDependencies{
// up-to-date check for inputs, plus add copyTask as dependency
inputs.file copyTask
outputs.dir 'some-dir' // up-to-date check for outputs
doLast{
copy {
// Copy the output of copyTask
from copyTask
into 'some-dir'
}
}
}
任何可能的情况下,优先使用Copy任务,因为它不需要你做额外的工作就可以支持增量是构建和任务依赖接口。copy方法可以用来拷贝文件,作为任务实现的一部分。也就是说,copy方法设计用来为自定义的任务服务((see Chapter 58, Writing Custom Task Classes) t),这些自定义的任务需要拷贝文件来作为他们功能的一部分。在这种场景下,自定义任务必须充分的定义好copy动作相关的输入和输出文件
你可以使用一些拷贝任务来拷贝文件。拷贝任务是非常灵活的,例如,它允许你在复制文件的时候过滤文件内容,并且映射到另外一个文件。
要使用拷贝任务,你必须提供一些要拷贝的源文件,以及拷贝后文件要存放的目录。你也可以描述文件在拷贝的过程中是怎么传输的,你可以使用一个copy spec来完成这些任务。一个copy spec 是由CopySpec接口表示的.CopyTask实现了这个接口。你可以通过CopySpec.from()方法来描述源文件,并使用Copy.into()来设置拷贝到的目录。
Example 15.10. Copying files using the copy task
build.gradle
task copyTask(type: Copy) {
from 'src/main/webapp'
into 'build/explodedWar'
}
from()方法接受和files()方法相同的任意参数。当参数是一个目录时,所有在这个目录下的文件都会递归的拷贝到新目录(除了目录本身自己)。当参数描述的是一个文件时,文件被拷贝到目标目录。当参数描述的文件不存在时,该参数被忽略。如果参数是一个任务,该任务的输出文件(例如任务创建的文件)都会被拷贝,并且该任务会添加到Copy任务的依赖中.into()方法接受和file()相同的任意参数。如下是一个例子:
Example 15.11. Specifying copy task source files and destination directory
build.gradle
task anotherCopyTask(type: Copy) {
// Copy everything under src/main/webapp
from 'src/main/webapp'
// Copy a single file
from 'src/staging/index.html'
// Copy the output of a task
from copyTask
// Copy the output of a task using Task outputs explicitly.
from copyTaskWithPatterns.outputs
// Copy the contents of a Zip file
from zipTree('src/main/assets.zip')
// Determine the destination directory later
into { getDestDir() }
}
你可以使用Ant风格的 include 或者exclude或者使用闭包来选择文件:
Example 15.12. Selecting the files to copy
build.gradle
task copyTaskWithPatterns(type: Copy) {
from 'src/main/webapp'
into 'build/explodedWar'
include '**/*.html'
include '**/*.jsp'
exclude { details -> details.file.name.endsWith('.html') &&
details.file.text.contains('staging') }
}
你也可以使用Project.copy()方法来复制文件。除了有一些限制外,它跟前面的复制任务一样工作。首先,copy()不是增量式的(查看第14.9章“Skipping tasks that are up-to-date”)
Example 15.13. Copying files using the copy() method without up-to-date check
build.gradle
task copyMethod << {
copy {
from 'src/main/webapp'
into 'build/explodedWar'
include '**/*.html'
include '**/*.jsp'
}
}
第二,copy()方法不能影响任务依赖当一个任务被使用为一个复制的源文件(例如作为from()的一个参数),因为他是一个方法而不是一个任务,如果使用copy方法作为任务动作的一部分,你必须明确定义所有的输入和输出来获取正确的行为。
Example 15.14. Copying files using the copy() method with up-to-date check
build.gradle
task copyMethodWithExplicitDependencies{
// up-to-date check for inputs, plus add copyTask as dependency
inputs.file copyTask
outputs.dir 'some-dir' // up-to-date check for outputs
doLast{
copy {
// Copy the output of copyTask
from copyTask
into 'some-dir'
}
}
}
任何可能的情况下,优先使用Copy任务,因为它不需要你做额外的工作就可以支持增量是构建和任务依赖接口。copy方法可以用来拷贝文件,作为任务实现的一部分。也就是说,copy方法设计用来为自定义的任务服务((see Chapter 58, Writing Custom Task Classes) t),这些自定义的任务需要拷贝文件来作为他们功能的一部分。在这种场景下,自定义任务必须充分的定义好copy动作相关的输入和输出文件
0 0
- gradle--第十五章 使用文件3
- gradle--第十五章 使用文件1
- gradle--第十五章 使用文件2
- gradle--第十五章 使用文件4
- gradle--第十一章 使用Gradle命令行3
- Gradle 1.12 翻译——第十五章. 任务详述
- 第十五章 使用定义标签
- 第十五章--使用条件语句
- Gradle 1.12 翻译——第十六章. 使用文件
- 第十五章文件加载与保存
- 第十五章,读取txt文件(C++)
- Gradle第十二章:使用Gradle 图形用户界面
- gradle--第十一章 使用Gradle命令行
- gradle--第十一章 使用Gradle命令行2
- 第十五章:在应用程序中使用虚拟内存
- 第十五章:在应用程序中使用虚拟内存
- 使用java理解程序逻辑第十五章
- 第十五章 使用查询表达式的LINQ
- stm32使用超高精度的同步时钟的设计
- GoF 种设计模式
- 浅谈方法重写 super
- 面试10大算法汇总以及常见题目解答
- spring mvc拦截器和<mvc:annotation-driven />的详解
- gradle--第十五章 使用文件3
- Android Api Demos登顶之路(九十一)Graphics-->TouchPaint
- 23种经典设计模式UML类图汇总
- 文件编辑器vim--vim使用技巧
- null的数据类型
- JVM内存管理的机制
- LibSVM在x64windows下的安装与初次使用
- cvWaitKey函数说明
- Redis大冒险