2013-04-07 67 views
0

這將是美妙的,如果我可以這樣做:能夠訪問螞蟻macrodef中封閉目標的「名稱」嗎?

<macrodef name="process-target"> 
    <attribute name="target" default="?enclosing.target.name?" /> 
    ... 
</macrodef> 


<target name="myTarget> 
    <process-target/> 
</target> 
+0

剛剛發現了類似的回答:http://stackoverflow.com/questions/1427745/how-can-i-get-the-value-of-the-current-target-ant – 2013-04-07 20:30:17

+0

呀看起來像你必須轉義螞蟻並使用Javascript - – peterk 2013-04-08 22:01:48

回答

2

需要使用腳本來訪問目標名稱如下:

<project name="demo" default="run1"> 

    <macrodef name="process-target"> 
     <sequential> 
      <script language="javascript"> 
      project.setProperty("enclosing.target.name", self.getOwningTarget()); 
      </script> 
      <echo message="${enclosing.target.name}"/> 
     </sequential> 
    </macrodef> 

    <target name="run1"> 
     <process-target/> 
    </target> 

    <target name="run2"> 
     <process-target/> 
    </target> 

</project> 
0

我把這個在這裏,因爲我想有core ant中的可選屬性。

<macrodef name="process-target"> 
    <attribute name="target" default="_not_set_" /> 
    <sequential> 
     <property name="_target_" value="@{target}" /> 
     <script language="javascript"> 
      if(project.getProperty("_target_") == "_not_set_") { 
       project.setProperty("_target_", self.getOwningTarget()); 
      } 
     </script> 
     <echo message="${_target_}"/> 
    </sequential> 
</macrodef>