我很难让以下Apache Ant脚本工作。我们的想法是将属性值从具有特定前缀的属性复制到没有前缀的属性。
<project name="scp.result.files">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<property name="prop.a.sub1" value="a" />
<property name="prop.b.extra2" value="b" />
<property name="prop.c.foo" value="c" />
<property name="prop.d.foo" value="not selected" />
<target name="prep-props">
<!-- Select all interesting properties. -->
<propertyselector property="prop.list"
delimiter=","
match="prop\.(a|b|c)\..+"
casesensitive="false"
override="true" />
<echo message="matched properties: ${prop.list}" />
<!-- For each selected property, set a property without the "prop." prefix. -->
<for param="prop" list="${prop.list}" delimiter="," trim="true">
<propertyregex property="dest.prop"
input="@{prop}"
regex="prop\.((a|b|c)\..+)"
select="\1"
overrride="true" />
<propertycopy name="${dest.prop}"
from="prop.${dest.prop}"
override="true" />
<propertycopy name="_pval" from="${dest.prop}" override="true" />
<echo message="${dest.prop} is ${_pval}" />
</for>
</target>
</project>
不幸的是,上面的脚本对我产生了以下错误。
BUILD FAILED
C:\test.xml:19: Invalid type class net.sf.antcontrib.property.RegexTask used in For task, it does not have a public iterator method
at net.sf.antcontrib.logic.ForTask$ReflectIterator.<init>(ForTask.java:450)
at net.sf.antcontrib.logic.ForTask.add(ForTask.java:411)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.IntrospectionHelper$13.create(IntrospectionHelper.java:1552)
at org.apache.tools.ant.IntrospectionHelper$Creator.create(IntrospectionHelper.java:1329)
at org.apache.tools.ant.UnknownElement.handleChild(UnknownElement.java:574)
at org.apache.tools.ant.UnknownElement.handleChildren(UnknownElement.java:358)
at org.apache.tools.ant.UnknownElement.configure(UnknownElement.java:204)
at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
at org.apache.tools.ant.Task.perform(Task.java:347)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
有可能解决这个问题吗?是否有其他方法可以迭代选定的属性列表并设置未加前缀的属性?
答案 0 :(得分:1)
for
每次迭代要执行的任务列表需要放在sequential
元素内,否则propertyregex
将被视为&#34;列表&#34 ; for
任务将迭代。此外,propertyregex
使用regexp
代替regex
(查看其文档)。
<for param="prop" list="${prop.list}" delimiter="," trim="true">
<sequential>
<propertyregex property="dest.prop"
input="@{prop}"
regexp="prop\.((a|b|c)\..+)"
select="\1"
overrride="true" />
<propertycopy name="${dest.prop}"
from="prop.${dest.prop}"
override="true" />
<propertycopy name="_pval" from="${dest.prop}" override="true" />
<echo message="${dest.prop} is ${_pval}" />
</sequential>
</for>