2011-03-11 21 views
18

在Ant中,我想定義一個定義屬性的目標(稱爲A),並從另一個目標(稱爲B)定義一個目標爲antcall。我希望在抵制目標A之後,目標B可以訪問目標A中定義的屬性。Ant antcall一個定義屬性的目標

例如:

<target name="B"> 
    <antcall target="A" inheritAll="true" inheritRefs="true" /> 
    <echo>${myprop}</echo> 
</target> 
<target name="A"> 
    <property name="myprop" value="myvalue" /> 
</target> 

但是它不工作,<echo>${myprop}</echo>不打印myvalue(我想是因爲物業mypropB沒有定義)。

有沒有辦法做到這一點?

+0

你介意檢查你的問題嗎?你可能不打算讓B打電話B. – rajah9 2011-03-11 17:14:12

+0

@ rajah9:我想在目標A中定義屬性,並在目標B中回顯它。從目標B調用目標A.主要問題是我想要創建一個目標並將其分爲子目標。 – alem0lars 2011-03-11 17:21:29

回答

11

按照Apache Ant FAQ

<target name="cond" depends="cond-if"/> 

    <target name="cond-if" if="prop1"> 
     <antcall target="cond-if-2"/> 
    </target> 

    <target name="cond-if-2" if="prop2"> 
     <antcall target="cond-if-3"/> 
    </target> 

    <target name="cond-if-3" unless="prop3"> 
     <echo message="yes"/> 
    </target> 

Note: <antcall> tasks do not pass property changes back up to the environment they were called from, so you wouldn't be able to, for example, set a result property in the cond-if-3 target, then do <echo message="result is ${result}"/> in the cond target.

在這方面,它是不可能做你想要使用antcall什麼。

========== 編輯 ===========

嘗試antcallback:AntCallBack是相同的標準 'antcall' 任務,只是它允許在調用目標中設置的屬性在調用目標中可用。從上面的頁面粘貼
http://antelope.tigris.org/nonav/docs/manual/bk03ch20.html

示例代碼:

<target name="testCallback" description="Test CallBack"> 
     <taskdef name="antcallback" classname="ise.antelope.tasks.AntCallBack" classpath="${antelope.home}/build" /> 
     <antcallback target="-testcb" return="a, b"/> 
     <echo>a = ${a}</echo> 
     <echo>b = ${b}</echo> 
    </target> 

    <target name="-testcb"> 
     <property name="a" value="A"/> 
     <property name="b" value="B"/> 
    </target> 
+0

謝謝。這是問題。你如何以不同的方式實現這一行爲?我的意思是我想將目標的邏輯劃分爲子目標 – alem0lars 2011-03-11 17:17:58

+0

另外,您可以從Ant-Contrib包中嘗試任務'AntCallBack':http://ant-contrib.sourceforge.net/tasks/tasks/index.html – kevinarpe 2011-10-19 08:11:00

0

我想你想使用一個參數。

<project default="B"> 
<target name="B"> 
    <antcall target="A"> 
     <param name="myprop" value="myvalue"/> 
    </antcall> 

</target> 
<target name="A"> 
    <echo>${myprop}</echo> 
</target> 
</project> 

我用項目標籤包圍了這一點,並將echo語句移入「A」。我說輸出

B: 
A: 
    [echo] myvalue 
BUILD SUCCESSFUL 
+2

這不是OP想要的。他想在目標A中定義屬性,並在目標B中回顯該屬性。在此處,將其定義在A中並在A中回顯。 – SOUser 2011-03-11 17:13:55

+0

這不能解決問題 – alem0lars 2011-03-11 17:19:32

+0

感謝您澄清問題。 – rajah9 2011-03-11 17:49:18

0

@ alem0lars,因爲你說你想細分的目標,讓我提供了不同的解決方案(即遺憾的是不回答你原始問題)。

​​

這將組件細分爲aTgt和bTgt。

輸出將被

aTgt: 
    [echo] "In aTgt doing a build" 
bTgt: 
    [echo] "In bTgt doing a build" 
deps: 
BUILD SUCCESSFUL 
4

而不是使用<antcall>爲什麼不只是有靶B是否取決於目標A

<target name="B" depends="A"> 
    <echo>${myprop}</echo> 
</target> 
<target name="A"> 
    <property name="myprop" value="myvalue" /> 
</target> 
9

另一種方法是將目標重構爲宏。你正在嘗試使用像函數這樣的目標,它們只是不打算這樣使用。我通常將大部分邏輯作爲宏寫下來,這樣我就可以將它更容易地編寫成更復雜的宏。然後我編寫我需要的命令行入口點的簡單包裝目標。

+2

這是最好的方法。螞蟻屬性不是變量,它們是不可變的。 antcall放入同一個build.xml中_always_聞起來很糟糕。 – thekbb 2013-01-25 21:33:33

相關問題