Gradle 操纵文件

CopyTask

将文件从一个目录拷贝到另一个目录,也可以拷贝副本。

全部拷贝到另外一个目录
Example 1-1. A trivial copy task configuration
task copyPoems(type: Copy) {
 from 'text-files'
 into 'build/poems'
}
除了指定名称的文件,其他都拷贝到另外一个目录
Example 1-2. A copy task that copies all the poems except the one by Henley
task copyPoems(type: Copy) {
from 'text-files'
into 'build/poems'
exclude '**/*henley*'
}
只拷贝指定名称的文件
Example 1-3. A copy task that only copies Shakespeare and Shelley
task copyPoems(type: Copy) {
from 'text-files'
into 'build/poems'
include '**/sh*.txt'
}
将多个目录的文件拷贝到一个目录
Example 1-4. A copy task taking files from more than one source directory
task complexCopy(type: Copy) {
 from('src/main/templates' ) {
 include '**/*.gtpl'
 }
 from('i18n' )
from('config' ) {
 exclude 'Development*.groovy'
 }
 into 'build/resources'
}
拷贝文件的时候保留目录结构
Example 1-5. A copy task mapping source directory structure onto a new destination structure
task complexCopy(type: Copy) {
 from('src/main/templates' ) {
 include '**/*.gtpl'
 into 'templates'
 }
 from('i18n' )
 from('config' ) {
 exclude 'Development*.groovy'
 into 'config'
 }
 into 'build/resources'
}
拷贝的时候使用正则表达式重命名文件
Example 1-6. Renaming files using regular expressions
taskrename(type:Copy) {
from 'source'
into 'dest'
rename(/file-template-(\d+)/,'production-file-$1.txt' )
}

Groovy编程方式重命名文件
Example 1-7. Renaming files programmatically
taskrename(type:Copy) {
from 'source'
into 'dest'
rename { fileName ->
"production-file${(fileName - 'file-template')}"
}
}


Example 1-8. Copying a file with keyword expansion
versionId= '1.6'
task copyProductionConfig(type:Copy) {
from 'source'
include 'config.properties'
into 'build/war/WEB-INF/config'
expand([
databaseHostname: 'db.company.com' ,
version: versionId,
buildNumber: (int)(Math.random() *1000),
date: new Date()
])
}

Example 1-9. Use filter() with a closure to transform a text file as it is copied
importcom.petebevin.markdown.MarkdownProcessor
buildscript{
repositories {
mavenRepo url: 'http://scala-tools.org/repo-releases'
}

dependencies{
classpath 'org.markdownj:markdownj:0.3.0-1.0.2b4'
}
}
task markdown(type:Copy) {
defmarkdownProcessor= new MarkdownProcessor()
into 'build/poems'
from 'source'
include 'todo.md'
rename { it - '.md' + '.html' }
filter { line ->
markdownProcessor.markdown(line)
}
}


Example 1-10. Use filter() with a custom Ant Filter class to transform a text file as it is
copied
importorg.apache.tools.ant.filters.*
import com.petebevin.markdown.MarkdownProcessor
buildscript{
repositories {
mavenRepo url: 'http://scala-tools.org/repo-releases'
}
dependencies {
classpath 'org.markdownj:markdownj:0.3.0-1.0.2b4'
}
}
classMarkdownFilterextends FilterReader{
MarkdownFilter(Reader input) {
super(newStringReader(newMarkdownProcessor().markdown(input.text)))
}
}
task copyPoem(type:Copy) {
into 'build/poems'
from 'source'
include 'todo.md'
rename { it - ~/\.md$/ + '.html' }
filter MarkdownFilter
}


Example 1-11. Use eachFile() to accumulate a hash of several files
importjava.security.MessageDigest;
task copyAndHash(type:Copy) {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
into 'build/deploy'
from 'source'
eachFile { fileCopyDetails ->
sha1.digest(fileCopyDetails.file.bytes)
}
doLast {
Formatter hexHash = new Formatter()
sha1.digest().each { b -> hexHash.format('%02x', b) }
println hexHash
}
}


Example 1-12. Trying to set the destinationDir property of a Jar task with a string
jar{
destinationDir = 'build/jar'
}


Example 1-13. Trying to set the destinationDir property of a Jar task with a string
jar{
destinationDir = new File('build/jar')
}


Example 1-14. Setting the destinationDir property of a Jar task using the file() method
jar{
destinationDir = file('build/jar')
}





 
   
 
   
Example 1-15. Using fileTree() with includes and excludes
defnoBackups= fileTree('src/main/java') {
exclude '**/*~'
}
defxmlFilesOnly= fileTree('src/main/java') {
include '**/*.xml'
}


Example 1-16. Using fileTree() with includes and excludes given in a map literal
defnoBackups= fileTree(dir:'src/main/java' , excludes: ['**/*~' ])
defxmlFilesOnly= fileTree(dir:'src/main/java' , includes: ['**/*.xml' ])


Example 1-17. The default toString() implementation of a FileCollection
taskcopyPoems(type:Copy) {
from 'text-files'
into 'build/poems'
}
println "NOT HELPFUL:"
println files(copyPoems)


Example 1-18. A more useful way to look at a FileCollection
taskcopyPoems(type:Copy) {
from 'text-files'
into 'build/poems'
}
println "HELPFUL:"
println files(copyPoems).files

Example 1-19. The base build from which we will derive FileCollection examples
applyplugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-context:3.1.1.RELEASE'
}


Example 1-20. A naive way to list source files
task naiveFileLister{
doLast {
println fileTree('src/main/java'). files
}
}


Example 1-21. Printing out all of the compile-time dependencies of the build as a pathlike string
println configurations.compile.asPath

Example 1-22. Using module dependencies as a FileCollection to capture JAR files.
taskcopyDependencies(type:Copy) {
from configurations.compile
into 'lib'
}


Example 1-23. Using FileCollection addition to create a runtime classpath
taskrun(type:JavaExec) {
main = 'org.gradle.example.PoetryEmitter'
classpath = configurations.compile + sourceSets.main.output
}


Example 1-24. Creating an intersection of two FileCollections
defpoems= fileTree(dir:'src/main/resources' , include: '*.txt' )
defromantics= fileTree(dir:'src/main/resources' , include: 'shelley*' )
defgoodPoems= poems - romantics

Example 1-25. Printing out the collection of source files in the main Java SourceSet
println sourceSets.main.allSource.files

Example 1-26. Printing out the output directories the Java compiler will use for the
main source set.
println sourceSets.main.output.files


来自《Gradle_Beyond_the_Basics.pdf》:http://pan.baidu.com/s/1jPeNG



你可能感兴趣的:(GRADLE)