Gradle Goodness: Copy Files with Filtering

Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method to define a filter. The good news is we can reuse the Ant filtering classes from the org.apache.tools.ant.filters package. We define the filtering class and can pass parameters for the filter. Or we can pass a closure which is passed each line as an argument. Within the closure we must return the filtered line.

00.import org.apache.tools.ant.filters.*
01. 
02.task('filterCopy', type: Copy) {
03.from 'src/templates'
04.into buildDir
05.include '**/*.txt'
06.filter { line -> line.contains('Gradle') ? line : '' }
07.filter(ReplaceTokens, tokens: [author: 'mrhaki', gradleVersion: gradle.gradleVersion])
08.filter(ConcatFilter, prepend: file('src/include/header.txt'))
09.}

Now let's create a sample text file that will get filtered in src/templates/HelloGradle.txt:

This is just a simple text file. This line will not make it.
We show filtering capabilities of Gradle copy.
This file is written by @author@ with Gradle version @gradleVersion@.

And we create the file src/include/header.txt:

Include this header file.
-------------------------

After we run $ gradle filterCopy we get the following contents for the file build/HelloGradle.txt:

Include this header file.
-------------------------
We show filtering capabilities of Gradle copy in combination with the Ant filtering types.
This file is written by mrhaki with Gradle version 0.9-rc-1.

最新文章

  1. javascript学习笔记之array.sort
  2. CSS的::selection使用方法
  3. iOS RSA加密解密及签名验证
  4. [转]HTTP请求模型和头信息
  5. win32 sdk显示一个载入的位图的方法
  6. C++学习笔记(十二):类继承、虚函数、纯虚函数、抽象类和嵌套类
  7. 强烈推荐android studio用的几个插件,androidstudio
  8. (转)SQL流程控制语句学习(二):begin…end if…else case
  9. keyStore很重要,千万不能丢失
  10. 使用xorm工具,根据数据库自动生成 go 代码
  11. Robots.txt - 禁止爬虫
  12. 开源项目——小Q聊天机器人V1.5
  13. Odoo 学习【一】http & rpc
  14. 自己设置 WiFi
  15. IMDG
  16. java 中int与integer的区别
  17. .NET并行计算和并发8-QueueUserWorkItem异步
  18. ceph S3测试--cosbench
  19. SQL数据库增量备份还原方式
  20. _findfirst和_findnext

热门文章

  1. 线性规划费用流解法(Bzoj1061: [Noi2008]志愿者招募)
  2. jquery特效:无缝向上循环滚动列表
  3. Maven学习总结(六):pom.xml文件的说明
  4. PHP读取Excel类文件
  5. js图片跟随鼠标移动
  6. Linux 添加yum命令
  7. Spring Tech
  8. linux shell实现守护进程 看门狗 脚本
  9. mongodb shell警告
  10. react native学习资料