我有一个执行过滤器副本的ant任务,我多次调用它只更改参数。这是为各种环境创建属性文件。
我想简化调用目标,这样我就可以减少复制和粘贴。
这是调用目标
<target name="create_local_property_files" depends="clean,prepare">
<!-- create first machine property files -->
<antcall target="property.filter.copy" inheritAll="false">
<param name="tmp.dom" value="machine1" />
</antcall>
<!-- create second machine property files -->
<antcall target="property.filter.copy" inheritAll="false">
<param name="tmp.dom" value="machine2" />
</antcall>
[...] <!-- to the n'th machine property file -->
</target>
我想拨打一个电话并传入一系列机器。有什么建议吗?
以下是完整性的过滤器复制目标
<target name="property.filter.copy">
<copy todir="${local.property.file.dir}" failonerror="true" verbose="true" overwrite="true">
<filterset>
<!-- Uses the same filters files as scripts -->
<filtersfile file="${property.variables.dir}/${tmp.dom}.properties" />
</filterset>
<fileset dir="${property.file.dir}">
<include name="cnmp.properties" />
<include name="cnmp.jdo.properties" />
</fileset>
<!-- Deployment script looks for hostname.rest_of_filename-->
<globmapper from="*" to="${tmp.dom}.*" />
</copy>
</target>
答案 0 :(得分:0)
在ant-contrib中实现了一些可以很好地完成工作的循环。
查看foreach
任务的实例。
答案 1 :(得分:0)
我建议将macrodefs用于重复的代码段
<project name="demo" default="copy">
<macrodef name="propertyFilterCopy">
<attribute name="tmp.dom"/>
<attribute name="local.property.file.dir" default="build/properties"/>
<attribute name="property.variables.dir" default="build/variables"/>
<attribute name="property.file.dir" default="build/files"/>
<sequential>
<copy todir="@{local.property.file.dir}" failonerror="true" verbose="true" overwrite="true">
<filterset>
<filtersfile file="@{property.variables.dir}/@{tmp.dom}.properties" />
</filterset>
<fileset dir="@{property.file.dir}">
<include name="cnmp.properties" />
<include name="cnmp.jdo.properties" />
</fileset>
<globmapper from="*" to="@{tmp.dom}.*" />
</copy>
</sequential>
</macrodef>
<target name="copy" depends="gen-data">
<propertyFilterCopy tmp.dom="machine1"/>
<propertyFilterCopy tmp.dom="machine2"/>
</target>
<target name="gen-data">
<mkdir dir="build/properties"/>
<mkdir dir="build/variables"/>
<mkdir dir="build/files"/>
<echo file="build/files/cnmp.properties">x=1</echo>
<echo file="build/files/cnmp.jdo.properties">x=1</echo>
<echo file="build/variables/machine1.properties">x=1</echo>
<echo file="build/variables/machine2.properties">x=1</echo>
</target>
<target name="clean">
<delete dir="build"/>
</target>
</project>
macrodef的第二个优点是它们可以打包为antlibs,这进一步提高了可重用构建逻辑的可移植性。