gradle--第十五章 使用文件2

来源:互联网 发布:html5网页小游戏源码 编辑:程序博客网 时间:2024/06/14 02:44

15.3 文件树
一个文件树就是一些文件以层级的形式进行排布。例如,一个文件树可能代表一个目录树或者一个zip文件的内容。它由FileTree接口来进行描述。FileTree接口继承自FileCollection接口,所以你也可以把文件树当作为一个文件集合。Gradle中有些些对象继承于FileTree接口,例如source sets.
获取FileTree实例的一个方法是使用Project.fileTree()方法。它创建了一个文件树,并且定义了一些基本的目录,并且有一些Ant风格的include和exclude样式可用。
Example 15.5. Creating a file tree


build.gradle
// Create a file tree with a base directory
FileTree tree = fileTree(dir: 'src/main')


// Add include and exclude patterns to the tree
tree.include '**/*.java'
tree.exclude '**/Abstract*'


// Create a tree using path
tree = fileTree('src').include('**/*.java')


// Create a tree using closure
tree = fileTree('src') {
    include '**/*.java'
}


// Create a tree using a map
tree = fileTree(dir: 'src', include: '**/*.java')
tree = fileTree(dir: 'src', includes: ['**/*.java', '**/*.xml'])
tree = fileTree(dir: 'src', include: '**/*.java', exclude: '**/*test*/**')
使用文件树跟使用文件接口是一样的。你也可以通过使用Ant风格样式访问文件树中的内容:
Example 15.6. Using a file tree


build.gradle
// Iterate over the contents of a tree
tree.each {File file ->
    println file
}


// Filter a tree
FileTree filtered = tree.matching {
    include 'org/gradle/api/**'
}


// Add trees together
FileTree sum = tree + fileTree(dir: 'src/test')


// Visit the elements of the tree
tree.visit {element ->
    println "$element.relativePath => $element.file"
}
15.4 把一个存档文件的内容当作文件树
你可以通过使用Project.zipTree或者Project.tarTree方法来把一个归档文件的内容,如一个zip或者Tar文件,当作一个文件树。这两个接口返回一个FileTree实例,你也可像使用其他文件树或者文件接口一样来使用这个文件树实例。例如,你可以使用他通过复制一个归档文件的内容,或者合并一些归档文件到另一个文件来扩展归档文件的内容
Example 15.7. Using an archive as a file tree


build.gradle
// Create a ZIP file tree using path
FileTree zip = zipTree('someFile.zip')


// Create a TAR file tree using path
FileTree 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'))
15.5 描述一些输入文件
在Gradle中许多的对象都拥有一些属性,这些属性可以接收一系列的文件输入。例如,JavaCompile任务拥有一个source属性,这个属性定义了要编译的源代码文件,你可以通过使用file()支持的方法来定义这个属性的值,这写方法在上面已经使用过。这也意味着你如下方法可以设置一个属性:一个文件、字符串、集合、文件集合、获取甚至是一个闭包,如下是一些实例:
Example 15.8. Specifying a set of files


build.gradle
// Use a File object to specify the source directory
compile {
    source = file('src/main/java')
}


// Use a String path to specify the source directory
compile {
    source = 'src/main/java'
}


// Use a collection to specify multiple source directories
compile {
    source = ['src/main/java', '../shared/java']
}


// Use a FileCollection (or FileTree in this case) to specify the source files
compile {
    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()方法支持的所有类型:
Example 15.9. Specifying a set of 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() }
}  
0 0
原创粉丝点击