Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a macrodef from within another file

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.

like image 889
scheffield Avatar asked Feb 18 '11 16:02

scheffield


1 Answers

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.

like image 125
martin clayton Avatar answered Nov 11 '22 17:11

martin clayton