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

时间:2011-06-08 17:08:42

标签: ant

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

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

任何人都可以建议我做任何可以做到这一点的ANT任务。

提前致谢。

2 个答案:

答案 0 :(得分:11)

您可以使用以下内容:

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

仅当文件相同时才会设置属性。 然后,您可以使用

检查该属性
<target name="foo" if="property">
...
</target>

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

答案 1 :(得分: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的任务永远不会开始。

相关问题