他們可能是一對夫婦的方式來解決這個問題,但沒有一個是作爲使用螞蟻的contrib元素那麼簡單。我不確定這是否會爲您提供所需的應用程序,但您可以嘗試以下操作:
使用條件目標。如果您可以將您的macrodef替換爲要調用的目標,這可能適用於您。請注意,這將全局設置該屬性,因此它可能無法用於您的應用程序。
<target name="default">
<condition property="platformIsProd">
<equals arg1="${platform}" arg2="prod" />
</condition>
<antcall target="do-buildstamp" />
</target>
<target name="do-buildstamp" if="platformIsProd">
<echo>doing prod stuff...</echo>
</target>
處理'else'的情況。如果你需要處理的另一種情況,你需要提供幾個目標...
<target name="default">
<property name="platform" value="prod" />
<antcall target="do-buildstamp" />
</target>
<target name="do-buildstamp">
<condition property="platformIsProd">
<equals arg1="${platform}" arg2="prod" />
</condition>
<antcall target="do-buildstamp-prod" />
<antcall target="do-buildstamp-other" />
</target>
<target name="do-buildstamp-prod" if="platformIsProd">
<echo>doing internal prod stuff...</echo>
</target>
<target name="do-buildstamp-other" unless="platformIsProd">
<echo>doing internal non-prod stuff...</echo>
</target>
使用外接構建文件。如果您需要爲您的屬性創建多個具有不同值的調用,則可以在同一個項目中的另一個構建文件中將其隔離。這創造了一點性能問題,但您不需要額外的庫。
build.xml中
:
<target name="default">
<ant antfile="buildstamp.xml" target="do-buildstamp" />
<ant antfile="buildstamp.xml" target="do-buildstamp">
<property name="platform" value="prod" />
</ant>
<ant antfile="buildstamp.xml" target="do-buildstamp">
<property name="platform" value="nonprod" />
</ant>
</target>
在buildstamp.xml:
<condition property="platformIsProd">
<equals arg1="${platform}" arg2="prod" />
</condition>
<target name="do-buildstamp">
<antcall target="do-buildstamp-prod" />
<antcall target="do-buildstamp-other" />
</target>
<target name="do-buildstamp-prod" if="platformIsProd">
<echo>doing external prod stuff...</echo>
</target>
<target name="do-buildstamp-other" unless="platformIsProd">
<echo>doing external non-prod stuff...</echo>
</target>
添加螞蟻了contrib到您的項目。當然,如果您可以將文件添加到項目中,最簡單的方法就是添加ant-contrib.jar文件。你可以把它放在一個「工具」文件夾下,並把它在使用的taskdef:
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${basedir}/tools/ant-contrib.jar" />
我認爲創建一個單獨的宏來處理這是不可接受的? – jheddings 2009-10-29 01:25:51