如何从另一个文件中调用宏定义

发布于 2024-10-18 02:39:34 字数 840 浏览 8 评论 0原文

我在单独的文件中编写了一个小宏定义:

ma​​crodefs.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦初启 2024-10-25 02:39:34

您可以导入文件并使用宏,例如this:

<import file="macrodefs.xml" />
<do-cool-stuff message="Hello, World!" />

请注意,在宏定义中,引用宏属性时应使用 @{curlybrackets}

<sequential>
    <echo message="@{message}" />
</sequential>

Ant 的末尾有一些示例 macrodef 任务 文档。

更多

Ant 不能很好地支持您尝试做的事情。 antantcall 任务不允许“被调用者”直接影响调用者。您可以在被调用的任务中写入文件,然后在调用者中加载这些文件。但正如您所观察到的,预处理任务 importinclude 无法从目标内部调用。 ant/antcall 任务只允许您在辅助构建中运行目标,而不是宏。

一种解决方法(这可能与您提到的方法类似,但允许您将所有实际工作放在顶级构建中)是拥有一个包含 Macrodefs.xml 顶级导入的内部构建文件。

像下面这样的东西。 Macrodefs.xml 文件与以前一样。 (但请注意,导入的文件 - 包括宏定义 - 需要是完整的 Ant 项目文件,因此它们必须包含项目元素。)

build.xml:

<target name="build">
    <!-- cvs actions -->
    <ant antfile="inner-build.xml" target="target-runner">
        <property name="target" value="top-target" />
    </ant>
</target>

<!-- this target will fail unless invoked from the inner build -->
<target name="top-target">
    <do-cool-stuff message="Hello, World!" />
</target>

inner-build.xml:

<project>
    <import file="macrodefs.xml" />

    <target name="target-runner">
        <ant antfile="build.xml" target="${target}" />
    </target>
</project>

实际上您将执行

build.xml --> inner-build.xml --> build.xml (again)
(cvs)         (import macros)     (use macros)

内部构建文件可能会由主构建即时生成 - 比如说,如果您想导入多个宏定义文件 - 但这可能变得太笨重了。

You can import the file and use the macro like this:

<import file="macrodefs.xml" />
<do-cool-stuff message="Hello, World!" />

Note that in the macro definition you should use @{curlybrackets} when referencing macro attributes:

<sequential>
    <echo message="@{message}" />
</sequential>

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 and antcall 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 tasks import and include 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:

<target name="build">
    <!-- cvs actions -->
    <ant antfile="inner-build.xml" target="target-runner">
        <property name="target" value="top-target" />
    </ant>
</target>

<!-- this target will fail unless invoked from the inner build -->
<target name="top-target">
    <do-cool-stuff message="Hello, World!" />
</target>

inner-build.xml:

<project>
    <import file="macrodefs.xml" />

    <target name="target-runner">
        <ant antfile="build.xml" target="${target}" />
    </target>
</project>

Effectively you would be doing

build.xml --> inner-build.xml --> build.xml (again)
(cvs)         (import macros)     (use macros)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文