Category Archives: Ant

Apache Ant – Concatenate text files

This is an example showing how to concatenates files using Ant script.

1. Assume you have the following files in your Ant project folder.

ant-for-example
  | - build.xml
  | - Text-1.txt
  | - Text-2.txt
  | - Text-3.txt

Continue reading Apache Ant – Concatenate text files

Apache Ant – Error/Exception Handling

The Ant-Contrib library also provides <trycatch> task for error/exception handling. Here is an example of build.xml which demonstrate the usage and again it requires the ant-contrib-1.0b3.jar which i place it in the ant-lib folder.
Continue reading Apache Ant – Error/Exception Handling

Apache Ant – Capture Ant output log using recorder task

There is a <record> task in Ant so you could save the Ant output during the Ant Build. Here is an example.

1. build.xml

<project name="ant-recorder-example" default="record" basedir=".">
  <target name="record">
    <record name="${basedir}/build.log" action="start" append="yes"/>
      <!--
        Whatever ant tasks being executed here,
        the output would be recorded by the recorder
      -->
      <echo message="Hello world"/>
      <echo message="All the above output will be recorded down in the build.log."/>
      <echo message="After the run, check the build.log."/>
    <record name="${basedir}/build.log" action="stop" append="yes"/>
  </target>
</project>

Continue reading Apache Ant – Capture Ant output log using recorder task

Apache Ant – for loop example for reading a text file

The Ant-Contrib library also provides u a <for> task to do iteration. Here is an simple Ant example which read a text files and print it to the Ant build output.

1. This is the structure of our Ant project.

ant-for-example
  | - ant-lib
  |    | - ant-contrib-1.0b3.jar
  | - build.xml
  | - text.txt

 

2. text.txt

1. Hello World
2. Welcome to Eureka!
3. Goodbye

Continue reading Apache Ant – for loop example for reading a text file

Maven – Run sshexec and scp in maven-antrun-plugin

If you want to use the <sshexec> or <scp> tasks in the maven-antrun-plugin, you may find the following error.

  • [ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (default-cli) on project fraudcheck: An Ant BuildException has occured…

 

Just like what we have discussed in Apache Ant – Using scp and sshexec tasks in build.xml, you need to define the <sshexec> or <scp> tasks in the Ant script. Try to configure your pom.xml as follow.

...
<!-- Deployment using Ant -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>
  <dependencies>
    <!-- Include Ant-Contrib tasks -->
    <dependency>
      <groupId>ant-contrib</groupId>
      <artifactId>ant-contrib</artifactId>
      <version>20020829</version>
    </dependency>
    <!-- Include sshexec task -->
    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant-jsch</artifactId>
      <version>1.9.2</version>
    </dependency>
    <dependency>
      <groupId>com.jcraft</groupId>
      <artifactId>jsch</artifactId>
      <version>0.1.50</version>
    </dependency>
  </dependencies>
  <configuration>
    <tasks>
      <!-- Add the sshexec and scp task -->
      <taskdef name="sshexec"
        classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
        classpathref="maven.plugin.classpath" />
      <taskdef name="scp"
        classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp"
        classpathref="maven.plugin.classpath" />

      <!-- Rest of the Ant script -->
    </tasks>
  </configuration>
  <goals>
    <goal>run</goal>
  </goals>
</plugin>
...

 

Done =)

Reference:

Ant – Stop the script without failing the build

We can use the <fail> Ant task to exit the Ant script but this will fail the build.

Sometimes we may have some checking during the build such that it could exit peacefully without build failure in certain cases. So in this case, we can’t use the <fail> Ant task.

A workaround is to use the <if> Ant-contrib task to create a boolean variable and whether continue or not could be determined by checking this boolean in the <target> if attribute.

Here is an example:
build.xml
Continue reading Ant – Stop the script without failing the build

Apache Ant – Check if a file exists with wildcard

The <available> command can help you to check if a file or folder exists. Unfortunately it doesn’t support wildcard checking. Here is an workaround using the <pathconvert> such that we can check if a file or folder exists with wildcard.

<pathconvert property="filePath" setonempty="false" pathsep=" ">
  <path>
    <fileset dir="./" includes="a_file_name_with_wildcard*" />
  </path>
</pathconvert>
<condition property="fileExists">
  <resourcecount when="greater" count="0">
    <fileset file="${filePath}"/>
  </resourcecount>
</condition>
<if>
  <istrue value="${fileExists}"/>
  <then>
    <echo message="File exists"/>
  </then>
  <else>
    <echo message="File NOT exists"/>
  </else>
</if>

 

Please note that it only works if there is only one file matched the wildcard file name. This is because the <pathconvert> would returns more than one file path if there are more than one file. So it’s not a perfect solution but should be good enough for you to explore more from this example.

Done =)

Reference: StackOverflow – How to use wildcard in Ant’s Available command

Apache Ant – Using scp and sshexec tasks in build.xml

Unlike the Ant Contrib library which could be included thru the <taskdef>, the <scp> and <sshexec> tasks could not be included in this way or it will throw the following errors.

  • A class needed by class org.apache.tools.ant.taskdefs.optional.ssh.Scp
    cannot be found: com/jcraft/jsch/…

 

If you want to make use of the SCP protocol to do file transfer in Apache Ant, first, visit JCraft and download the jsch-0.1.50.jar.

There are 2 ways to enable the <scp> and <sshexec> tasks in the Ant build.xml.
Continue reading Apache Ant – Using scp and sshexec tasks in build.xml

Apache Ant – String to uppercase / lowercase using ScriptDef

In Apache Ant, we can define custom task using MacroDef.

 

Other than that, Apache Ant is now shipped with a Javascript engine so we could use ScriptDef to define our own task in Javascript. The following example defines two tasks converting a string to uppercase and lowercase.
Continue reading Apache Ant – String to uppercase / lowercase using ScriptDef

Linux – Wait until a specific pattern is written on log using tail and sed

In the Ant build.xml, i need to make sure the JBoss server is started before running the test cases. There are 2 possible approaches.

  • Using twiddle.sh to check the status
  • Monitoring server.log until “Started in” is written on it

 

I took the 2nd approach. Here is an example output log when the JBoss server is started successfully.
Continue reading Linux – Wait until a specific pattern is written on log using tail and sed