Generate Checksum for directories using Ant build

2019-05-14 15:22发布

问题:

I tried to generate the checksum for directory using ant.

I have tried the below command, but it generates recursively inside each folder for each file.

<target name="default" depends="">
    <echo message="Generating Checksum for each environment" />
    <checksum todir="${target.output.dir}" format="MD5SUM" >
        <fileset dir="${target.output.dir}" />
    </checksum>
</target>

I just want to generate one checksum for particular directory using Ant command. How do I do that?

回答1:

You want to use the totalproperty attribute. As per the documentation this property will hold a checksum of all the checksums and file paths.

e.g.

<target name="hash">
  <checksum todir="x" format="MD5SUM" totalproperty="sum.of.all">
    <fileset dir="x"/>
  </checksum>
  <echo>${sum.of.all}</echo>
</target>

Some other general notes.

  • This is not idempotent. Each time you run it you will get a new value because it includes the previous hash file in the new hash (and then writes a new hash file). I suggest that you change the todir attribute to point elsewhere
  • It's a good idea to name your targets meaningfully. See this great article by Martin Fowler for some naming ideas
  • You don't need the depends attribute if there's no dependency.