Creating Ant Tartgets with PAnt

PAnt makes creating Ant targets in python truly trivial:

@target
def ptarget1():
    """Python project ptarget1"""
    ant.echo("echo ptarget1")

PAnt relies on python decorators (decorators are similar to Java annotations) to denote functions that will be invoked when we run Ant targets. “target” decorator uses function name and doc string to set target’s name and description respectively. Other target’s attributes, such as “if”, “unless” and “depends” are supplied using keyword arguments of the decorator. Note that you have to use “if_” instead of “if”. You can also provide the name of the target if it is different from the function name:

@target(name="p.target2", unless="prop.name", depends=ptarget1)
def p_target2():
    """Python project ptarget2"""
    ant.echo("echo ptarget2")


Note how we can simply point to another function in “depends” instead of providing a string name (although the string name is supported as well). Using python functions instead of strings allows for being able to rely on an IDE (such as PyDev) for auto-completion and name verification.

PAnt comes with “pimport” task that creates Ant targets from any python module that uses ‘target’ decorators (you can define your targets in multiple modules as well). The task works much the same way as Ant “import” task, all you need to do is to invoke it somewhere in your Ant build file (“pimport” has to reside outside of any target).

With PAnt you can freely mix and match targets defined in XML and in python. You can also override XML targets in python and vice-versa. However, there is really no reason to be stuck with XML when you can use full power of python. So we recommend creating a small “starter” “build.xml” with a single “pimport” call and have all your target definitions done in python.

PAnt also provides a programmatic way of creating Ant targets using “antarget” function:

    antarget(ptarget2, if_="prop.name", depends=ptarget1)


The signature of “antarget” is similar to the “target” decorator; the main difference is that a function has to be provided as an argument. Note that using decorators requires Jython 2.5 whereas “antarget” can be used with Jython 2.2.

You can download the latest version of PAnt from PAnt project page. PAnt distribution comes with detailed examples.