You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Keith Hatton <kh...@axiomsystems.com> on 2007/07/30 11:55:33 UTC

How to achieve "if" and "unless"-style behaviour with macrodefs?

Hi,
 
I have cases where it would be useful to call a macrodef if a property
was set, but to skip it otherwise. If these were targets I could use the
standard Ant if/unless attributes to achieve this behaviour, but
macrodefs are called explicitly at certain points in the build file,
rather than using the Ant dependency model for target execution. I don't
see any corresponding options for a macrodef or (equivalently in my
case) for the sequential task. What's the best way to achieve this?
 
Thanks,
Keith
 

Axiom Systems Limited is a limited company registered in England and
Wales. Registered number: 2358771. Registered office: One Forbury
Square, The Forbury, Reading, Berks RG1 3EB

 

Re: How to achieve "if" and "unless"-style behaviour with macrodefs?

Posted by Andrew Goktepe <an...@gmail.com>.
You could probably define "if" and "unless" custom attributes in your macro
and then use <isset> with ant-contrib's <if> to check them inside of your
macro.

Something like this (untested!):

<macrodef name="my-macro">
    <attribute name="if" default="basedir" />
    <attribute name="unless" default="unset.property"/>
    ... other attributes...
    <sequential>
        <if> <and><isset property="@{if}" /><not><isset
property="@{unless}"/></not></and>
            <then>
               ... do stuff
            </then>
            <else />
        </if>
    </sequential>
</macrodef>

<my-macro if="my.if.property" />

I purposely gave the if and unless attributes default values that cause the
macro to proceed when neither are set in the call. basedir should always be
set and unset.property will hopefully never be set.

-Andrew

On 7/30/07, Keith Hatton <kh...@axiomsystems.com> wrote:
>
> Hi,
>
> I have cases where it would be useful to call a macrodef if a property
> was set, but to skip it otherwise. If these were targets I could use the
> standard Ant if/unless attributes to achieve this behaviour, but
> macrodefs are called explicitly at certain points in the build file,
> rather than using the Ant dependency model for target execution. I don't
> see any corresponding options for a macrodef or (equivalently in my
> case) for the sequential task. What's the best way to achieve this?
>
> Thanks,
> Keith
>
>
> Axiom Systems Limited is a limited company registered in England and
> Wales. Registered number: 2358771. Registered office: One Forbury
> Square, The Forbury, Reading, Berks RG1 3EB
>
>
>