Tuesday, April 11, 2006

Running jython scripts from Ant

Using the Script task it is possible to run jython, groovy, javascript etc. from Ant, either embedded within the script task or by reference to a source file.

In python/jython it is common to identify by whom the script is invoked and to behave accordingly e.g. depending if the code is imported as a module or invoked directly by python/jython. This is done by inspecting the __name__ property which is set to "__main__" if invoked directly and to the name of the invoker if invoked e.g. by a module import.

I am not sure if it is Ant or Bean Scripting Framework (BSF) which is responsible but the __name__ property is set to "main" and not "__main__" if invoked using the Ant script task. This is actually quite usefull because it is possible to identify if the script is invoked from the command prompt or if it is invoked by ant. However to avoid confusion I believe that Ant/BSF should set the __name__ property to something other than main, perhaps "__ant__".

No comments: