Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant conditional if within a macrodef

Tags:

ant

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?

like image 586
bharal Avatar asked Aug 13 '13 10:08

bharal


2 Answers

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>
like image 179
David W. Avatar answered Oct 26 '22 21:10

David W.


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"/>
like image 7
Mark O'Connor Avatar answered Oct 26 '22 22:10

Mark O'Connor