2012-04-09 52 views
3

我想編寫一個macrodef,它取決於在另一個macrodef中設置的屬性,例如this,這不起作用...(macrodef不支持depends,除非屬性)無論如何要做到這一點?ant'unless'on macrodef

<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}[email protected]{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}[email protected]{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

這似乎工作:

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

關於macrodef的好處在於它可以在外部範圍內使用(在目標之前),因此此解決方案不考慮這一點。 – 2013-02-18 15:50:52

2

即使這個問題是一個 「位」 前輩,這可能幫助別人:

螞蟻1.9.1的,如果:設置除非:設置屬性引入。 https://ant.apache.org/manual/ifunless.html

這種方式,您可以使用您的宏如下(螞蟻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}[email protected]{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}[email protected]{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> 

BTW。還有其他的技巧與宏,如果:設置/除非:設置太,看到這裏http://www.artificialworlds.net/blog/2013/09/13/using-ifset-unlessset-etc-with-macrodefs-in-ant/

如果你添加if /除非宏的順序體,你甚至可以模擬一個條件宏。