19 September 2016

In the previous two articles we listed our requirements and started exploring how Bash and Apache Ant could help us satisfy them. We ended the last part discussing using the Exec task to run the “tar” command from the operating system. This works well if the target file is present, but what happens if the file is not present? The Ant task will fail with a somewhat cryptic message. Fortunately Ant provides us with the tools to check for the files existence and to fail with a custom error message. Three tasks help us accomplish this goal: echo, waitfor and fail. We have already discussed the echo task. Waitfor and fail both rely on conditions The waitfor task, as its name implies, waits for a condition to be true. Today our condition will be whether or not a file exists. The fail task stops Ant from continuing and prints out a message. Example code below shows how they can be used together.

Ant wait for file example



    <target name="verifyFileExists">
      <echo message="Waiting for Liferay log file."/>
      <echo message="/path/to/file/liferay.log"/>
      <waitfor maxwait="60" maxwaitunit="second">
        <available file="/path/to/file/liferay.log"/>
      </waitfor>
      <fail message="Liferay log file failed to appear.">     
         <condition>
           <not><available file="/path/to/file/liferay.log"/></not>
         </condition>
      </fail>
    </target>

The first action taken by the target is to print a message telling us what is happening. This is done using the echo task. Next it waits 60 seconds for the log file to appear. If the file is already available or appears before 60 seconds have passed the task will end and move on to the next task. If the file never appears it will wait the full 60 seconds and then move to the next task. If the file never appears waitfor does not throw an error or otherwise let you know. This is were the fail task comes into play. The fail task will print an error message and end the process if its condition evaluates to true. This is why we put the <not/> around the available condition. We only want to fail the process if the file is not available. Which is how it reads in the XML markup: <not><available/></not>

Combining this with the Exec task we can use two targets to verify a tar file is present and then extract it. Alternatively it will stop Ant from continuing and print and error message.

Wait for file and then extract it.



    <target name="extractTarFileContents" depends="verifyFileExists">
      <exec executable="tar" failonerror="true">
        <arg value="-pxzf"/>
        <arg value="/path/to/file/liferay_deploy.tar.gz"/>
      </exec>
    </target>

    <target name="verifyFileExists">
      <echo message="Waiting for Liferay log file."/>
      <echo message="/path/to/file/liferay.log"/>
      <waitfor maxwait="60" maxwaitunit="second">
        <available file="/path/to/file/liferay_deploy.tar.gz"/>
      </waitfor>
      <fail message="Liferay log file failed to appear.">     
         <condition>
           <not><available file="/path/to/file/liferay.log"/></not>
         </condition>
      </fail>
    </target>


In this scenario we would run the extractTarFileContents target which depends on the verifyFileExists target. VerifyFileExists would run first and exit with an error message if the liferay_deploy.tar.gz file doesn’t exist. Thus preventing us from trying to extract a file that doesn’t exist and giving us a nice error message telling us why it failed. Testing for existance of files will also be useful to us when we are starting and stopping our application server. We will need to verify that our log files exist before we can scan them for log messages. In the next article we will cover how to find passwords in one set of configuration files and place them in another set of configuration files using Ant.