With the Gradle copy task we can define renaming rules for the files that are copied. We use the rename() method of the copy task to define the naming rules. We can use a closure where the filename is the argument of the closure. The name we return from the closure is the new name of copied file. Or we can define a regular expression and set the replacement value for the corresponding regular expression. We can use groups in the regular expression and use them in the replacement value like $<group>.

0.task copyFiles(type: Copy) {
1.from 'src/files'
2.into "$buildDir/files"
3.rename '(.*)-(.*).html', '$2/$1.html'
4.rename ~/(.*).template.(.*)/, '$1.$2'
5.rename { filename ->
6.filename.replace 'java', 'groovy'
7.}
8.}

Let's create some source files, so the renaming rules can be applied to them.

src/files/index-en.html:

<html>
<body>
<h1>Hello Gradle</h1>
</body>
</html>

src/files/index-nl_NL.html:

<html>
<body>
<h1>Hallo Gradle</h1>
</body>
</html>

src/files/sample.template.txt:

Sample file.

src/files/Sample.java:

public class Sample {
private String gradle = "Gradle";
}

We run $ gradle copyFiles and we get the following files in build/files:

nl_NL
|
+-- index.html
en
|
+-- index.html
Sample.groovy
sample.txt

Gradle Goodness: Renaming Files while Copying的更多相关文章

  1. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

  2. Gradle Goodness: Parse Files with SimpleTemplateEngine in Copy Task

    With the copy task of Gradle we can copy files that are parsed by Groovy's SimpleTemplateEngine. Thi ...

  3. Gradle Goodness: Task Output Annotations Create Directory Automatically

    Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...

  4. Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

    Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. ...

  5. Could not resolve all files for configuration;Andriod在build.gradle添加compile files()报错

    在build.gradle中添加个 compile files('libs/alipaySdk-20170922.jar') 就一直报这个错误 Error:Could not resolve all ...

  6. Gradle Goodness: Add Incremental Build Support to Tasks

    Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unle ...

  7. Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations

    In a previous post we learned how we can use the inputs and outputs properties to set properties or ...

  8. Gradle Goodness: Automatic Clean Tasks

    Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This ...

  9. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

随机推荐

  1. MySql:局域网和权限用户管理

    MySql 5.6(XP)/5.7(win7) 添加用户和设置局域访问权限操作.请在 http://sourceforge.net/  下载MySql Control Center(不是安装版本). ...

  2. HTML5触摸事件演化tap事件

    触摸事件是移动浏览器特有的HTML5事件,虽然click事件在pc和移动端更通用,但是在移动端会出现300ms延迟,较为影响用户体验,300ms延迟来自判断双击和长按,因为只有默认等待时间结束以确定没 ...

  3. BZOJ2476: 战场的数目(矩阵快速幂)

    题意 题目链接 Sol 神仙题Orzzz 考虑两边是否有\(1\) 设\(f[i]\)表示周长为\(2i\)的方案数 第一种情况:左侧或右侧有一个1,那么把这个1删去,对应的方案数为\(f[i - 1 ...

  4. CSS属性之padding

    0.inline元素中的padding 大家都知道padding对于block元素和inline-block元素的影响,而对于inline元素,padding只会在水平方向产生影响,垂直方向不会产生影 ...

  5. php备注

    一.关于OOP 1.PHP目前不支持方法重载

  6. 如何从只会 C++ 语法的水平到达完成项目编写软件的水平?

    原文:https://www.zhihu.com/question/29702729 学习 C++ 有一段时间了,但只是停留在熟悉语法阶段,在看 C++primer,不过感觉这本书比较深奥,不太适合, ...

  7. HTTP状态码和HTTP请求头

    HTTP报文是在Web服务器和浏览器之间进行交换的文本数据,一种是从浏览器发出的请求,一种是服务器发出的响应. 请求报文的第一行包括:1.请求方法 2.当前使用的HTTP协议版本 3.请求地址 GET ...

  8. java.lang.IllegalArgumentException: XML fragments parsed from previous mappers does not contain value for

    使用mybatis做一个简单的查询的时候,报了这个问题.代码如下: <mapper namespace="cn.gaiay.business.zm.live.living.dao.Li ...

  9. 中文乱码(Python、WEB、ajax)

    http://my.oschina.net/leejun2005/blog/74430 #查看errorb是unicode,还是stringprint isinstance(errorb,unicod ...

  10. 第三课 java编程入门

    java特点: 1.面对象性 2.可移植性/跨平台性 java组成: jdk(java工具开发工具包) /       \              \ jre       指令集合   api和常用 ...