Ant 'if' on macrodef

I would like to write a macro definition depending on what property is set in another macrodef, for example this doesn't work ... (macrodef doesn't support dependencies and if not attributes) Is there anyway to do this?

<project name="mac">

    <property name="antlr.version" value="3.2"/>

    <macrodef name="check">
        <attribute name="dest"/>
        <attribute name="name"/>
        <attribute name="version"/>
        <sequential>
            <available file="@{dest}/@{name}-@{version}.jar" property="@{name}-exists"/>
        </sequential>
    </macrodef>

    <macrodef name="pull" depends="check" unless="@{name}-exists">
        <attribute name="url"/>
        <attribute name="dest"/>
        <attribute name="name"/>
        <attribute name="version"/>

        <sequential>
            <get src="@{url}" dest="@{dest}/@{name}-@{version}" verbose="true" ignoreerrors="true" unless="@{name}-exists"/>
        </sequential>
    </macrodef>

    <target name="pullall">
           <pull url="http://repo1.maven.org/maven2/org/antlr/antlr/${antlr.version}/antlr-${antlr.version}.jar" dest="." name="antlr" version="${antlr.version}"/>
    </target>

      

+3


source to share


2 answers


It works:



    <project name="mac">

        <property name="antlr.version" value="3.2"/>
        <property name="stringtemplate.version" value="4.0.2"/>

        <target name="check">
            <available file="${dest}/${name}-${version}.jar" property="jar-exists"/>
        </target>

        <target name="_pull" depends="check" unless="jar-exists">
            <get src="${url}" dest="${dest}/${name}-${version}.jar" verbose="true" ignoreerrors="true"/>
        </target>

        <macrodef name="pull">
            <attribute name="url"/>
            <attribute name="dest"/>
            <attribute name="name"/>
            <attribute name="version"/>

            <sequential>
                <antcall target="_pull">
                    <param name="url" value="@{url}"/>
                    <param name="dest" value="@{dest}"/>
                    <param name="name" value="@{name}"/>
                    <param name="version" value="@{version}"/>
                </antcall>
            </sequential>
        </macrodef>

        <target name="pullall">
        <pull url="http://repo1.maven.org/maven2/org/antlr/antlr/${antlr.version}/antlr-${antlr.version}.jar" dest="." name="antlr" version="${antlr.version}"/>
        <pull url="http://repo1.maven.org/maven2/org/antlr/stringtemplate/${stringtemplate.version}/stringtemplate-${stringtemplate.version}.jar" dest="." name="stringtemplate" version="${stringtemplate.version}"/>
        </target>

</project>

      

+4


source


Even if this question is a bit older, it might help someone:

as of ant 1.9.1 if: set, if: set the attributes that were entered. https://ant.apache.org/manual/ifunless.html

this way you can use your macros like this (tested with ant 1.9.3)



<project name="mac" xmlns:unless="ant:unless">

    <property name="antlr.version" value="3.2"/>

    <macrodef name="check">
        <attribute name="dest"/>
        <attribute name="name"/>
        <attribute name="version"/>
        <sequential>
            <available file="@{dest}/@{name}-@{version}.jar" property="@{name}-exists" />
        </sequential>
    </macrodef>

    <macrodef name="pull">
        <attribute name="url"/>
        <attribute name="dest"/>
        <attribute name="name"/>
        <attribute name="version"/>

        <sequential>
            <check dest="@{dest}" name="@{name}" version="@{version}" />
            <get src="@{url}" dest="@{dest}/@{name}-@{version}.jar" verbose="true" ignoreerrors="true" unless:set="@{name}-exists" />
        </sequential>
    </macrodef>

    <target name="pullall">
           <pull url="http://repo1.maven.org/maven2/org/antlr/antlr/${antlr.version}/antlr-${antlr.version}.jar" dest="." name="antlr" version="${antlr.version}"/>
    </target>

</project>

      

by the way. there are other tricks with macros and if: set / except: set too see here http://www.artificialworlds.net/blog/2013/09/13/using-ifset-unlessset-etc-with-macrodefs- in-ant /

You can even simulate a conditional macro if you add an if / except to a sequential macro chunk.

+2


source







All Articles