There are 2 ways to execute Python scripts in Ant:
JSR-223 enables dynamic languages to be callable via Java in a seamless manner. Unfortunately this is currently only supported with Jython 2.2.1 and not the latest 2.5.0. Since PyAMF only supports Jython 2.5 or newer, you have to use the PythonInterpreter class directly. This style of embedding code is very similar to making use of a scripting engine, but it has the advantage of working with Jython 2.5.
Here you see the gateway running in Ant:
[mkdir] Created dir: build/classes
[javac] Compiling 1 source file to build/classes
[mkdir] Created dir: build/jar
[jar] Building jar: build/jar/HelloWorld.jar
[java] *sys-package-mgr*: processing new jar, 'build/jar/HelloWorld.jar'
[java] Running AMF gateway on http://localhost:8000
Read the complete howto, including a sample server/client, on http://pyamf.org/tutorials/jython/ant.html.
Today a new tutorial was added that shows you how to deploy your PyAMF applications with Apache Tomcat and modjy. modjy is an implementation of a WSGI compliant gateway/server for Jython, built on Java/J2EE servlets. This allows you to run Jython WSGI applications inside a Java/J2EE servlet container, e.g. Apache Tomcat. modjy is part of the Jython project.
Now that the upcoming Jython 2.5 is getting closer to it's final release, I thought it would be nice to create an example that shows PyAMF running on top of Jython. Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrates with the Java platform. It thus allows you to run Python scripts on any Java platform.
Below you see a screenshot of the example, which was created using Java's native Swing UI toolkit. It allows you to start a simple AMF server on localhost:8000 and embeds an AMF client that makes calls to that server. This particular example doesn't interact with an Adobe Flash Player application, it simply shows it's possible to run PyAMF servers and clients on top of the upcoming Jython release.
Today I decided to introduce some Jython in one of our build scripts at work because it was simply too painful to use a pure Ant script. It would/ve been nice to avoid the Jython dependency but after screwing around with Ant for a few hours I decided to write a Python script to do the job.
Although the Ant documentation for the Script task is pretty good it wasn't dead simple to hook up Jython to Ant, and because I couldn't find any examples online I decided to create a simple example so others might benefit from it. All files (except for the .jar's) are available in the sandbox of the PyAMF repository.
- Download Jython and the JSR 223 scripting engine .jar files for Jython
- Externalize some paths in a build.properties file (optional)
- Create a Python test script that prints out some property defined in the Ant build file
- Create the Ant build.xml file that defines a Script task and includes the build.properties file
Run the Ant script on the console and it should output something like:
[script] Apache Ant told Jython: Hello World!