Monday, October 18, 2010

Creating Robot Framework jar distribution

As of Robot Framework 2.5.2, it is also distributed as a single stand alone jar file.

The goal was to create a jar distribution with following properties:
  • Standalone executable jar file, i.e. java -jar robotframework.jar mystest.txt would suffice to run tests.
  • The exit code of the above command must be the same as if mytests.txt was executed with pybot.
  • There would also be a programmatic API to start Robot Framework test execution from any Java program
While there was a wealth of information about Jython-Java interaction available in the net, I could not find a single set of instructions describing all the necessary steps to create the required jar.  Since I was able to do it and since the process is general, I decided to write it down.

First requirement is the Jython standalone jar, which can be created with the Jython installer. Following the instructions in the Jython Book, I created three Java classes and one Python for the interaction. These classes implement the one-to-one Jython object factory described in the Jython book. Source code of these classes is available in Robot Framework source repository.

The java classes had to (of course) be compiled before inserting them into the jar file. Additionally, a MANIFEST.MF file with correct main-class had to be created. Once these were done, the files could be inserted into the Jython jar:
  • Robot Framework source code must be placed in Lib directory inside the jar
  • The java code must be inserted starting from the root of the and according to package structure
  • The MANIFEST.MF has be to in META-INF directory
That's pretty standard stuff expect for one gotcha: Zip file format (and jar files are Zip files) supports multiple entries with same path. Since the Jython jar already contains MANIFEST.MF and since Python's zipfile module does not support overriding or removing entries, the first released version of the jar distribution contained two manifest files. For reasons unknown to me, the one we inserted was effective when the jar file was executed with java, but adding files to the jar file with jar failed because of the duplicate manifest.

This was resolved by unpacking the Jython jar and replacing the MANIFEST.MF with our own on the file system before repackaging. .

All the Python source files were compiled with Jython's compileall module before they were inserted into the jar file. This presumably increases performance slightly.

I will probably do another post about how we finally got the jar distribution uploaded to Maven central, since that's an interesting story too.

1 comment:

adidas_pump said...

pretty cool! I'll try the standalone version right now.
Looks very useful for others that need to execute your tests in different machines.