当我在macrodef中的groovy任务中使用ant macrodef属性时,我无法运行移动文件的代码。
<macrodef name="dirmove">
<attribute name="todir" />
<attribute name="fromdir" />
<attribute name="includes" default="*" />
<sequential>
<var name="todir" value="@{todir}" />
<var name="fromdir" value="@{fromdir}" />
<var name="includes" value="@{includes}" />
<groovy>
File dir1 = new File(properties.'fromdir');
File dir2 = new File(properties.'todir');
def pattern = properties.get('includes')
println pattern;
dir1.eachFileMatch ~/pattern/, {
f->
boolean fileMoved = f.renameTo(new File(dir2, f.getName()));
//assert f.name == '1.txt' //**because File object is immutable, so I am just checking for the existing of previous file name. It is still there.
println fileMoved;
}
</groovy>
</sequential>
</macrodef>
此代码正确打印出来自属性值的pattern的值。但是eachFileMatch函数没有获取规范
答案 0 :(得分:0)
dir1.eachFileMatch ~/pattern/, {
应该是
dir1.eachFileMatch ~/${pattern}/, {
由于pattern是String变量,因此您需要将其添加到正则表达式模式中。
以前,您只是搜索名为pattern