Tag Archives: Ant-Contrib

Apache Ant – Extract a string from property using regular expression

The Ant-Contrib library is a must have tool if you need to have more control on your build flow. Here is another example on using the propertyRegex task provided by Ant-Contrib to select or replace a string from an input.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Property Regex" default="version" basedir=".">
  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="${basedir}/lib/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <target name="version">
    <propertyregex
      property="select.example"
      input="package.ABC.name"
      regexp="package\.([^\.]*)\.name"
      select="\0"
      casesensitive="false" />

    <echo message="${select.example}"/>

    <propertyregex
      property="replace.example"
      input="package.ABC.name"
      regexp="(package)\.[^\.]*\.(name)"
      replace="\1.DEF.\2"
      casesensitive="false" />

    <echo message="${replace.example}"/>      
  </target>
</project>

Continue reading Apache Ant – Extract a string from property using regular expression

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 – 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

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 – 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