Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ant:如何比较两个文件的内容_Ant - Fatal编程技术网

ant:如何比较两个文件的内容

ant:如何比较两个文件的内容,ant,Ant,我想使用ANT比较两个文件(比如file1.txt、file2.txt)的内容 如果文件内容相同,则应将某些“属性”设置为true;如果内容不相同,则应将“属性”设置为false 谁能给我推荐一个蚂蚁任务来完成这个任务吗 提前感谢。您可以使用以下内容: <condition property="property" value="true"> <filesmatch file1="file1" file2="file2"/> </con

我想使用ANT比较两个文件(比如file1.txt、file2.txt)的内容

如果文件内容相同,则应将某些“属性”设置为true;如果内容不相同,则应将“属性”设置为false

谁能给我推荐一个蚂蚁任务来完成这个任务吗


提前感谢。

您可以使用以下内容:

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

这将仅在文件相同时设置属性。 然后,您可以使用

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

...

这在ant中可用,没有添加依赖项,有关其他条件,请参阅。

我处于相同的情况下,可以比较两个文件,并根据文件匹配或文件不匹配切换到不同的目标

代码如下:

<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>

属性正确,并随文件内容的更改而更改。
非匹配版本的任务从未开始。

完成条件任务后,我想做一些事情,比如打印属性的值。如果文件相同,那么它应该打印为真,如果不是,它应该打印为假,但对我来说,即使文件相同,它也只能打印为假“我不能让它工作,它快把我逼疯了!出于某种奇怪的原因,脚本中的filematch任务总是返回“false”,除非file1和file2指向同一个文件。但只要我指向不同的文件(即使其中一个是另一个的1:1副本),该值就是false。你知道为什么会这样吗?这是在使用ant版本1.8.2的Windows 10上,不确定是什么导致了我们的问题。这可能是对生成文件中属性的误用,导致在计算实际条件之前设置condition属性。下面是一个非常简单的示例:。如果两个文件完全相同,则打印为true,否则不会打印任何内容。如果属性未设置为true,则可以通过添加