2011-06-08 135 views
9

我想使用ANT比較兩個文件(比如file1.txt,file2.txt)的內容。ant:如何比較兩個文件的內容

如果文件內容相同,那麼它應該設置一些'屬性'爲true,如果內容不相同,那麼它應該將'屬性'設置爲false。

任何人都可以建議我任何可以做到這一點的ANT任務。

在此先感謝。

回答

10

您可以使用類似:

<condition property="property" value="true"> 
    <filesmatch file1="file1" 
       file2="file2"/> 
</condition> 

這將設置只有當文件是相同的財產。 然後,您可以檢查屬性,使用

<target name="foo" if="property"> 
... 
</target> 

這是螞蟻提供,沒有添加任何的依賴性,見here的其他條件。

+0

hi Tonio,謝謝你的回覆。它的工作..........,但我有一些疑問: - > – nitinJi 2011-06-08 18:16:13

+0

做完條件任務後,我想做一些像打印**屬性**的值的東西。如果文件是相同的,那麼它應該打印** true **,如果不是,它應該打印** false **,但對於我來說它只是打印** false **,即使文件相同。 '<目標名稱= 「主」> <條件屬性= 「myProperty的」 值= 「真」 否則= 「假」> \t <回聲消息=「myproperty = $ {myproperty}」/> ' – nitinJi 2011-06-08 18:26:12

+0

我不能得到那個工作,它驅使我瘋了!由於某些奇怪的原因,腳本中的文件匹配任務總是返回「false」,除非file1和file2指向同一個文件。但是,只要我指向不同的文件(即使其中一個是另一個的1:1副本),該值也是錯誤的。任何想法,爲什麼會這樣?這是在Windows 10上使用ant版本1.8.2 – mmo 2016-04-12 11:33:51

0

我在同樣的情況來比較兩個文件,並切換到不同的文件或不同的目標相匹配的文件不匹配......

繼承人的代碼:

<project name="prospector" basedir="../" default="main"> 

<!-- set global properties for this build --> 
<property name="oldVersion" value="/code/temp/project/application/configs/version.ini"></property> 
<property name="newVersion" value="/var/www/html/prospector/application/configs/version.ini"></property> 

<target name="main" depends="prepare, runWithoutDeployment, startDeployment"> 
    <echo message="version match ${matchingVersions}"></echo> 
    <echo message="version mismatch ${nonMatchingVersion}"></echo> 
</target> 

<target name="prepare"> 

    <!-- gets true, if files are matching --> 
    <condition property="matchingVersions" value="true" else="false"> 
     <filesmatch file1="${oldVersion}" file2="${newVersion}" textfile="true"/> 
    </condition> 

    <!-- gets true, if files are mismatching --> 
    <condition property="nonMatchingVersion" value="true" else="false"> 
     <not> 
      <filesmatch file1="${oldVersion}" file2="${newVersion}" textfile="true"/> 
     </not> 
    </condition> 

</target> 

<!-- does not get into it.... --> 
<target name="startDeployment" if="nonMatchingVersions"> 
    <echo message="Version has changed, update gets started..."></echo> 
</target> 

<target name="runWithoutDeployment" if="matchingVersions"> 
    <echo message="Version equals, no need for an update..."></echo> 
</target> 

屬性是正確的,並改變文件內容。 nonMatchingVersions的任務永遠不會開始。