当前位置:K88软件开发文章中心编程工具Gradle → 文章内容

Gradle 使用文件

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-24 10:31:28

文件,作为一个文件树。你可以通过使用 Project.zipTree() 或 Project.tarTree()方法来实现这一过程。这些方法返回一个 FileTree 实例,您可以像使用任何其他文件树或文件集合一样使用它。例如,您可以用它来通过复制内容扩大归档,或把一些档案合并到另一个归档文件中。使用归档文件作为文件树build.gradle // Create a ZIP file tree using pathFileTree zip = zipTree('someFile.zip')// Create a TAR file tree using pathFileTree tar = tarTree('someFile.tar')//tar tree attempts to guess the compression based on the file extension//however if you must specify the compression explicitly you can:FileTree someTar = tarTree(resources.gzip('someTar.ext')) 指定一组输入文件Gradle 中的许多对象都有一个接受一组输入文件的属性。例如,JavaCompile 任务有一个 source 属性,定义了要编译的源代码文件。你可以使用上面所示的 files()方法所支持的任意类型的对象设置此属性。这意味着您可以通过如 File、String、集合、FileCollection 对象,或甚至是一个闭包来设置此属性。这里有一些例子:指定一组文件build.gradle // Use a File object to specify the source directorycompile { source = file('src/main/java')}// Use a String path to specify the source directorycompile { source = 'src/main/java'}// Use a collection to specify multiple source directoriescompile { source = ['src/main/java', '../shared/java']}// Use a FileCollection (or FileTree in this case) to specify the source filescompile { source = fileTree(dir: 'src/main/java').matching { include 'org/gradle/api/**' }}// Using a closure to specify the source files.compile { source = { // Use the contents of each zip file in the src dir file('src').listFiles().findAll {it.name.endsWith('.zip')}.collect { zipTree(it) } }} 通常情况下,有一个与属性相同名称的方法,可以追加这个文件集合。再者,这个方法接受 files()方法所支持的任何类型的参数。指定一组文件build.gradle compile { // Add some source directories use String paths source 'src/main/java', 'src/main/groovy' // Add a source directory using a File object source file('../shared/java') // Add some source directories using a closure source { file('src/test/').listFiles() }} 复制文件你可以使用 Copy 任务来复制文件。复制任务非常灵活,并允许您进行,比如筛选要复制的文件的内容,或映射文件的名称。若要使用 Copy 任务,您必须提供用于复制的源文件和目标目录。您还可以在复制文件的时候指定如何转换文件。你可以使用一个复制规范来做这些。一个复制规范通过 CopySpec 接口来表示。Copy 任务实现了此接口。你可以使用 CopySpec.from()方法指定源文件,使用 CopySpec.into()方法使用目标目录。使用 copy 任务复制文件build.gradle task copyTask(type: Copy) { from 'src/main/webapp' into 'build/explodedWar'} from() 方法接受和 files() 方法一样的任何参数。当参数解析为一个目录时,该目录下的所有文件(不包含目录本身) 都会递归复制到目标目录。当参数解析为一个文件时,该文件会复制到目标目录中。当参数解析为一个不存在的文件时,参数会被忽略。如果参数是一个任务,那么任务的输出文件 (即该任务创建的文件)会被复制,并且该任务会自动添加为 Copy 任务的依赖项。into() 方法接受和 files() 方法一样的任何参数。这里是另一个示例:示例 16.11. 指定复制任务的源文件和目标目录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 风格的包含或排除模式,或使用一个闭包,来选择要复制的文件:选择要复制的文件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()不能进行增量操作。使用没有最新状态检查的 copy() 方法复制文件build.gradle task copyMethod << { copy { from 'src/main/webapp' into 'build/explodedWar' include '**/*.html' include '**/*.jsp' }} 第二,当一个任务用作复制源(即作为 from() 的参数)的时候,copy()方法不能建立任务依赖性,因为它是一个方法,而不是一个任务。因此,如果您在任务的 action 里面使用 copy()方法,必须显式声明所有的输入和输出以得到正确的行为。使用有最新状态检查的 copy() 方法复制文件build.gradle task copyMethodWithExplicitDependencies{ inputs.file copyTask // up-to-date check for inputs, plus add copyTask as dependency 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() 方法旨在用于自定义任务中,需要文件复制作为其一部分功能的时候。在这种情况下,自定义任务应充分声明与

上一页  [1] [2] [3]  下一页


Gradle 使用文件