Somebody asked me about examples for PAnt Jython wrapper. Here are some. I’ll be updating this page with more examples in the future.

Following is a simple echo task. Note that has to be on your “python.path”. You can set it by adding it
to your ANT_OPTS environment variable (ANT_OPTS=-Dpython.path=python_path).

        <script language="jython">
from pant import *
pant.execTask(name="echo", message="foo")

Following is a copy task example. Note the use of “nested” function to denore nested elements. “expandproperties” is assigned
an empty dictionary since it does not have any attributes.

pant = PAnt( project )
pant.copy(todir="${test.prop}", fileset=nested(dir=".", include=nested(name="*.xml")), 
          filterchain=nested(expandproperties={}) ) 

Following is an example of Exec task. Multiple “env” elements
are distinguished by adding the suffix “_number“. The suffix can in fact be anything, PAnt,
simply ignores the substring starting with underbar.
This example also demonstrates how you can mix and match python variables and Ant properties
in the same piece of code.

pant.exec(dir="${bin.dir}", executable=shellFile,  failonerror="true", resultproperty="result.code",
          env_1=nested(key="key1", value='${val1}'),
          env_2=nested(key="key2", value='${val2}'),
          env_3=nested(key="key3", value='${val3}'),
          arg=nested(line=commandLine) )


MyArch Jython Task provides tighter integration of Ant and Jython. You may want to use it together with PAnt.

Please refer to our official PAnt project page for more information and to download PAnt

3 thoughts on “Jython Ant Wrapper Examples

  1. Hi,
    i love you pant module !!, i have added
    this lines from the groovy antbuilder
    def createDummyProject():
    project =Project();
    logger = DefaultLogger();
    logger.setOutputPrintStream( System.out );
    logger.setErrorPrintStream( System.out );
    logger.setMessageOutputLevel( Project.MSG_INFO );
    project.addBuildListener( logger );
    return project;

    and now i’am able to do ant without a single xml line ;)
    Regards an thanks for youir blog

  2. Hi Godin,
    I’m glad you liked it. I’m wondering how are you dealing with targets. From my experience, creating coarse-grained targets and then invoking PAnt script from those targets (as opposed to using Jython to drive Ant, which you can certainly do) gives the best of both world – declarative nature of Ant targets and the power of Python.


  3. Hi Alexander,
    i’am not in the process off building an artifact in multiple phase(not a build file), but more to create rich admin shell,
    in fact i combines the powerful python cmd module and you pant module to do some install script and admin console
    ex(download template conf file from svn,install tomcat+apache,apply filter to template)
    or from the shell (start the server,stop a cluster branch etc..)

    it looks like more the WAS6 jython scripting shell wsadmin , but cost 0 and work with tomcat ;)
    for my compilation i use maven for now
    i migrate from ivy to maven recently (pressure of the market) and now i regret….


Comments are closed.