Wed
9
Mar '05
Ant macrodef
by Frank Spychalski filed under articles, Java, Work

If you find this useful, you might like my other articles, too.

Finally Ant 1.6. supports a macrodef target. This article provides a not so short example to see macrodefs in action.

There are three different ways to pass arguments to a macro:

  • as an attribute – this is the easiest way and the one you will most likely use the most
  • as text content – if you have to pass large amounts of text to your macro, this is probably the way to go
  • as element content – if you have to pass complex data or other ant tasks into your macro

And you can combine these ways as you can see in fancy-echo-comb.

<?xml version="1.0"?>
<project name="whatever" default="help" basedir=".">

  <!-- parameter as attribute -->
  <macrodef name="fancy-echo">
    <attribute name="text" default="default value"/>
    <sequential>
      <echo message="by attribute"/>
      <echo message="this is the simple example with parameter @{text}"/>
    </sequential>
  </macrodef>

  <!-- parameter as text content -->
  <macrodef name="fancy-echo-2">
    <text name="text"/>
    <sequential>
      <echo message="by text"/>
      <echo message="@{text}"/>
    </sequential>
  </macrodef>

  <!-- parameter as child element -->
  <macrodef name="fancy-echo-element">
    <element name="content"/>
    <sequential>
      <echo message="by element"/>
      <content/>
    </sequential>
  </macrodef>

  <!-- parameter as text content and attribute-->
  <macrodef name="fancy-echo-comb">
    <attribute name="text-attr" default="default value"/>
    <text name="text"/>
    <sequential>
      <echo message="a combination"/>
      <echo message="text content"/>
      <echo message="@{text}"/>
      <echo message="text attribute"/>
      <echo message="@{text-attr}"/>
    </sequential>
  </macrodef>

  <!-- usage examples -->
  <target name="help" description="print this help">
    <fancy-echo text="some text"/>
    <echo/>
    <fancy-echo/>
    <echo/>
    <fancy-echo-2>use this way for longer text with
with newlines etc.</fancy-echo-2>
    <echo/>
    <fancy-echo-element>
      <content>
        <echo>To pass an ant task into a macro, this is the way to go.</echo>
      </content>
    </fancy-echo-element>
    <echo/>
    <fancy-echo-comb text-attr="and an additional attribute">longer text with
with newlines
etc.</fancy-echo-comb>
  </target>
</project>

prints:

Buildfile: build.xml

help:
     [echo] by attribute
     [echo] this is the simple example with parameter some text

     [echo] by attribute
     [echo] this is the simple example with parameter default value

     [echo] by text
     [echo] longer text with
     [echo] with newlines
     [echo] etc.

     [echo] by element
     [echo] To pass an ant task into a macro, this is the way to go.

     [echo] a combination
     [echo] text content
     [echo] use this way for longer text with
     [echo] with newlines etc.
     [echo] text attribute
     [echo] and an additional attribute

BUILD SUCCESSFUL
Total time: 0 seconds

BTW, here are two books you might find useful, if you work with ant:


3 Responses to “Ant macrodef”

  1. 1

    Hi,

    I have one xml file having list of projects with its attributes. is it possible to pass these attributes to macrodef,if macrodef is defined in other xml file?

    bhavesh (February 11th, 2006 at 07:05)
  2. 2

    I’m not really sure what you want to achieve. You have 3 files: your a) build.xml, b) xml with list of projects, c) macrodef in other file. Include c) in a), parse b) and pass as content to your target. Is that what you want to do?

    Frank Spychalski (February 11th, 2006 at 10:11)
  3. 3

    Great article. Helped me a lot more than all those hello-world examples. This is more like hello-real-world ;-)
    Thanks!

    geert3 (September 4th, 2012 at 16:13)

Any comments? Or questions? Just leave a Reply: