Function 使用ant macrodef功能化几行代码
我试图使用Function 使用ant macrodef功能化几行代码,function,ant,macrodef,Function,Ant,Macrodef,我试图使用macrodef将几行ant代码“函数化”。但它会导致如下错误: copy doesn't support the nested "my-macro" element. 如果我将在复制任务中添加filterchian的宏的定义“内联”起来,它就会工作 我的测试目标是这样的- <target name="copy-files"> <sequential> <copy todir="abc" >
macrodef
将几行ant代码“函数化”。但它会导致如下错误:
copy doesn't support the nested "my-macro" element.
如果我将在复制任务中添加filterchian的宏的定义“内联”起来,它就会工作
我的测试目标是这样的-
<target name="copy-files">
<sequential>
<copy todir="abc" >
<fileset dir="xyz">
<!--needy includes/excludes -->
</fileset>
<my-macro/>
</copy>
</sequential>
</target>
我的宏如下所示:
<macrodef name="my-macro">
<sequential>
<filterchain>
<fixcrlf includes="**" eol="lf"/>
</filterchain>
</sequential>
</macrodef>
起作用的代码(内联代码)如下所示:
<target name="copy-files">
<sequential>
<copy todir="abc" >
<fileset dir="xyz">
<!--needy includes/excludes -->
</fileset>
<filterchain>
<fixcrlf includes="**" eol="lf"/>
</filterchain>
</copy>
</sequential></target>
复制任务不接受嵌套的宏元素,这就是错误消息所说的。
将所有复制内容放入macrodef,f.e.:
<macrodef name="my-macro">
<attribute name="dest"/>
<attribute name="fsdir"/>
<attribute name="fsincludes"/>
<attribute name="fsexcludes"/>
<attribute name="fixincl"/>
<sequential>
<copy todir="@dest}">
<fileset dir="@{fsdir}">
<include name="@{fsincludes}"/>
<exclude name="@{fsexcludes}"/>
</fileset>
<filterchain>
<fixcrlf includes="@{fixincl}" eol="lf"/>
</filterchain>
</copy>
</sequential>
</macrodef>
已使用。
毕竟,如果它变得更复杂,你就应该考虑用./P>> P > ReSeSE的答案已经显示了你应该使用的代码< > /Cord>,这里有一些解释。
复制任务
,
,
都是Ant任务。每个Ant任务都有Java代码支持。大多数内置Ant任务的Java类都在org.apache.tools.Ant.taskdefs
下,例如是
任务的后端
任务的嵌套元素由任务的Java代码处理。对于
任务,它是Copy.java(或它所依赖的其他类)中处理嵌套元素的代码
您将在Copy.java中看到使用createFilterChain
、addFileset
、createMapper
等命名的方法。这就是为什么
支持文件集、过滤器链、映射器和中所述的其他嵌套元素的原因
Macrodef
这将使用嵌套任务作为模板定义新任务
Macrodef是一种定义Ant任务的方法,无需在Java中编码。它将其嵌套的Ant xml行转换为一个新任务,其工作方式与其他Ant任务相同
显然,您不应该只在
中添加
,因为
不是一项Ant任务,而是一项任务。您还可以显示有效的代码吗@coolcfan在我的问题-显示工作内联代码中进行了编辑我确实考虑过这个设计,但不幸的是它不适合当前的问题:因为ant任务与不同类型的选项一起使用,如flatte
属性设置为true/false等。。而且#of文件集
的发生率也随着#of include/exclude模式的变化而变化。因此,我之所以只尝试将公共行放在宏中&在复制任务中重用在macrodef
中使用element
的选项对于寻址不同的文件集非常方便。感谢分享。但我仍然没有看到一种简单的方法来处理复制任务中不同的属性集支持,以使其完全工作,例如:我不能使用同一个宏,当它仅使用文件和文件属性进行文件复制时,因为它将被设计用于目录。其他公共属性仍然可以参数化,并通过设置为适当的值来传递。有没有其他方法可以通过编写一些自定义类来扩展/模拟“函数”的行为?哇,你最近的编辑简化到了一个新的级别。由于我当前的项目在具有不同的复制样式/策略方面比较复杂:尽管上述选项解决了使用文件集复制单个文件的问题,但在项目中有意地使用属性文件,就像复制到目标一样,我们使用不同的文件名进行复制。f、 e:x/y/some.abc.profile
至x/abc/y/some.profile
关于项目部署路径和上述可变值在一个循环中根据使用属性选择器进行的一些计算动态生成框架…感谢@coolcfan分享macrodef
工作/支持的实施细节。正如我在下面的评论中对@Rebse的回答所说,我只是想实现“功能化”部分。在ant中是否有与macrodef等效的类型来实现这一点?
旨在使用户能够在构建文件中声明新任务——类似的任务:
和
。您希望从“复制”用法中获得一些共同点,但是使用
调用
和仅使用file
参数调用
在Ant中只是两种不同的逻辑。如果您想在许多
实例中共享
或
,可以使用refid
。
<macrodef name="my-macro">
<attribute name="dest"/>
<element name="fs" description="nested filesets"/>
<attribute name="fixincl"/>
<sequential>
<copy todir="@dest}">
<!-- 1-n nested filesets) -->
<fs/>
<filterchain>
<fixcrlf includes="@{fixincl}" eol="lf"/>
</filterchain>
</copy>
</sequential>
</macrodef>
<my-macro dest="C:/whatever" fixincl="**">
<fs>
<fileset dir="." includes="**/*.foo"/>
<fileset dir="../foo" includes="**/*.xml"/>
<!-- ... -->
</fs>
</my-macro>
<fileset file="C:/somepath/some.file"/>
<macrodef name="my-macro">
<attribute name="dest"/>
<element name="copyfiles" description="nested copy"/>
<element name="fs" description="nested filesets"/>
<attribute name="fixincl"/>
<sequential>
<copy todir="@dest}">
<!-- 1-n nested filesets) -->
<fs/>
<filterchain>
<fixcrlf includes="@{fixincl}" eol="lf"/>
</filterchain>
</copy>
<copyfiles/>
</sequential>
</macrodef>
<my-macro dest="C:/whatever" fixincl="**">
<fs>
<fileset dir="." includes="**/*.foo"/>
<fileset dir="../foo" includes="**/*.xml"/>
<!-- ... -->
</fs>
<copyfiles>
<copy file="..." tofile="..."/>
<!-- ... -->
</copyfiles>
</my-macro>