Tag Archives: Ant

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

Advertisement

Apache Ant – Run piped command in exec task

The Ant <exec> task allows us to run shell command in Ant script. We could provide input arguments like the following example which prints today’s weekday.
build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Piped Commands" default="today" basedir=".">
  <target name="today">
    <exec executable="date" outputproperty="weekday">
      <arg value="+%A" />
    </exec>
    <echo message="Today is : ${weekday}"/>
  </target>
</project>

ant-exec-piped-commands-1
Continue reading Apache Ant – Run piped command in exec task

Ant – Execute Grunt and fail it on error

I want to execute a Grunt build in an Ant build project. This could be done by using <exec>.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Called Grunt" default="grunt" basedir=".">
  <target name="grunt">
    <exec executable="grunt" dir="${basedir}">
      <arg value="build"/>
    </exec>
  </target>
</project>

 

It works quite well, but then i realize if there is error in the grunt execution, Ant will still mark the Ant build successful and this may lead to some downstream failure in a continuous integration workflow.
Continue reading Ant – Execute Grunt and fail it on error

Apache Ant – Get the SVN revision number by exec task

In Apache Ant, we can use the <exec> task to execute shell command in Ant script. Previously i have shown you how to integrate Apache Ant with SVN.

 

If you only want to do some simple SVN actions, you can consider using the <exec> task. The build.xml below is a simple example to get the current SVN version number.
Continue reading Apache Ant – Get the SVN revision number by exec task

Apache Ant – Get the current running Ant version in Ant script

I am trying to figure out which Apache Ant version is running on the Jenkins server. Starting from Apache Ant 1.7.0, you can make use of the <Antversion> task to determine the running Ant version. The following is a simple build.xml which could serve this purpose.

<project name="ant-antversion-example" default="run" basedir=".">
  <target name="run">
    <echo message="Checking ANT version ..."/>
    <antversion property="antversion"/>
    <echo message="  version: ${antversion}"/>
  </target>
</project>

 

Try it out. Continue reading Apache Ant – Get the current running Ant version in Ant script

Apache Ant – Check if a string property contains a certain string

The following example which check if a string property contains a another string.

<project name="ant-string-contains-example" default="run" basedir=".">
  <target name="run">
    <property name="greeting" value="Hello World!"/>
    <condition property="hasWorld">
      <matches pattern="World" string="${greeting}"/>
    </condition>
    <fail message="No world in the greeting!" unless="hasWorld"/>
    <!-- Move on if no failture -->
    <echo message="Passed!"/>
  </target>
</project>

 

Done =)

Reference: StackOverflow – Ant: how to fail if property contains a certain string

Apache Ant – Iteration within a specific number range

Previously i have published a post about using for loop in Ant to read a text file line by line.

 

The <for> task requires a list attribute containing a comma separated string. Here is an example.

<echo message="The first five letters of the alphabet are:"/>
<for list="a,b,c,d,e" param="letter">
  <sequential>
    <echo>Letter @{letter}</echo>
  </sequential>
</for>

Continue reading Apache Ant – Iteration within a specific number range