Home Fabio Cortesi Stefan Jäger

Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.

 

 
 

 

Ant offers some XML features like the xslt task, which transforms XML files into other files. But unfortunately, with Ant you can’t make simple XPath Queries on a specific XML file.

 

Luckily, there is a nice open source solution called XmlTask, which offers many possibilities to work with XML files.

 

  1. Download the latest version of XmlTask from http://xmltask.sf.net
  2. Create a task definition in Ant
    1
    2
    3
    4
    
    <taskdef 
      name="xmltask" 
      classname="com.oopsconsultancy.xmltask.ant.XmlTask"
      classpath="./xmltask-v1.15.1.jar" />
  3. Use it with the <xmltask> definition

    For example, read out the number of a specific node in the XML file:

    1
    2
    3
    
    <xmltask source="someFile.xml">
      <copy path="count(//aNode)" property="numberOfNodes" />
    </xmltask>

    Or change an attribute:

    1
    2
    3
    
    <xmltask source="someFile.xml" dest="toAnotherFile.xml">
      <attr path="//aNode[1]" attr="enabled" value="true"/>
    </xmltask>

 

There are many other possibilites to work with XML files inside Ant. Check out the documentation at http://xmltask.sf.net.

 

 

 
 

 

There are some kind of tasks, which Ant won’t do. One of them is the loop. There is no simple way to implement a dynamic for or while loop in Ant. What do i mean with dynamic? Let me make an example. Ant reads out a number of iterations from a property file (we call it the number x) and should perform a loop x-times to call another Ant task. And this can only hardly be done with Ant.

 

 

image But fortunately, there is Groovy. Groovy is a scripting language, which runs in a Java Virtual Machine. And because of that, it can be easily integrated with Ant.

 

 

 

 

 

 

 

Continue Reading

 

 

 
 

 

In some cases, ANT has it’s limitations. For example, it is not really easy to create an if-else structure or a for-loop. For this reason, a few people started long time ago the Ant-Contrib project. Just download it and put the jar file in the lib folder of the project. Then, just at following line to the build file:

<taskdef resource="net/sf/antcontrib/antcontrib.properties"></taskdef>

Now, you can start using constructs like if, foreach and combine these tasks with usual ANT tasks. Here a little example:

with antcontrib:

<target name="theultimateconditionmachine">
  <if>
    <equals arg1="${foo}" arg2="bar" />
    <then>
      <echo message="property foo is bar" />
    </then>
    <else>
      <echo message="property foo is not bar" />
    </else>
  </if>
</target>

without antcontrib:
if you want to perform the same task with ant, you need to create 4 (!) ant targets:

<target name="noway">
  <antcall target="conditionIf"/>
  <antcall target="conditionElse"/>
</target>
 
<target name="conditionDef">
  <condition property="conditionIsTrue">
    <equals arg1="${foo}" arg2="bar"/>
  </condition>
</target>
 
<target name="conditionIf" depends="conditionDef" if="conditionIsTrue">
  <echo message="property foo is bar"/>
</target>
 
<target name="conditionElse" depends="conditionDef" unless="conditionIsTrue">
  <echo message="property foo is not bar"/>
</target>