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


AddThis Social Bookmark Button

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

Suppose we change the configuration file. Then we have this output:

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


     [echo] WARNING: Configuration file changed.


Total time: 1 second

If our software is open source, we can do even better and propose restoring changed files to their standard versions. For class files specifically, this means we have to recompile the Java files. Of course, your users have to have the whole JDK (not just a Java runtime) and your source code and the build file for your source code. To restore the changed class files, we delete them and call the compilation task of the installation build file. The specifics of this task depend on your build system.

As an alternative (for example, if your software is closed source), you could put known-good versions of the class files in a .jar or .zip in a safe location and unpack them. You can then replace the changed class files with their known-good versions.

On the other hand, configuration files can be copied from the source directory. In order to do this, we extend the target configChanged appropriately and add a target configRestore:

<target name="configChanged" 
 depends="checksum" unless="config.unchanged">
    <echo message="WARNING: Configuration file 
    <input message="Backup configuration file 
     and restore original? " validargs="y,n"
    <condition property="config.copy">
      <equals arg1="y" arg2="${config.restore}"/>

  <target name="configRestore" 
   depends="configChanged" if="config.copy">
    <echo message="Copying build/config.xml to 
     build/config.xml.1 and restoring configuration 
    <copy file="build/config.xml" 
     tofile="build/config.xml.1" overwrite="true"/>
    <copy file="src/config.xml" todir="build" 

Add the targets to the dependencies of the target all.

If the configuration file has been changed, Ant asks the user if he wants to back up the configuration file and restore the original. If he answers "yes" by pressing Y and Enter, the property config.copy is set. The target configRestore will only be executed when this property is set, backing up build/config.xml to build/config.xml.1 and copying the original src/config.xml to build/config.xml.


We developed an Ant script to run diagnostic tests for a Java application. The script checks whether the version of the Java installation meets a minimum requirement, if some important files haven't been changed, if a specific Java class is in the class path, if a directory exists, etc. After checking all of these prerequisites of the software, the script reports the results to the user. The script can even repair some problems. In addition, the output of the diagnostic test can be used by technical support to help the user quickly, without asking him a whole list of questions.


Koen Vervloesem has a master's degree in computer science and has been freelancing as an IT journalist since 2000, primarily for Dutch IT magazines.

Return to ONJava.com.