javaantmacrodef

Get value held in Ant element


Let's say I have a macrodef like so

<macrodef name="Test">
    <element name="someName"/>
    <sequential>
        <java classname="path_to_Test">
            <classpath> blah </classpath>
            <arg value="someText and {valueOfsomeName}"/>
        </java>
    </sequential>
</macrodef>

Then further down I have a target which uses this macrodef, like

<target name="testMacrodef">
    <Test>
        <someName value="someValue"/>
    </Test>
</target>

My question is, how do I get {valueOfsomeName} to be exactly the string someValue? I can't seem to get it to work at all.

Also, just for a bit of context, I can't just use an attribute tag instead of an element tag, as I need it to be an optional argument.

I've Googled for ages trying to find a solution for this; maybe there isn't one. But as far as I can see no one has asked this, and it doesn't seem to be covered well in the Ant documentation either, so if it's not possible is there a way of having an optional attribute?

EDIT: spelling


Solution

  • Use a simple echo if element has only text content, f.e. :

    <macrodef name="Test">
     <element name="someName" optional="yes"/>
     <attribute name="foo" default="bar"/>
      <sequential>
       <echo>
        <someName/>
       </echo>
       <echo> @@{foo} => @{foo}</echo>
      </sequential>
    </macrodef>
    
    <Test>
     <someName>blablabla..</someName>
    </Test>
    

    output :

    [echo]     
    [echo]    blablabla..
    [echo]  @{foo} => bar
    

    otherwise for nested xml content use echoxml, f.e. :

    <macrodef name="Test">
     <element name="someName" optional="yes"/>
     <attribute name="foo" default="bar"/>
      <sequential>
       <echoxml>
        <someName/>
       </echoxml>
       <echo> @@{foo} => @{foo}</echo>
      </sequential>
    </macrodef>
    
    <Test>
     <someName>
      <echo>blablabla..</echo>
     </someName>
    </Test>
    

    output :

    <?xml version="1.0" encoding="UTF-8"?>
    <echo>blablabla..</echo>
    [echo]  @{foo} => bar
    

    If you need the element values for further processing use echo / echoxml to file and loadfile afterwards.
    --EDIT after comment --
    Echo to file :

    <macrodef name="Test">
     <element name="someName" optional="yes"/>
     <attribute name="foo" default="bar"/>
      <sequential>
       <echo file="somefile.txt">
        <someName/>
       </echo>
       <echo> @@{foo} => @{foo}</echo>
      </sequential>
    

    or

    <macrodef name="Test">
     <element name="someName" optional="yes"/>
     <attribute name="foo" default="bar"/>
      <sequential>
       <echoxml file="somefile.txt">
        <someName/>
       </echoxml>
       <echo> @@{foo} => @{foo}</echo>
      </sequential>
    

    and use loadfile afterwards => creates property (= string) with content.
    When using echoxml strip of the xml header with filterchain :

    <loadfile srcfile=" ... " property="whatever">
     <filterchain>
      <headfilter lines="10" skip="1"/>
     </filterchain>
    </loadfile>
    

    Adapt the value of headfilter lines attribute to your needs.
    Property whatever will have the value :

    <echo>blablabla..</echo>