3

我想编写一个取决于在另一个宏定义中设置的属性的宏定义,例如这个,它不起作用......(宏定义不支持依赖和除非属性)无论如何要这样做?

<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>

4

2 回答 2

4

这似乎有效:

    <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>
于 2012-04-09T21:10:09.177 回答
3

即使这个问题“有点”老了,这可能会对某人有所帮助:

在 ant 1.9.1 中引入了 if:set unless:set 属性。 https://ant.apache.org/manual/ifunless.html

这样,您可以按如下方式使用宏(使用 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>

顺便提一句。宏还有其他技巧和 if:set/unless:set ,请参见此处http://www.artificialworlds.net/blog/2013/09/13/using-ifset-unlessset-etc-with-macrodefs-in-蚂蚁/

如果将 if/unless 添加到宏的顺序主体中,您甚至可以模拟条件宏。

于 2014-07-14T18:11:44.330 回答