2009-10-28 87 views
1

如果macrodef屬性設置爲prod(下面的示例),我試圖刪除所有以爲開頭的行日誌。我計劃使用replaceregexp刪除所有以log開頭的行。但是,我不知道如何測試某個屬性是否設置爲特定值,除了使用如果任務。我不想介紹任何非核心Ant任務來執行此任務,但我無法提出任何其他解決方案。除了使用if-task之外,我還有其他選擇嗎?測試沒有IF的Macrodef屬性

感謝

<macrodef name="setBuildstamp"> 
    <attribute name="platform" /> 
    <sequential> 
     <if> 
      <equals arg1="platform" arg2="prod" /> 
      <then> 
       <replaceregexp match="^log\(.*" value="" /> 
      </then> 
     </if> 
    </sequential> 
</macrodef> 
+0

我認爲創建一個單獨的宏來處理這是不可接受的? – jheddings 2009-10-29 01:25:51

回答

3

他們可能是一對夫婦的方式來解決這個問題,但沒有一個是作爲使用螞蟻的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" /> 
+0

我決定接受你的建議並添加了ant-contrib庫。 – Steve 2009-10-29 13:58:00

5

您應該使用一個參考的參數,這樣@{platform}

此外,您的replaceregexp任務缺少一些參數。

我認爲在您的特殊情況下最好使用linecontainsregexp濾光片閱讀器。這裏是修改後的代碼(注意否定參數linecontainsregexp)。

<macrodef name="setBuildstamp"> 
    <attribute name="platform" /> 
    <sequential> 
    <if> 
     <equals arg1="@{platform}" arg2="prod" /> 
     <then> 
     <copy todir="dest-dir"> 
      <fileset dir="src-dir"/> 
      <filterchain> 
      <linecontainsregexp 
       regexp="^log\(.*" 
       negate="true" 
      /> 
      </filterchain> 
     </copy> 
     </then> 
    </if> 
    </sequential> 
</macrodef> 
+0

過濾的好輸入...我試圖專注於問題的「使用核心任務」部分。 – jheddings 2009-10-29 02:53:31

0

看來,當你明確構建項目的生產環境一樣 - 你是剝離出來的代碼你不想在生產環境中運行。因此,您正在創建與開發或測試環境中運行的不同的二進制文件。

如何在運行時使用環境變量或屬性文件而不是構建時間來確定是否記錄日誌?這樣,當您在生產中遇到問題並且想要使用相同的確切二進制文件(而不是確定版本,檢出代碼,使用不同的環境標誌重建)時,只需將其重新部署到開發或測試環境並打開屬性文件或環境變量中的調試?

+0

我們實際上正在開發一個Javascript TV Widget應用程序,它有一些嚴重的性能限制,所以我們試圖刪除任何額外的東西,我們可以。 – Steve 2009-10-29 11:21:34

+0

啊,對於JavaScript我可以看到希望保持你的功能小而精。 – 2009-10-29 12:31:39