PAnt is a Python/jython module that allows for defining Ant targets and tasks in Jython. Instead of XML, PAnt relies on Python dictionaries, lists and/or Python’s named (keyword) arguments. In some cases, positional arguments are supported as well. Targets are defined using decorators. Here is an obligatory Hello World example:
@target def helloTarget(): # using python's named arguments: ant.echo( message="Hello World!") # using positional arguments (available for select tasks) ant.echo( "Hello World!") # using dictionaries: ant.exec_task("echo", {"message" : "Hello World!"})
PAnt comes with several custom Ant tasks to facilitate the use of Python and PAnt from Ant.
The rules for translating Ant XML syntax to python are straightforward and are similar to converting from XML to JSON:
* An XML attribute is represented by “name” : “value” dictionary entry (or a named argument, which is the same thing is python).
* An XML element is represented by a dictionary (you can use “dict” function introduced in python 2.2 if you prefer named arguments notation).
* Repeating XML elements (e.g., multiple filesets) are expressed as a list of dictionaries.
* Alternatively, you can use a suffix with an underscore to make names of repeating elements unique (e.g., “fileset_1″, fileset_2”).
Refer to this file for examples illustrating these rules.
PAnt allows to call any optional or custom task. You can even define new custom tasks in PAnt using “ant.taskdef” call.
Ant targets can be defined using decorators or programmatically as documented here.
PAnt supports positional arguments where it makes sense. Positional arguments can only work for tasks with simple attribute lists where each attribute has unique position. For example, positional arguments won’t work for the “copy” task which can have either “tofile” or “todir” in the third place in its list of attributes.
PAnt supports positional arguments only for “property”, “mkdir” and “echo” tasks out of the box. You can extend this support by modifying “ant_attr_map” module variable. The type of this variable is a simple dictionary of lists:
ant_attr_map={"property" : ["name", "value"], "echo" : ["message"], "mkdir" : ["dir"]}
Each list defines attribute names. PAnt will translate the arguments provided to the task to the attribute names as defined by the list.
For example, to add support for the “basename” task, simply do the following:
from pant.pant import ant_attr_map ant_attr_map["basename"]=["file", "property"]
Any Ant task invoked from PAnt can be prefixed with “ant_”. This is useful in case if there is a custom task that conflicts with a Python keyword, e.g., pant.ant_del(file).
PAnt does not support nested text elements and Ant namespaces.
To get started with PAnt, please follow these instructions.
PAnt is provided under BSD license.
If you have any PAnt-related feedback, please add a comment to “this page”:/ant-jython-task