ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Diagnostic Tests with Ant
Pages: 1, 2, 3, 4, 5

In our installation build file, we generate the checksums like this:



<target name="checksum">
  <checksum>
    <fileset dir="build">
      <include name="**/*.class"/>
      <include name="config.xml"/>
    </fileset>
  </checksum>
</target>

For all files with extension .class in the directory build or its subdirectories, Ant generates an MD5 checksum. The checksum will be stored in a file named after the original file's name, with the extension .MD5 added to it. The same happens with the configuration file config.xml. The target checksum has to be executed after all files have been built.

In our diagnostic build file, we verify the checksums:


<target name="checksum">
    <echo message="Verifying checksums of binary
     files..."/>
    <condition property="binary.unchanged">
      <checksum>
        <fileset dir="build">
          <include name="**/*.class"/>
        </fileset>
      </checksum>
    </condition>
    <echo message="Verifying checksum of 
     configuration file..."/>
    <condition property="config.unchanged">
      <checksum file="build/config.xml"/>
    </condition>
</target>

  <target name="binaryChanged" depends="checksum" 
   unless="binary.unchanged">
    <echo level="error" message="ERROR: Binary 
     files changed."/>
  </target>

  <target name="configChanged" depends="checksum" 
   unless="config.unchanged">
    <echo message="WARNING: Configuration file 
     changed."/>
  </target>

Add the targets to the dependencies of the target all.

First, the checksum target will be executed. If the checksums of all class files match the generated checksums, the property binary.unchanged is set to true. However, if at least one class file has been changed, the property doesn't get a value. Next, if the config.xml file is unchanged, the property config.unchanged is set to true. The target binaryChanged, which depends on the target checksum, only gets executed when binary.unchanged doesn't have a value; that is, when at least one of the class files has been changed. Then we output an error message. We do the same with the configuration file, but we issue the message as a warning.

If no files have been changed, our diagnostics build script outputs this:

checksum:
     [echo] Verifying checksums of binary files...
     [echo] Verifying checksum of configuration file...

binaryChanged:

configChanged:

all:

BUILD SUCCESSFUL
Total time: 1 second

Pages: 1, 2, 3, 4, 5

Next Pagearrow