Common.xml 20080129

From HaFrWiki
Jump to: navigation, search

Contents

The file below is part of the Ant-templates. A detailed description will be added later. The stopwatch task will not work on your system. The task is not defined in standard Ant.

<?xml version="1.0"?>
<!--
      ==============================================================
      Ant Common file. A handy ant-build include file.

      ==============================================================
      The common xml file is standard included by the local.xml.
      This Ant file needs to be set: JAVA_HOME and ANT_HOME.
      
      Common.xml includes the Ant-Contrib library which is 
      specified by the environmental ANT_HOME variable.

      Common.xml defines the variables:
      - env                         : Environment
      - both                        : false
      - common.classpath            : java classpath
      - common.general.classpath    : JAVA_HOME jars
      - copy.granularity            : Copy sensability 
                                      (5500)
      - copy.preservelastmodified   : Preserve settings for copy 
                                      (true)

      Common.xml contains the targets:
      - getos     : 
            Gets the Operation system and sets isUnix 
            or isWindoze
      - internal.show.properties
            Shows/displays common.xml internal used settings.
      - internal.show.sync.properties
            Shows/displays common.xml internal used properties.
      - internal.test.availability
            Tests the availability of a drive.
      - internal.test.availability.directory
            Tests the availability of a directory on a drive.
            Can only be used after internal.test.availability
      - internal.show.availability
            Shows the availability of drives
      - internal.general.copy
            Copies given source files to destination if newer
            and based on the given filter (**/*.* format).
            One parameter call (with underlying structure). 
      - internal.general.copy.id
            Copies given source files (as fileset with id).
            This makes every thinkable copy possible.
            Currently no synchronisation version available.
            Since 1.0.5.1
      - internal.general.copy.param
            Copies given source files to destination if newer
            and based on the given filter (**/*.* format). 
            All parameters are be specified at call-time.
      - internal.general.sync
            Synchronizes 2 directory structures to each other.
            Uses the internal.general.copy.param version.
      - internal.general.java
            Performs a java FileFind command.
      - internal.general.doscmd
            Performs a dos command.
      - internal.zip.fileset
            Zips a given fileset referenced by its id 
            to a given zipfile.
      - internal.print.fileset
            Shows/prints a given fileset referenced by 
            its id to the console.
      - internal.print.path
            Shows/prints a given fileset referenced by 
            its id to the console.
      - isBoth
            Utility task for synchronize
      - timestamp
            Utility task for settings the date and time 
            format.            
      - startstopwatch
            Utility task for using a stopwatch function.
            The name of the stopwatch has not been resolved.

      ==============================================================
      Creator  : Harm Frielink, June 2007
      Modified : 
      1.0.1.0 - 25 Jun 2007 - Created
      1.0.1.1 - 28 Jun 2007 - Adepted for sync (granularity).
      1.0.1.2 - 02 Jun 2007 - Added preservelastmodified.
                              Please be aware of these 2
      1.0.2.1 - 03 Jul 2007 - isboth and both 
      1.0.2.1 - 03 Jul 2007 - isboth and both
      1.0.2.2 - 28 Jul 2007 - VCS Subversion
      1.0.2.3 - 30 Oct 2007 - Added Copy task for pictures(How to add a set)
      1.0.2.4 - 01 Nov 2007 - Added to the pictures Copy task also the bak and renamed to 'internal.general.iaa.copy'
                              And removed to the local build.xml file.
      1.0.3.1 - 01 Nov 2007 - Started to use the fileset id instead of the file filter. (First attemp).
      1.0.4.1 - 05 Nov 2007 - Added all tasks in comment and edit zip- and print- target
                              with selectors ad fileset references.
      1.0.5.1 - 28 Jan 2008 - Copy files with file id instead of file filter.
                              The internal.general.copy.id is the preferred way to copy files.
-->
<project name="common">

   <!-- ======================================================= -->
   <!-- Properties for the common build                         -->
   <!-- ======================================================= -->
   <property environment="env" />

   <!-- Versioning information -->
   <property name="common.version.number"  value="1.0.5.1"       />
   <property name="common.version.date"    value="28 Jan 2008"   />
   <property name="common.version.copy"    value="HJMF 2000-2008"/>
   <property name="common.stopwatch"       value="CommonWatch"   />

   <!--  5.5 seconds or 5500 milliseconds is used. -->
   <property name="copy.granularity"          value="5500" />
   <property name="copy.preservelastmodified" value="true" />

   <!-- ======================================================= -->
   <!-- Antiloppe or Ant Contrib                                -->
   <!-- V: 1.0.1.0 - Created                                    -->
   <!-- ======================================================= -->
   <taskdef resource="net/sf/antcontrib/antlib.xml">
      <classpath>
         <pathelement location="${env.ANT_HOME}\ant-contrib\lib\ant-contrib.jar"/>
      </classpath>
   </taskdef>

   <!-- ======================================================= -->
   <!-- Variables for the common build                          -->
   <!-- ======================================================= -->
   <var name="both" value="false" />

   <!-- ======================================================= -->
   <!-- Classpath id reference definitions                      -->
   <!-- V: 1.0.1.0 - Created                                    -->
   <!-- ======================================================= -->
   <path id="common.classpath">
      <pathelement location="${env.CLASSPATH}"             />
      <pathelement location="./classes"                    />
   </path>

   <!-- ======================================================= -->
   <!-- Example of a common.general.classpath (No-Good)         -->
   <!-- ======================================================= -->
   <path id="common.general.classpath">
      <pathelement location="${env.JAVA_HOME}/lib/dt.jar"     />
      <pathelement location="${env.JAVA_HOME}/lib/tools.jar " />
      <pathelement location="e:/usr/jars/Speeltuin.jar"       />
      <pathelement location="E:/Apache/jakarta-oro-2.0.8/jakarta-oro-2.0.8.jar" />
      <pathelement location="E:/Apache/logging-log4j-1.2.12/dist/lib/log4j-1.2.12.jar" />
   </path>

   <!-- ======================================================= -->
   <!-- Gets the operation system and sets isXXX variables      -->
   <!-- V: 1.0.1.0 - Created                                    -->
   <!-- ======================================================= -->
   <target name="getos">
      <!-- only one of the following will be set true -->
      <condition property="isWindoze">
         <os family="windows"/>
      </condition>
      <condition property="isUnix">
         <os family="unix"/>
      </condition>
   </target>

   <!-- ======================================================= -->
   <!-- General show utility, used to show settings used in     -->
   <!-- common.xml.                                             -->
   <!-- V: 1.0.1.0 - Created, 1.0.1.2  preservelastmodified     -->
   <!--    1.0.5.1 - Added environment variables                -->
   <!-- ======================================================= -->
   <target name="internal.show.properties">
      <echo>--------------- Common Used Properties --------------------------</echo>
      <echo>Common Version: ${common.version.number} - ${common.version.date}</echo>
      <echo>Common CR     : ${common.version.copy}</echo>
      <echo>Granularity   : ${copy.granularity}</echo>
      <echo>Last modified : ${copy.preservelastmodified}</echo>
      <echo>--------------- Environment Variables ---------------------------</echo>
      <echo>ANT_HOME      : ${env.ANT_HOME}</echo> 
      <echo>ANT_VERSION   : ${env.ANT_VERSION}</echo>
      <echo>CLASSPATH     : ${env.CLASSPATH}</echo>
      <echo>JAVA_HOME     : ${env.JAVA_HOME}</echo>
      <echo>JAVA_VERSION  : ${env.JAVA_VERSION}</echo>
      <echo>User name     : ${env.USERNAME}</echo>
      <echo>Windows root  : ${env.SystemRoot}</echo>
      <echo>Run Date-Time : ${TODAY_NL} (dutch)</echo>
      <echo>-----------------------------------------------------------------</echo>
   </target>

   <!-- ======================================================= -->
   <!-- Shows the settings of a sync for using the              -->
   <!-- internal.general.copy with the filter method            -->
   <!-- ======================================================= -->   
   <target name="internal.show.sync.properties">
      <propertycopy name="issp.message"        from="${param}.message" />
      <propertycopy name="issp.srcdir"         from="${param}.srcdir" />
      <propertycopy name="issp.todir"          from="${param}.todir" />
      <propertycopy name="issp.filter"         from="${param}.filter" />
      <propertycopy name="issp.verbose"        from="${param}.verbose" />
      <propertycopy name="issp.copydirection"  from="${param}.copydirection" />
      <echo>--------------- Common Sync Properties --------------------------</echo>
      <echo>Message       : ${issp.message}</echo>
      <echo>Source dir    : ${issp.srcdir}</echo>
      <echo>Destination   : ${issp.todir}</echo>
      <echo>Verbose       : ${issp.verbose}</echo>
      <echo>Filter        : ${issp.filter}</echo>
      <echo>Direction     : ${issp.copydirection}</echo>
   </target>

   <!--
         General utility for checking if a drive is available.
         This routine is called by other ant tasks.
         The caller should use trycatch.
         If an exception is caught the drive is not available.
         @param   param       : Name of the base. Exact names are derived.
         @derived isAvailable : The availability of the drive.
         V: 1.0.1.0 - 25 Jun 2007
   -->
   <target name="internal.test.availability">
      <!--
      <propertycopy name="isa.name" from="${param}.name" />
      <echo>${isa.name}</echo>
      -->
      <propertycopy name="isa.isAvailable" from="${param}.isaDir" />            
   </target>

   <!--
         General test utility, routine is called by other ant tasks.
         This test can only be used if the previous test has a succesful result.
         So first call 'internal.test.availability'. 
         Tests the availability of a directory on a drive. 
         The caller should use trycatch mechanism.
         If an exception is caught the drive is not available.
         @param   param1      : Name of the base. Exact names are derived.
         @param   param2      : Name of the directory. Exact name is derived.
         @derived isAvailable : The availability of the drive/directory.
         V: 1.0.1.0 - 25 Jun 2007
   -->
   <target name="internal.test.availability.directory">
      <propertycopy name="ita.isAvailable"  from="${param1}.isaDir" />
      <propertycopy name="ita.srcdir"       from="${param2}.srcdir" />
      <available file="${ita.srcdir}" type="dir" property="ita.isAvailable" />
      <echo>${ita.srcdir} is ${ita.isAvailable}</echo>
   </target>

   <!--
        General show utility, routine is called by other ant tasks.
        Before using this routine be sure isaDir is set to true or false,
        and has to be defined.
        @param   param       : Name of the base. Exact names are derived.
        @derived driveName   : Full drive name description.
        @derived driveLetter : Driveletter belonging to the description
        @derived isAvailable : The availability of the drive.
        V: 1.0.1.0 - 25 Jun 2007
   -->
   <target name="internal.show.availability" >
      <propertycopy name="isa.isAvailable" from="${param}.isaDir" />
      <propertycopy name="isa.driveName"   from="${param}.name" />
      <propertycopy name="isa.driveLetter" from="${param}.letter" />
      <if>
        <equals arg1="${isa.isAvailable}" arg2="true" />
        <then>
            <echo>${isa.driveName} : available as ${isa.driveLetter}</echo>
        </then>
        <else>
            <echo>${isa.driveName} : NOT available!</echo>
        </else>
      </if>
    </target>

   <!-- 
        General copy utility, routine is called by other ant tasks.
        This is a version using one param with fileset id. 
        This is currently the prefered copy version.
        @param isDir    Availability of the Source Drive
        @param message  Message text for description of the action
        @param todir    Destination drive and path location
        @param srcId    RefID to a fileset.
        @param verbose  Verbose operation
        V: 1.0.4.5 - 28 Jan 2008 - From scrage
   -->
   <target name="internal.general.copy.id">
      <propertycopy name="igci.isDir"    from="${param}.isDirSD1" />
      <if>
         <equals arg1="${igci.isDir}" arg2="true"/>
      <then>
         <propertycopy name="igci.message" from="${param}.message" />
         <propertycopy name="igci.srcId"   from="${param}.srcId"   />
         <propertycopy name="igci.todir"   from="${param}.todir" />
         <propertycopy name="igci.message" from="${param}.message" />
         <propertycopy name="igci.verbose" from="${param}.verbose" />
         <copy todir="${igci.todir}"
               granularity="${copy.granularity}"
               preservelastmodified="${copy.preservelastmodified}"
               verbose="${igci.verbose}">
            <fileset refid="${igci.srcId}"/>
         </copy>         
      </then>
      <else>
         <echo>${igci.todir} is currently not available!</echo>
      </else>
      </if>
   </target>


   <!-- 
        General copy utility, routine is called by other ant tasks.
        @param param    Name of the base. Exact names are derived.
        @derived isDir    Availability of the Destination Drive
        @derived message  Message text for description of the action
        @derived todir    Destination drive and path location
        @derived srcdir   Source drive path location
        @derived verbose  Verbose operation
        @derived filter   Filter file name.
        V: 1.1.1.0 - 25 Jun 2007 - Granularity
           1.1.1.1 - 02 Jul 2007 - preservelastmodified
           1.1.2.1 - 03 Jul 2007 - isboth and both
   -->
   <target name="internal.general.copy"
           depends="isboth">
      <propertycopy name="isa.isDir"    from="${param}.isDirSD1" />
      <if>
         <!-- equals arg1="${isa.isDir}" arg2="true"/-->
         <equals arg1="${both}" arg2="true" />
      <then>
         <propertycopy name="isa.message"    from="${param}.message" />
         <propertycopy name="isa.srcdir"     from="${param}.srcdir" />
         <propertycopy name="isa.todir"      from="${param}.todir" />
         <propertycopy name="isa.filter"     from="${param}.filter" />
         <propertycopy name="isa.verbose"    from="${param}.verbose" />
         <echo>${isa.srcdir} --> ${isa.message} --> ${isa.todir}</echo>
         <copy todir="${isa.todir}" 
               granularity="${copy.granularity}"
               preservelastmodified="${copy.preservelastmodified}"
               verbose="${isa.verbose}">
            <fileset dir="${isa.srcdir}" >
               <include name="${isa.filter}" />
            </fileset>
         </copy>
      </then>
      <else>
         <echo>${isa.todir} is currently not available!</echo>
      </else>
      </if>
   </target>


   <!--
        Synchronize a directory beased on the sync rules.
        copydirection { "both", "a21", "12a" }
        @param param    Name of the base. Exact names are derived.
        V: 1.0.1.1 - 25 Jun 2007
           1.1.2.1 - 03 Jul 2007 - isboth and both
   -->
   <target name="internal.general.sync"
           depends="isboth">
      <propertycopy name="igs.isDirSD1"      from="${param}.isDirSD1" />
      <propertycopy name="igs.isDirSD2"      from="${param}.isDirSD2" />
      <propertycopy name="igs.copydirection" from="${param}.copydirection" />
      <propertycopy name="igs.message"       from="${param}.message" />
      <propertycopy name="igs.dir1"          from="${param}.srcdir" />
      <propertycopy name="igs.dir2"          from="${param}.todir" />
      <propertycopy name="igs.verbose"       from="${param}.verbose" />
      <propertycopy name="igs.filter"        from="${param}.filter" />
      <if>
         <!--equals arg1="${igs.isDirSD1}" arg2="true" /-->
         <equals arg1="${both}" arg2="true" />
         <then>
            <switch value="${igs.copydirection}">
               <case value="a2l">
                  <var name="do.the.copy" value="true" />
               </case>
               <case value="both">
                  <var name="do.the.copy" value="true" />
               </case>
               <case value="l2a">
                  <var name="do.the.copy" value="false" />
               </case>
               <default>
                  <fail message="Unvalid switch, aborting" />
               </default>
            </switch>
            <if>
               <equals arg1="${do.the.copy}"  arg2="true" />
               <then>
                  <antcall target="internal.general.copy.param">
                     <param name="param.isDir"   value="${igs.isDirSD1}"    />
                     <param name="param.message" value="${igs.message}, forward" />
                     <param name="param.srcdir"  value="${igs.dir1}"        />
                     <param name="param.todir"   value="${igs.dir2}"        />
                     <param name="param.verbose" value="${igs.verbose}"     />
                     <param name="param.filter"  value="${igs.filter}"      />
                  </antcall>
               </then>
            </if>
         </then>
      </if>
      <if>
         <!--equals arg1="${igs.isDirSD2}" arg2="true" /-->
         <equals arg1="${both}" arg2="true" />
         <then>
            <switch value="${igs.copydirection}">
               <case value="a2l">
                  <var name="do.the.copy" value="false" />
               </case>
               <case value="both">
                  <var name="do.the.copy" value="true" />
               </case>
               <case value="l2a">
                  <var name="do.the.copy" value="true" />
               </case>
               <default>
                  <fail message="Unvalid switch, aborting" />
               </default>
            </switch>
            <if>
               <equals arg1="${do.the.copy}"  arg2="true" />
            <then>
               <antcall target="internal.general.copy.param">
                  <param name="param.isDir"   value="${igs.isDirSD2}"      />
                  <param name="param.message" value="${igs.message}, back" />
                  <param name="param.srcdir"  value="${igs.dir2}"          />
                  <param name="param.todir"   value="${igs.dir1}"          />
                  <param name="param.verbose" value="${igs.verbose}"       />
                  <param name="param.filter"  value="${igs.filter}"        />
               </antcall>
            </then>
            </if>
         </then>
      </if>
   </target>

   <!-- 
        General copy utility, routine is called by other ant tasks.
        This is a version using parameters version. 
        Prefered usage for copying is 'internal.general.copy'.
        @param isDir    Availability of the Source Drive
        @param message  Message text for description of the action
        @param todir    Destination drive and path location
        @param srcdir   Source drive path location
        @param verbose  Verbose operation
        @param filter   Filter (since 1.0.4.5)
        V: 1.0.4.5 - 25 Jun 2007 - From older build.xml
   -->
   <target name="internal.general.copy.param">
      <!--
      <echo>${param.isDir}</echo>
      <echo>${param.message}</echo>
      <echo>${param.srcdir}</echo>
      <echo>${param.todir}</echo>
      <echo>${param.verbose}</echo>
      <echo>${param.filter}</echo>
      -->
      <if>
         <equals arg1="${param.isDir}" arg2="true"/>
      <then>
         <echo>${param.message}</echo>
         <copy todir="${param.todir}" 
               granularity="${copy.granularity}"
               preservelastmodified="${copy.preservelastmodified}"
               verbose="${param.verbose}">
            <fileset dir="${param.srcdir}" >
               <include name="**/*.*" />
            </fileset>
         </copy>
      </then>
      <else>
         <echo>${param.todir} is currently not available!</echo>
      </else>
      </if>
   </target>

   <!--
      Executes a doscmd.
      @param.dir     : Path, directory where the cvommand is located
      @param.command : Command to be executed.
   -->
   <target name="internal.general.doscmd"
           description="Executes a command" >
      <echo>${env.windir}\system32</echo>
      <exec dir="${param.dir}" 
            executable="cmd" 
            failonerror="Errrror" >
         <arg line="${param.command}" />
      </exec>
   </target> 

   <!-- ======================================================= -->
   <!-- Performs the java command FileFind                      -->
   <!-- @param.dir   : Path, directory                          -->
   <!-- @param.filter: Dos filter like *.*                      -->
   <!-- V: 1.0.1.2 - Created                                    -->
   <!-- ======================================================= -->
   <target name="internal.general.java">
      <java classname="com.frielink.nl.filefind.FileFind"
            classpathref="common.general.classpath">
         <arg value="-d" />
         <arg value="${param.dir}" />
         <arg value="-f" />
         <arg value="${param.filter}" />
      </java>
   </target>


   <!-- ======================================================= -->
   <!-- Tools, Helptasks                                        -->
   <!-- Sets the usage for date and time                        -->
   <!-- ======================================================= -->
   <target name="timestamp" 
           description="Sets the the timestamp of this ant-task-execution">
      <tstamp>
         <format property="TODAY_NL" pattern="dd MMM yyyy  HH:mm:ss" locale="nl" />
         <format property="TODAY_UK" pattern="yyyy MM  dd  HH:mm:ss" locale="uk" />
      </tstamp>
   </target>

   <!-- ======================================================= -->
   <!-- Checks if both drives are available.                    -->
   <!-- Used by copy and sync. Be carefull param's usage!       -->
   <!-- ======================================================= -->
   <target name="isboth">
      <propertycopy name="isb.isDirSD1"      from="${param}.isDirSD1" />
      <propertycopy name="isb.isDirSD2"      from="${param}.isDirSD2" />
      <if>
         <equals arg1="${isb.isDirSD1}" arg2="true" />
         <then>
            <if>
            <equals arg1="${isb.isDirSD2}" arg2="true" />
            <then>
               <var name="both" value="true" />
            </then>
            <else>
               <var name="both" value="false" />
            </else>
            </if>
         </then>
         <else>
            <var name="both" value="false" />
         </else>
      </if>
   </target>

   <!-- ======================================================= -->
   <!-- Prints a path in a readable format using the            -->
   <!-- Using the line separator as cutter.                     -->
   <!-- ======================================================= -->
   <target name="internal.print.path"
           description="Prints/Shows the path in a readable format">
      <propertycopy name="gpp.refid"        from="param.refid" />
      <pathconvert pathsep  = "${line.separator}"
                   property = "pretty.print.path"
                   refid    = "${gpp.refid}" />
      <echo message= "${pretty.print.path}" />
   </target>

   <!-- ======================================================= -->
   <!-- Prints the content of a fileset represented by an id.   -->
   <!-- Using the line separator as cutter.                     -->
   <!-- ======================================================= -->
   <target name="internal.print.fileset"
           description="Prints/Shows the files in a fileset on a readable format">
      <propertycopy name="gpf.refid"   from="param.refid" />
      <pathconvert pathsep  = "${line.separator}"
                   property = "pretty.print.fileset"
                   refid    = "${gpf.refid}" />
      <echo message= "${pretty.print.fileset}" />
   </target>

   <!-- ======================================================= -->
   <!-- Zips a fileset to a given zippfilename                  -->
   <!-- ======================================================= -->
   <target name       = "internal.zip.fileset"
           description= "Creates a zip file">
      <propertycopy name="izf.refid"        from="param.refid" />
      <zip destfile= "${param.destfile}"
           update  = "${param.update}"
           comment = "${param.comment}" >
         <zipfileset refid= "${izf.refid}" />
      </zip>                                                         
   </target>

   <!-- ======================================================= -->
   <!-- Starts a stopwatch                                      -->
   <!-- ======================================================= -->
   <target name="startstopwatch">
      <stopwatch name="${stopwatch.name}" />
   </target>
</project>

See also