访问Groovy中.eachFileMatch()文件中模式变量的ant属性值
当我在macrodef中使用groovy任务中的ant macrodef属性时,我无法运行用于移动文件的代码访问Groovy中.eachFileMatch()文件中模式变量的ant属性值,groovy,Groovy,当我在macrodef中使用groovy任务中的ant macrodef属性时,我无法运行用于移动文件的代码 <macrodef name="dirmove"> <attribute name="todir" /> <attribute name="fromdir" /> <attribute name="includes" default="*" />
<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>
File dir1=新文件(属性“fromdir”);
File dir2=新文件(属性“todir”);
def pattern=properties.get('includes')
println模式;
dir1.eachFileMatch~/pattern/,{
f->
boolean fileMoved=f.renameTo(新文件(dir2,f.getName());
//assert f.name=='1.txt'//**因为文件对象是不可变的,所以我只是检查以前的文件名是否存在。它仍然存在。
println文件移动;
}
这段代码正确地打印pattern的值,该值来自属性值。但是eachFileMatch函数不符合规范
dir1.eachFileMatch ~/pattern/, {
应该是
dir1.eachFileMatch ~/${pattern}/, {
因为模式是一个字符串变量,所以您需要将其添加到regex模式中
以前,您只是在搜索名为模式的所有文件
@user117339好消息!:-)你能把这个答案标为A吗?