我想从另一个宏的元素内部调用一个宏。假设我有以下宏:
<macrodef name="jc">
<attribute name="name" />
<attribute name="destdir" />
<element name="fileset-list" optional="false" />
<sequential>
<jar destfile="@{destdir}${file.separator}@{name}.jar" update="false">
<fileset-list />
<manifest>
<attribute name="Manifest-Version" value="1.0" />
</manifest>
</jar>
</sequential>
</macrodef>
和另一个宏
<macrodef name="defaultfs" description="Defines the default fileset">
<attribute name="path" />
<sequential>
<fileset dir="${dir.build.classes}">
<include name="@{path}/**/*.class" />
</fileset>
<fileset dir="${src.ehs}">
<include name="@{path}/**/icons/**" />
<include name="@{path}/**/sounds/**" />
<include name="@{path}/**/*.gif" />
<include name="@{path}/**/*.png" />
<include name="@{path}/**/*.wav" />
<include name="@{path}/**/*.jpg" />
<include name="@{path}/**/*.properties" />
<include name="@{path}/**/*.xml" />
<include name="@{path}/**/jaxb.index" />
</fileset>
</sequential>
</macrodef>
我使用这些宏如下:
<jc destdir="${dir.build.jar}" name="thejar">
<fileset-list>
<defaultfs path="org/path/inner" />
</fileset-list>
</jc>
我得到的是以下错误消息:
jar doesn't support the nested "defaultfs" element.
怎么了?
您可以尝试在主宏"jc"中进行宏调用,并传递文件集路径以将其视为附加属性
<macrodef name="jc">
<attribute name="name" />
<attribute name="destdir" />
<attribute name="fileset.path.to.consider" />
<sequential>
<jar destfile="@{destdir}${file.separator}@{name}.jar" update="false">
<defaultfs path="@{fileset.path.to.consider}"/>
<manifest>
<attribute name="Manifest-Version" value="1.0" />
</manifest>
</jar>
</sequential>
</macrodef>
然后您的主宏调用可能如下所示:
<jc destdir="${dir.build.jar}" name="thejar" fileset.path.to.consider="org/path/inner"/>
(未测试,您可以尝试一下)