2011-06-08 131 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的任务永远不会开始。