13

Within ant, I have a macrodef.

Assuming I have to use this macrodef, and there is a item inside said macrodef that I want to run if the property special.property exists and is true, what do I do?

I currently have

<macrodef name="someName">
    <sequential>
        <someMacroDefThatSetsTheProerty  />
        <some:thingHereThatDependsOn if="special.property" />
    <sequential>
</macrodef>

Which doesn't work - the some:thingHereThatDependsOn doesnt have an "if" attribute, and I cannot add one to it.

antcontrib is not available.

With a target I can give the target an "if", what can I do with a macrodef?

Feedforward
  • 4,521
  • 4
  • 22
  • 34
bharal
  • 15,461
  • 36
  • 117
  • 195
  • 1
    Conditional execution in ANT does not work at task level, it applies to targets. Perhaps supply some more detail of what you're trying to do. – Mark O'Connor Aug 13 '13 at 22:12
  • Why can't you use ant contrib? (Or a scripting language within Ant like BeanShell.) Ant isn't meant to be used for conditional logic in targets; only for the targets themselves. Ant contrib was introduced as a way to get around this. Ant contrib is an open source jar. Why can't you make it available? Saying you can't use ant contrib feels a bit like saying "I need to solve this problem, but I'm not allowed to use any spaces in my code" – Jeanne Boyarsky Aug 14 '13 at 02:50
  • @MarkO'Connor I was just wondering if it was possible, to be honest. – bharal Aug 14 '13 at 16:36
  • @JeanneBoyarsky just because something is "open source" doesn't mean "it can be used". In my *particular* situation, yes, I have access to ant contrib. This was a question more on "is it possible", which i imagine means "it is not". – bharal Aug 14 '13 at 16:38
  • @bharal, how did you get it to work? I am running into a similar issue. I have a macrodef sequential, and I have two macro references inside of the sequential, one of which I need to trigger only if an exported variable is set to a specific value. – Floam Jan 16 '23 at 01:30

2 Answers2

18

In Ant 1.9.1 and higher, there is now a new implementation of if and unless attributes. This might be what you're thinking of.

First, you need to put them into your namespace. Add them to your <project> header:

<project name="myproject" basedir="." default="package"
    xmlns:if="ant:if"
    xmlns:unless="ant:unless">

Now, you can add them to almost any Ant task or sub entity:

<!-- Copy over files from special directory, but only if it exists -->
<available property="special.dir.available"
    file="${special.dir} type="dir"/>

<copy todir="${target.dir}>
    <fileset dir="${special.dir}" if:true="special.dir.available"/>
    <fileset dir="${other.dir}"/>
</copy>

<!-- FTP files over to host, but only if it's on line-->
<condition property="ftp.available">
    <isreachable host="${ftp.host}"/>
</condition>

<ftp server="${ftp.host}" 
    userid="${userid}"
    passowrd="${password}"
    if:true="ftp.available">
    <fileset dir=".../>
</ftp>
thekbb
  • 7,668
  • 1
  • 36
  • 61
David W.
  • 105,218
  • 39
  • 216
  • 337
7

This is only possible if the ANT "thingHereThatDependsOn" task supports an "if" attribute.

As stated above, conditional execution in ANT, normally, only applies to targets.

<target name="doSomething" if="allowed.to.do.something">
   ..
   ..
</target>

<target name="doSomethingElse" unless="allowed.to.do.something">
   ..
   ..
</target>

<target name="go" depends="doSomething,doSomethingElse"/>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185