Gradle Goodness: Renaming Files while Copying

本文介绍如何在Gradle中通过自定义rename()方法实现文件重命名规则,包括使用闭包、正则表达式及group进行复杂操作。

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

转载于:https://www.cnblogs.com/GoAhead/p/4189089.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值