如何从另一个文件中调用宏定义
我在单独的文件中编写了一个小宏定义:
macrodefs.xml
<macrodef name="do-cool-stuff">
<attribute name="message"/>
<sequential>
<echo message="@{message}" />
</sequential>
</macrodef>
我得到了第二个文件,我的主构建文件:
build.xml
<target name="build">
<!-- do this and that -->
<!-- cheking out macrodefs.xml via CVS -->
<ant antfile="macrodefs.xml" target="do-cool-stuff" >
<property name="message" value="Hello, World!" />
</ant>
</target>
正如您可能猜到的那样,这不起作用。错误消息类似于:
Target 'do-cool-stuff' does not exist in this project.
我发现的唯一可能的解决方案是在 Macrodefs.xml 中提供额外的目标来转发 ant 调用。
是否有可能从另一个文件中调用宏定义?
提前致谢。
I wrote a small macrodef in separate file:
macrodefs.xml
<macrodef name="do-cool-stuff">
<attribute name="message"/>
<sequential>
<echo message="@{message}" />
</sequential>
</macrodef>
I got a second file, my main build file:
build.xml
<target name="build">
<!-- do this and that -->
<!-- cheking out macrodefs.xml via CVS -->
<ant antfile="macrodefs.xml" target="do-cool-stuff" >
<property name="message" value="Hello, World!" />
</ant>
</target>
As you might guess this dosen't work. The error message is something like:
Target 'do-cool-stuff' does not exist in this project.
The only possible solution I found is to provide a extra target in the macrodefs.xml to forward the ant calls.
Is there a possibility to invoke the macrodef from within another file?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以
导入
文件并使用宏,例如this:请注意,在宏定义中,引用宏属性时应使用
@{curlybrackets}
:Ant 的末尾有一些示例
macrodef
任务 文档。更多
Ant 不能很好地支持您尝试做的事情。
ant
和antcall
任务不允许“被调用者”直接影响调用者。您可以在被调用的任务中写入文件,然后在调用者中加载这些文件。但正如您所观察到的,预处理任务import
和include
无法从目标内部调用。 ant/antcall 任务只允许您在辅助构建中运行目标,而不是宏。一种解决方法(这可能与您提到的方法类似,但允许您将所有实际工作放在顶级构建中)是拥有一个包含 Macrodefs.xml 顶级导入的内部构建文件。
像下面这样的东西。 Macrodefs.xml 文件与以前一样。 (但请注意,导入的文件 - 包括宏定义 - 需要是完整的 Ant 项目文件,因此它们必须包含项目元素。)
build.xml:
inner-build.xml:
实际上您将执行
内部构建文件可能会由主构建即时生成 - 比如说,如果您想导入多个宏定义文件 - 但这可能变得太笨重了。
You can
import
the file and use the macro like this:Note that in the macro definition you should use
@{curlybrackets}
when referencing macro attributes:There are some examples at the end of the Ant
macrodef
task docs.More
What you're trying to do isn't well supported by Ant. The
ant
andantcall
tasks don't allow the 'callee' to affect the caller directly. You can write files in the called task, then load those in the caller. But as you have observed, the pre-process tasksimport
andinclude
cannot be called from within a target. The ant/antcall tasks only allow you to run targets in subsidiary builds, not macros.One workaround method (this might be similar to the one you mention, but allows you to put all the real work in the top-level build) would be to have an inner buildfile that includes the top-level import of the macrodefs.xml.
Something like the following. The macrodefs.xml file is as before. (But note that the imported files - including the macro definitions - need to be complete Ant project files, so they must include a project element.)
build.xml:
inner-build.xml:
Effectively you would be doing
The inner buildfile could potentially be generated on-the-fly by the main build - say if you wanted to import multiple macro definition files - but that's getting perhaps too unwieldy.